DateUtils 时间工具类

 public final static String patternTime1 = "yyyy-MM-dd HH:mm:ss";
    public final static String patternTime2 = "yyyy-MM-dd";
    public final static String patternTime3 = "MM-dd";
    public final static String patternTime4 = "MM-dd HH:mm";
    public final static String patternTime5 = "yyyy-MM";
    public final static String patternTime6 = "HH:mm";
    public final static String patternTime7 = "M月";
    public final static String patternTime8 = "yyyy-MM-dd HH";
    public final static String patternTime9 = "yyyy-MM-dd HH:mm";
    public final static String patternTime10 = "yyyy/MM/dd HH:mm:ss";
    public final static String Replace = "0";
    public final static String SUFFIX = ":00";

   /**
     * 获取指定日期
     *
     * @param patternTime 时间格式
     * @return 返回时间
     */
    public static String getNowTime(String patternTime, int number) {
        return LocalDateTime.now().plusDays(number).format(DateTimeFormatter.ofPattern(patternTime));
    }

    /**
     * 获取当天之前的几天日期,不包括当天
     *
     * @param number 日期数目
     * @return 返回日期数组
     */
    public static List<String> takeDateByNumberForShow(int number, String pattern) {
        List<String> dateList = new ArrayList<>();
        DateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        for (int i = number; i > 0; i--) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -i);
            String itemDate = simpleDateFormat.format(calendar.getTime());
            dateList.add(itemDate);
        }
        return dateList;
    }


    /**
     * 获取当天之前的几天日期,包含当天
     *
     * @param number 日期数目
     * @return 返回日期数组
     */
    public static List<String> takeCurrentDateByNumber(int number, String pattern) {
        List<String> dateList = new ArrayList<>();
        DateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        for (int i = number; i >= 0; i--) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -i);
            String itemDate = simpleDateFormat.format(calendar.getTime());
            dateList.add(itemDate);
        }
        return dateList;
    }

    /**
     * 获取距离当前月的指定月份数
     */
    public static List<String> takeMonth(int mon, String pattern) {
        List<String> list = new ArrayList<>();
        for (int i = mon; i >= 0; i--) {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.MONTH, -i);
            Date date = c.getTime();
            list.add(sdf.format(date));
        }
        return list;
    }

    //  <E> ArrayList<E>

    /**
     * 获取距离当前月的指定月份
     */
    public static String takeSpecifiedMonth(int mon, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -mon);
        Date date = c.getTime();
        return sdf.format(date);
    }

    /**
     * 获取月份的时间间隔 {2021-05-01,2021-06-01},{2021-06-01,2021-07-01}
     */

    public static LinkedHashMap<String, String> getMonthIntervalTimes() {
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        List<String> months = takeMonth(5, patternTime5);
        for (int i = 0; i < months.size() - 1; i++) {
            map.put(months.get(i), months.get(i + 1));
        }
        return map;
    }

    /**
     * 获取距离当前时间多少分钟
     */
    public static String getPushMinutesTimes(int minutes) {
        return LocalDateTime.now().plusMinutes(minutes).format(DateTimeFormatter.ofPattern(DateUtils.patternTime1));
    }

    /**
     * 获取当前时间以10分钟为间隔的多个时间 eg: 23:10 23:20 23:30
     */

    public static List<String> getTenIntervalTimes(int num, String pattern, String suffix) {
        List<String> list = new ArrayList<>();
        for (int i = num; i >= 0; i--) {
            LocalDateTime dateTime = LocalDateTimeUtil.offset(LocalDateTime.now(), -10L * i, ChronoUnit.MINUTES);
            String str = dateTime.format(DateTimeFormatter.ofPattern(pattern));
            StringBuilder sb = new StringBuilder(str);
            sb.replace(sb.length() - 1, sb.length(), Replace);
            if (suffix == null) {
                list.add(sb.toString());
            } else {
                list.add(dateTime.format(DateTimeFormatter.ofPattern(patternTime2)) + " " + sb.toString() + suffix);
            }
        }
        return list;
    }


    /**
     * 获取10分钟的时间间隔 {2021-05-23 23:10=2021-05-23 23:20}
     */

    public static LinkedHashMap<String, String> getTenIntervalTimes() {
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        List<String> intervalTimes = getTenIntervalTimes(5, patternTime6, SUFFIX);
        for (int i = 0; i < intervalTimes.size() - 1; i++) {
            map.put(intervalTimes.get(i), intervalTimes.get(i + 1));
        }
        return map;
    }

    /**
     * 获取指定日期开始时间 xxxx-xx-xx 00:00:00
     *
     * @param number 离本日天数
     */
    public static String dayStart(int number) {
        return LocalDateTime.of(LocalDate.now().plusDays(number), LocalTime.MIN)
                .format(DateTimeFormatter.ofPattern(patternTime1));
    }

    /**
     * 获取指定日期结束时间 xxxx-xx-xx 23:59:59
     */
    public static String dayEnd(int number) {
        return LocalDateTime.of(LocalDate.now().plusDays(number), LocalTime.MAX)
                .format(DateTimeFormatter.ofPattern(patternTime1));
    }


    /**
     * 日期格式化
     */
    public static String dateToString(Date date, String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 日期格式转换成 Y/M/D/00:00:00格式
     */
    public static Date formatYMDTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.patternTime2);
        try {
            String dateStr = dateToString(date, DateUtils.patternTime2);
            Date parse = sdf.parse(dateStr, new ParsePosition(0));
            return parse;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 日期增加count天
     */
    public static Date addCountByDay(Date date, Integer count) {
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.DATE, count);
            return cal.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static Long takeStartTime(Calendar todayStart) {
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 1);
        todayStart.set(Calendar.MILLISECOND, 1);
        return todayStart.getTime().getTime();
    }

    private static Long takeEndTime(Calendar todayStart) {
        todayStart.set(Calendar.HOUR_OF_DAY, 23);
        todayStart.set(Calendar.MINUTE, 59);
        todayStart.set(Calendar.SECOND, 59);
        todayStart.set(Calendar.MILLISECOND, 999);
        return todayStart.getTime().getTime();
    }

    /**
     * 非标准时间格式化
     */
    public static String formatDtime(String time) {
        String[] ts = time.split("T");
        String sub = ts[1].substring(0, 8);
        return ts[0] + " " + sub;
    }

    /**
     * 非标准时间格式化
     */
    public static Date formatDate(String time) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String[] ts = time.split("T");
            String sub = ts[1].substring(0, 8);
            return sdf.parse(ts[0] + " " + sub);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String formateTimeStamp(Long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date(Long.parseLong(String.valueOf(time))));
    }


    // 获取第二天整点
    public static Date nextDay(int i) {
        Date date = formatYMDTime(new Date());
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, i);
            return calendar.getTime();
        }
        return null;
    }

    /**
     * 时间戳转换成时间
     */
    public static String timeStampToTime(String s,String pattern) {
        long lt = new Long(s);
        return timeStampToTime(lt, pattern);
    }


    /**
     * 时间戳转换成时间
     */
    public static String timeStampToTime(Long s,String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = new Date(s);
        return sdf.format(date);
    }

    public static String dateToTimeStamp(String data) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(data);
            return String.valueOf(date.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取指定小时时间 21:00
     */
    public static String getBeforeByHourTime(int hour) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:00");
        //(1)获取当前时间
        LocalDateTime date = LocalDateTime.now();
        //(2)获取当前时间的前几小时时间
        LocalDateTime localDateTime = date.minusHours(hour);

        return dateTimeFormatter.format(localDateTime);
    }


    public static Date takeAssignDateHour(Integer num,String pattern) {
        if (num == null || num == 0) {
            return new Date();
        }
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            String res = takeStrAssignDateHour(num, pattern);
            return df.parse(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Date();
    }


    public static String takeStrAssignDateHour(Integer num,String pattern){
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + num);
        return df.format(calendar.getTime());
    }

    public static Date strToDate(String date, String pattern){
        try {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.parse(date);
        }catch (Exception e){
            e.printStackTrace();
        }
       return new Date();
    }

    public static String strToPatternStr(String date,String sourcePattern,String targetPattern){
        try {
            SimpleDateFormat df = new SimpleDateFormat(sourcePattern);
            SimpleDateFormat df2 = new SimpleDateFormat(targetPattern);
            Date parse = df.parse(date);
            return df2.format(parse);
        }catch (Exception e){
            e.printStackTrace();
        }
        return getNowTime(targetPattern,0);
    }

 

posted @ 2021-11-30 13:50  二先森  阅读(136)  评论(0编辑  收藏  举报