Java日期操作

public class DateUtil {

    private static final DateFormat ISO_DATE_FORMAT = newIsoDateFormat();
    private static final DateFormat ISO_DATE_TIME_FORMAT = newIsoDateTimeFormat();
    private static final DateFormat ISO_DATE_TIME_FORMAT_WITH_MS = newIsoDateTimeWithMsFormat();

    public DateUtil() {
    }


    public static String today() {
        return LocalDate.now().format(DateTimeFormatter.ISO_DATE);
    }


    /**
     * 根据当前时间向前推几个月
     *
     * @param month 向前推的月份
     * @return
     */
    public static String todayPlusMonths(int month) {
        return plusMonths(LocalDate.now(), month);
    }


    /**
     * 给定的日期向前推指定的月份
     *
     * @param localDate 给定日期
     * @param month     减去或者加上的月份, 正数为加, 负数为减
     * @return 运算后的年月 比如“2004-12”
     */
    public static String plusMonths(final LocalDate localDate, final int month) {
        return localDate
                .plusMonths(month)
                .format(DateTimeFormatter.ofPattern("uuuu-MM"));
    }


    public static DateFormat newIsoDateFormat() {
        return strictDateFormatForPattern("yyyy-MM-dd");
    }

    public static DateFormat newIsoDateTimeWithUtcTimeZoneFormat() {
        return strictDateFormatForPattern("yyyy-MM-dd'T'HH:mm:ssX");
    }

    public static DateFormat newIsoDateTimeFormat() {
        return strictDateFormatForPattern("yyyy-MM-dd'T'HH:mm:ss");
    }

    public static DateFormat newIsoDateTimeWithMsFormat() {
        return strictDateFormatForPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
    }

    public static DateFormat newTimestampDateFormat() {
        return strictDateFormatForPattern("yyyy-MM-dd HH:mm:ss.SSS");
    }

    private static DateFormat strictDateFormatForPattern(String pattern) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        dateFormat.setLenient(false);
        return dateFormat;
    }

    public static synchronized String formatAsDatetime(Date date) {
        return date == null ? null : ISO_DATE_TIME_FORMAT.format(date);
    }

    public static synchronized String formatAsDatetimeWithMs(Date date) {
        return date == null ? null : ISO_DATE_TIME_FORMAT_WITH_MS.format(date);
    }

    public static String formatAsDatetime(Calendar calendar) {
        return calendar == null ? null : formatAsDatetime(calendar.getTime());
    }

    public static synchronized Date parse(String dateAsString) {
        try {
            return dateAsString == null ? null : ISO_DATE_FORMAT.parse(dateAsString);
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }

    public static synchronized Date parseDatetime(String dateAsString) {
        try {
            return dateAsString == null ? null : ISO_DATE_TIME_FORMAT.parse(dateAsString);
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }

    public static synchronized Date parseDatetimeWithMs(String dateAsString) {
        try {
            return dateAsString == null ? null : ISO_DATE_TIME_FORMAT_WITH_MS.parse(dateAsString);
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }

    public static Calendar toCalendar(Date date) {
        if (date == null) {
            return null;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar;
        }
    }

    public static int yearOf(Date date) {
        return toCalendar(date).get(1);
    }

    public static int monthOf(Date date) {
        return toCalendar(date).get(2) + 1;
    }

    public static int dayOfMonthOf(Date date) {
        return toCalendar(date).get(5);
    }

    public static int dayOfWeekOf(Date date) {
        return toCalendar(date).get(7);
    }

    public static int hourOfDayOf(Date date) {
        return toCalendar(date).get(11);
    }

    public static int minuteOf(Date date) {
        return toCalendar(date).get(12);
    }

    public static int secondOf(Date date) {
        return toCalendar(date).get(13);
    }

    public static int millisecondOf(Date date) {
        return toCalendar(date).get(14);
    }

    public static Date truncateTime(Date date) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = toCalendar(date);
            cal.set(11, 0);
            cal.set(12, 0);
            cal.set(13, 0);
            cal.set(14, 0);
            return cal.getTime();
        }
    }

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

    public static Date yesterday() {
        Calendar cal = Calendar.getInstance();
        cal.add(5, -1);
        return cal.getTime();
    }

    public static Date tomorrow() {
        Calendar cal = Calendar.getInstance();
        cal.add(5, 1);
        return cal.getTime();
    }
}



发布了194 篇原创文章 · 获赞 55 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_42470710/article/details/103326792