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时为输入天数的前一天

    --待扩充--

  • 相关阅读:
    [LeetCode]Interleaving String关于遍历和动态规划
    [LeetCode]Power
    [LeetCode]Two Sum
    [LeetCode]wildcard matching通配符实现之贪心法
    [LeetCode]wildcard matching通配符实现之动态规划
    [LeetCode]wildcard matching通配符实现之递归
    回文分割
    iOS开发之应用内检测手机锁屏,解锁状态
    iOS 拨打电话
    iOS 中文转拼音
  • 原文地址:https://www.cnblogs.com/yoyotl/p/6708685.html
Copyright © 2011-2022 走看看