Java 日期处理工具类 —— DateUtils

Java 日期处理工具类 —— DateUtils,第1张

Java 日期处理工具类 —— DateUtils
package com.aboplus.utils;

import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class DateUtils {

	public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    
    public static String formatDate(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    
    @SneakyThrows
    public static Date parse(String date, String format) {
        return new SimpleDateFormat(format).parse(date);
    }

    /

    
    public static boolean sameDay(Date date1, Date date2) {
        if (date1 == null && date2 == null) {
            return true;
        }
        if (date1 == null || date2 == null) {
            return false;
        }
        String str1 = formatDate(date1, "yyyy-MM-dd");
        String str2 = formatDate(date2, "yyyy-MM-dd");
        return StringUtils.equals(str1, str2);
    }

    
    public static Date currentTime() {
        return new Date();
    }

    /

    
    public static Date getBeforeByDays(Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - days);
        return calendar.getTime();
    }

    
    public static Date getBeforeByDays(Date date, Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - days);
        return calendar.getTime();
    }

    
    public static Date getBeforeByHours(Integer hours) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hours);
        return calendar.getTime();
    }

    
    public static Date getBeforeByHours(Date date, Integer hours) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hours);
        return calendar.getTime();
    }

    
    public static Date getBeforeByMinutes(Integer minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - minutes);
        return calendar.getTime();
    }

    
    public static Date getBeforeByMinutes(Date date, Integer minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - minutes);
        return calendar.getTime();
    }

    
    public static Date getBeforeBySeconds(Integer seconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) - seconds);
        return calendar.getTime();
    }

    
    public static Date getBeforeBySeconds(Date date, Integer seconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) - seconds);
        return calendar.getTime();
    }

    
    public static Date getAfterByDays(Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + days);
        return calendar.getTime();
    }

    
    public static Date getAfterByDays(Date date, Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + days);
        return calendar.getTime();
    }

    
    public static Date getAfterByHours(Integer hours) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hours);
        return calendar.getTime();
    }

    
    public static Date getAfterByHours(Date date, Integer hours) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hours);
        return calendar.getTime();
    }

    
    public static Date getAfterByMinutes(Integer minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + minutes);
        return calendar.getTime();
    }

    
    public static Date getAfterByMinutes(Date date, Integer minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + minutes);
        return calendar.getTime();
    }

    
    public static Date getAfterBySeconds(Integer seconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + seconds);
        return calendar.getTime();
    }

    
    public static Date getAfterBySeconds(Date date, Integer seconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + seconds);
        return calendar.getTime();
    }

    
    public static Date handleTime(Date date, int field, int value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, value);
        return calendar.getTime();
    }

    /

    
    public static long differSeconds(Date startDate, Date endDate) {
        long diffTime = endDate.getTime() - startDate.getTime();
        return (diffTime / 1000);
    }

    
    public static long differMinutes(Date startDate, Date endDate) {
        return differSeconds(startDate, endDate) / 60;
    }

    
    public static long differHours(Date startDate, Date endDate) {
        return differMinutes(startDate, endDate) / 60;
    }

    
    public static long differDays(Date startDate, Date endDate) {
        return differHours(startDate, endDate) / 24;
    }

    /

    
    public static Date getStartOfThisDay(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        setStartTime(cal);
        return cal.getTime();
    }

    
    private static void setStartTime(Calendar cal) {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
    }

    
    public static Date getEndOfThisDay(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        setEndTime(cal);
        return cal.getTime();
    }

    
    private static void setEndTime(Calendar cal) {
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
    }

    /

    
    public static int getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    
    public static String getYearStr(Date date) {
        return formatDate(date, "yyyy");
    }

    
    public static int getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }

    
    public static String getMonthStr(Date date) {
        return formatDate(date, "MM");
    }

    
    public static int getDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    
    public static String getDayStr(Date date) {
        return formatDate(date, "dd");
    }

    
    public static int getHour(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.HOUR_OF_DAY);
    }

    
    public static String getHourStr(Date date) {
        return formatDate(date, "HH");
    }

    
    public static int getMinute(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MINUTE);
    }

    
    public static String getMinuteStr(Date date) {
        return formatDate(date, "mm");
    }

    
    public static int getSecond(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.SECOND);
    }

    
    public static String getSecondStr(Date date) {
        return formatDate(date, "ss");
    }

    
    public static int getWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return week == 0 ? 7 : week;
    }

    
    public static String getWeekStr(Date date) {
        return formatDate(date, "E");
    }

    /

    
    public static List getDates(Date startTime, Date endTime) {
        LocalDate startDate = convertDate2LocalDate(startTime);
        LocalDate endDate = convertDate2LocalDate(endTime);
        return startDate.datesUntil(endDate).collect(Collectors.toList());
    }

    
    public static List getMinuteDates(Date startTime, Date endTime) {
        LocalDateTime startDate = convertDate2LocalDateTime(startTime);
        LocalDateTime endDate = convertDate2LocalDateTime(endTime);

        long numOfDays = ChronoUnit.MINUTES.between(startDate, endDate);

        return Stream.iterate(startDate, date -> date.plusMinutes(1))
                .limit(numOfDays)
                .collect(Collectors.toList());
    }

    
    public static LocalDate convertDate2LocalDate(Date date) {
        return date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDate();
    }

    
    public static LocalDateTime convertDate2LocalDateTime(Date date) {
        return date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
    }

    
    public static Date convertLocalDate2Date(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay()
                .atZone(ZoneId.systemDefault())
                .toInstant());
    }

    /

    
    public static String getRangeDateTime(Date startDate, Date endDate, String format) {
        if (Objects.isNull(startDate)
                || Objects.isNull(endDate)
                || (!StringUtils.equals(format, YYYYMMDD)
                && !StringUtils.equals(format, YYYY_MM_DD)
                && !StringUtils.equals(format, YYYYMMDDHHMMSS)
                && !StringUtils.equals(format, YYYY_MM_DD_HH_MM_SS))) {
            return null;
        }
        return String.format("%s - %s", formatDate(startDate, format), formatDate(endDate, format));
    }

    /

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5692670.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存