zoukankan      html  css  js  c++  java
  • 日期常用操作类DateUtil

    一、给定yyyy-MM-dd hh:mm:ss格式的字符串,返回Date.

        public Date convertStr2Date(String dateString) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                Date date = sdf.parse(dateString);
                return date;
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }

    二、取得指定日期的前几分钟或者后几分钟的日期,返回"yyyy-MM-dd hh:mm:ss"形式的字符串。

        public String arroundIntervalMinute(Date inDate,int interval) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Calendar calender = Calendar.getInstance();
            calender.setTime(inDate);
            calender.add(Calendar.MINUTE, interval);
            Date endDateTime = calender.getTime();
            return sdf.format(endDateTime);
        }

    三、取得指定日期的前几天或者后几天的日期,返回"yyyy-MM-dd 00:00:00"形式的字符串.

    public String arroundIntervalDay(Date inDate, int interval) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calender = Calendar.getInstance();
            calender.setTime(inDate);
            calender.add(Calendar.DATE, interval);
            Date endDateTime = calender.getTime();
            String endDate = sdf.format(endDateTime);
            endDate += " 00:00:00";
            return endDate;
        }

    PS:返回00:00:00是我的需求,为了统计每一天的数据.

    interval为1时,为输入天数的后一天,为-1时为输入天数的前一天

    --待扩充--

  • 相关阅读:
    鼠标滑过,解决ul下 li下a的背景与父级Li不同宽的问题
    php函数
    常用函数之数组函数
    php流程控制
    php运算符
    php常量
    php变量的数据类型
    PHP是什么
    css3新增属性
    html5的常用标签
  • 原文地址:https://www.cnblogs.com/yoyotl/p/6708685.html
Copyright © 2011-2022 走看看