Java常用时间工具类总结(珍藏版)

2022-11-13 13:11:21 时间 常用 珍藏

肝了两天,重新整理了下时间工具类,以后我就以该时间工具类进行项目开发了,后会不定期更新功能,也欢迎留言需求,让工具类不断的完善。

常量介绍

相关方法

工具类源码

package com.zyq.util.date;
 
import java.text.ParseException;
import java.text.SimpleDateFORMat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 

public class DateUtils {
 
    
    public static final int WEEK_DAYS = 7;
    
    public static final int YEAR_MONTHS = 12;
    
    public static final int DAY_HOURS = 24;
    
    public static final int HOUR_MINUTES = 60;
    
    public static final int DAY_MINUTES = 1440;
    
    public static final int MINUTE_SECONDS = 60;
    
    public static final int HOUR_SECONDS = 3600;
    
    public static final int DAY_SECONDS = 86400;
    
    public static final long SECOND_MILLISECONDS = 1000L;
    
    public static final long MINUTE_MILLISECONDS = 60000L;
    
    public static final long HOUR_MILLISECONDS = 3600000L;
    
    public static final long DAY_MILLISECONDS = 86400000L;
    
    public static final int WEEK_1_MONDAY = 1;
    
    public static final int WEEK_2_TUESDAY = 2;
    
    public static final int WEEK_3_WEDNESDAY = 3;
    
    public static final int WEEK_4_THURSDAY = 4;
    
    public static final int WEEK_5_FRIDAY = 5;
    
    public static final int WEEK_6_SATURDAY = 6;
    
    public static final int WEEK_7_SUNDAY = 7;
    
    public static final int MONTH_1_JANUARY = 1;
    
    public static final int MONTH_2_FEBRUARY = 2;
    
    public static final int MONTH_3_MARCH = 3;
    
    public static final int MONTH_4_APRIL= 4;
    
    public static final int MONTH_5_MAY = 5;
    
    public static final int MONTH_6_JUNE = 6;
    
    public static final int MONTH_7_JULY = 7;
    
    public static final int MONTH_8_AUGUST = 8;
    
    public static final int MONTH_9_SEPTEMBER = 9;
    
    public static final int MONTH_10_OCTOBER = 10;
    
    public static final int MONTH_11_NOVEMBER = 11;
    
    public static final int MONTH_12_DECEMBER= 12;
    
    public static final String FORMAT_DATE = "yyyy-MM-dd";
    
    public static final String FORMAT_HOUR = "yyyy-MM-dd HH";
    
    public static final String FORMAT_MINUTE = "yyyy-MM-dd HH:mm";
    
    public static final String FORMAT_SECOND = "yyyy-MM-dd HH:mm:ss";
    
    public static final String FORMAT_MILLISECOND = "yyyy-MM-dd HH:mm:ss:SSS";
    
    public static final String FORMAT_NO_DATE = "yyyyMMdd";
    
    public static final String FORMAT_NO_HOUR = "yyyyMMddHH";
    
    public static final String FORMAT_NO_MINUTE = "yyyyMMddHHmm";
    
    public static final String FORMAT_NO_SECOND = "yyyyMMddHHmmss";
    
    public static final String FORMAT_NO_MILLISECOND = "yyyyMMddHHmmssSSS";
    
    private static final Map<String, SimpleDateFormat> simpleDateFormatMap = new HashMap<String, SimpleDateFormat>();
    static {
        simpleDateFormatMap.put(FORMAT_DATE, new SimpleDateFormat(FORMAT_DATE));
        simpleDateFormatMap.put(FORMAT_HOUR, new SimpleDateFormat(FORMAT_HOUR));
        simpleDateFormatMap.put(FORMAT_MINUTE, new SimpleDateFormat(FORMAT_MINUTE));
        simpleDateFormatMap.put(FORMAT_SECOND, new SimpleDateFormat(FORMAT_SECOND));
        simpleDateFormatMap.put(FORMAT_MILLISECOND, new SimpleDateFormat(FORMAT_MILLISECOND));
        simpleDateFormatMap.put(FORMAT_NO_DATE, new SimpleDateFormat(FORMAT_NO_DATE));
        simpleDateFormatMap.put(FORMAT_NO_HOUR, new SimpleDateFormat(FORMAT_NO_HOUR));
        simpleDateFormatMap.put(FORMAT_NO_MINUTE, new SimpleDateFormat(FORMAT_NO_MINUTE));
        simpleDateFormatMap.put(FORMAT_NO_SECOND, new SimpleDateFormat(FORMAT_NO_SECOND));
        simpleDateFormatMap.put(FORMAT_NO_MILLISECOND, new SimpleDateFormat(FORMAT_NO_MILLISECOND));
    }
 
    
    private static SimpleDateFormat getSimpleDateFormat(String formatStyle) {
        SimpleDateFormat dateFormat = simpleDateFormatMap.get(formatStyle);
        if (Objects.nonNull(dateFormat)) {
            return dateFormat;
        }
        return new SimpleDateFormat(formatStyle);
    }
 
    
    public static String format(Date date, String formatStyle) {
        if (Objects.isNull(date)) {
            return "";
        }
        return getSimpleDateFormat(formatStyle).format(date);
    }
 
    
    public static String formatDate(Date date) {
        return format(date, FORMAT_DATE);
    }
 
    
    public static String formatDateTime(Date date) {
        return format(date, FORMAT_SECOND);
    }
 
    
    public static String formatDateTimeStamp(Date date) {
        return format(date, FORMAT_MILLISECOND);
    }
 
    
    public static Date parseDate(String dateString) {
        return parse(dateString, FORMAT_DATE);
    }
 
    
    public static Date parseDateTime(String dateTimeStr) {
        return parse(dateTimeStr, FORMAT_SECOND);
    }
 
    
    public static Date parseDateTimeStamp(String dateTimeStampStr) {
        return parse(dateTimeStampStr, FORMAT_MILLISECOND);
    }
 
    
    public static Date parse(String dateString, String formatStyle) {
        String s = getString(dateString);
        if (s.isEmpty()) {
            return null;
        }
        try {
            return getSimpleDateFormat(formatStyle).parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
 
    
    private static String getString(String s) {
        return Objects.isNull(s) ? "" : s.trim();
    }
 
    
    public static Date getDateStart(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
 
    
    public static Date getDateEnd(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }
 
    
    public static int getDateNo(Date date) {
        if (Objects.isNull(date)) {
            return 0;
        }
        return Integer.valueOf(format(date, FORMAT_NO_DATE));
    }
 
    
    public static long getDateTimeNo(Date date) {
        if (Objects.isNull(date)) {
            return 0L;
        }
        return Long.valueOf(format(date, FORMAT_NO_SECOND));
    }
 
    
    public static long getDateTimeStampNo(Date date) {
        if (Objects.isNull(date)) {
            return 0L;
        }
        return Long.valueOf(format(date, FORMAT_NO_MILLISECOND));
    }
 
    
    public static int getWeek(Date date) {
        if (Objects.isNull(date)) {
            return 0;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return getWeek(calendar);
    }
 
    
    private static int getWeek(Calendar calendar) {
        switch (calendar.get(Calendar.DAY_OF_WEEK)) {
        case Calendar.MONDAY:
            return 1;
        case Calendar.TUESDAY:
            return 2;
        case Calendar.WEDNESDAY:
            return 3;
        case Calendar.THURSDAY:
            return 4;
        case Calendar.FRIDAY:
            return 5;
        case Calendar.SATURDAY:
            return 6;
        case Calendar.SUNDAY:
            return 7;
        default:
            return 0;
        }
    }
 
    
    public static int getWeekOfYear(Date date) {
        if (Objects.isNull(date)) {
            return -1;
        }
        int weeks = getWeekOfYearIgnoreLastYear(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int week = getWeek(calendar);
        if (week == 1) {
            return weeks;
        }
        return weeks - 1;
    }
 
    
    public static int getWeekOfYearIgnoreLastYear(Date date) {
        if (Objects.isNull(date)) {
            return -1;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int days = calendar.get(Calendar.DAY_OF_YEAR);
        int weeks = days / 7;
        // 如果是 7 的倍数,则表示恰好是多少周
        if (days % 7 == 0) {
            return weeks;
        }
        // 如果有余数,则需要再加 1
        return weeks + 1;
    }
 
    
    public static Datenode getDateNode(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        DateNode node = new DateNode();
        node.setTime(format(date, FORMAT_MILLISECOND));
        node.setYear(calendar.get(Calendar.YEAR));
        node.setMonth(calendar.get(Calendar.MONTH) + 1);
        node.setDay(calendar.get(Calendar.DAY_OF_MONTH));
        node.setHour(calendar.get(Calendar.HOUR_OF_DAY));
        node.setMinute(calendar.get(Calendar.MINUTE));
        node.setSecond(calendar.get(Calendar.SECOND));
        node.setMillisecond(calendar.get(Calendar.MILLISECOND));
        node.setWeek(getWeek(calendar));
        node.setDayOfYear(calendar.get(Calendar.DAY_OF_YEAR));
        node.setWeekOfYear(getWeekOfYear(date));
        node.setWeekOfYearIgnoreLastYear(getWeekOfYearIgnoreLastYear(date));
        node.setMillisecondStamp(date.getTime());
        node.setSecondStamp(node.getMillisecondStamp() / 1000);
        return node;
    }
 
    
    public static Date add(Date date, int field, int amount) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, amount);
        return calendar.getTime();
    }
 
    
    public static Date addYear(Date date, int year) {
        return add(date, Calendar.YEAR, year);
    }
 
    
    public static Date addMonth(Date date, int month) {
        return add(date, Calendar.MONTH, month);
    }
 
    
    public static Date aDDDay(Date date, int day) {
        return add(date, Calendar.DAY_OF_YEAR, day);
    }
 
    
    public static Date addWeek(Date date, int week) {
        return add(date, Calendar.WEEK_OF_YEAR, week);
    }
 
    
    public static Date addHour(Date date, int hour) {
        return add(date, Calendar.HOUR_OF_DAY, hour);
    }
 
    
    public static Date addMinute(Date date, int minute) {
        return add(date, Calendar.MINUTE, minute);
    }
 
    
    public static Date addSecond(Date date, int second) {
        return add(date, Calendar.SECOND, second);
    }
 
    
    public static Date addMillisecond(Date date, int millisecond) {
        return add(date, Calendar.MILLISECOND, millisecond);
    }
 
    
    public static Date getWeekDate(Date date, int index) {
        if (index < WEEK_1_MONDAY || index > WEEK_7_SUNDAY) {
            return null;
        }
        int week = getWeek(date);
        return addDay(date, index - week);
    }
 
    
    public static Date getWeekDateStart(Date date) {
        return getDateStart(getWeekDate(date, WEEK_1_MONDAY));
    }
 
    
    public static Date getWeekDateEnd(Date date) {
        return getWeekDateEnd(getWeekDate(date, WEEK_7_SUNDAY));
    }
 
    
    public static List<Date> getWeekDateList(Date date) {
        if (Objects.isNull(date)) {
            return Collections.emptyList();
        }
        // 获取本周开始时间
        Date weekFromDate = getWeekDateStart(date);
        // 获取本周截止时间
        Date weekeEndDate = getWeekDateEnd(date);
        return getBetweenDateList(weekFromDate, weekeEndDate, true);
    }
 
    
    public static List<String> getWeekDateList(String dateString) {
        Date date = parseDate(dateString);
        if (Objects.isNull(date)) {
            return Collections.emptyList();
        }
        return getDateStrList(getWeekDateList(date));
    }
 
    
    public static List<Date> getMonthDateList(Date date) {
        if (Objects.isNull(date)) {
            return Collections.emptyList();
        }
        Date monthDateStart = getMonthDateStart(date);
        Date monthDateEnd = getMonthDateEnd(date);
        return getBetweenDateList(monthDateStart, monthDateEnd, true);
    }
 
    
    public static List<String> getMonthDateList(String dateString) {
        Date date = parseDate(dateString);
        if (Objects.isNull(date)) {
            return Collections.emptyList();
        }
        return getDateStrList(getMonthDateList(date));
    }
    
    
    public static Date getMonthDateStart(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return getDateStart(calendar.getTime());
    }
 
    
    public static Date getMonthDateEnd(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Date monthDateStart = getMonthDateStart(date);
        Date nextMonthDateStart = getMonthDateStart(addMonth(monthDateStart, 1));
        return getDateEnd(addDay(nextMonthDateStart, -1));
    }
 
    
    public static int countBetweenDays(Date date1, Date date2) {
        if (Objects.isNull(date1) || Objects.isNull(date2)) {
            return -1;
        }
        // 获取两个日期 0 点 0 时 0 分 0 秒 0 毫秒时的时间戳(毫秒级)
        long t1 = getDateStart(date1).getTime();
        long t2 = getDateStart(date2).getTime();
        // 相差天数 = 相差的毫秒数 / 一天的毫秒数
        return (int) (Math.abs(t1 - t2) / DAY_MILLISECONDS);
    }
 
    
    public static List<Date> getBetweenDateList(Date date1, Date date2) {
        return getBetweenDateList(date1, date2, false);
    }
 
    
    public static List<Date> getBetweenDateList(Date date1, Date date2, boolean isContainParams) {
        if (Objects.isNull(date1) || Objects.isNull(date2)) {
            return Collections.emptyList();
        }
        // 确定前后日期
        Date fromDate = date1;
        Date toDate = date2;
        if (date2.before(date1)) {
            fromDate = date2;
            toDate = date1;
        }
        // 获取两个日期每天的开始时间
        Date from = getDateStart(fromDate);
        Date to = getDateStart(toDate);
        // 获取日期,开始循环
        List<Date> dates = new ArrayList<Date>();
        if (isContainParams) {
            dates.add(from);
        }
        Date date = from;
        boolean isBefore = true;
        while (isBefore) {
            date = addDay(date, 1);
            isBefore = date.before(to);
            if (isBefore) {
                dates.add(getDateStart(date));
            }
        }
        if (isContainParams) {
            dates.add(to);
        }
        return dates;
    }
 
    
    public static List<String> getBetweenDateList(String dateString1, String dateString2) {
        return getBetweenDateList(dateString1, dateString2, false);
    }
 
    
    public static List<String> getBetweenDateList(String dateString1, String dateString2, boolean isContainParams) {
        Date date1 = parseDate(dateString1);
        Date date2 = parseDate(dateString2);
        List<Date> dates = getBetweenDateList(date1, date2, isContainParams);
        return getDateStrList(dates);
    }
 
    
    public static List<String> getDateStrList(List<Date> dates) {
        if (dates.isEmpty()) {
            return Collections.emptyList();
        }
        List<String> dateList = new ArrayList<String>();
        for (Date date : dates) {
            dateList.add(formatDate(date));
        }
        return dateList;
    }
 
    static class DateNode {
        
        private int year;
        
        private int month;
        
        private int day;
        
        private int hour;
        
        private int minute;
        
        private int second;
        
        private int millisecond;
        
        private int week;
        
        private int dayOfYear;
        
        private int weekOfYear;
        
        private int weekOfYearIgnoreLastYear;
        
        private long secondStamp;
        
        private long millisecondStamp;
        
        private String time;
 
        public int getYear() {
            return year;
        }
 
        public void setYear(int year) {
            this.year = year;
        }
 
        public int getMonth() {
            return month;
        }
 
        public void setMonth(int month) {
            this.month = month;
        }
 
        public int getDay() {
            return day;
        }
 
        public void setDay(int day) {
            this.day = day;
        }
 
        public int getHour() {
            return hour;
        }
 
        public void setHour(int hour) {
            this.hour = hour;
        }
 
        public int getMinute() {
            return minute;
        }
 
        public void setMinute(int minute) {
            this.minute = minute;
        }
 
        public int getSecond() {
            return second;
        }
 
        public void setSecond(int second) {
            this.second = second;
        }
 
        public int getMillisecond() {
            return millisecond;
        }
 
        public void setMillisecond(int millisecond) {
            this.millisecond = millisecond;
        }
 
        public int getWeek() {
            return week;
        }
 
        public void setWeek(int week) {
            this.week = week;
        }
 
        public int getDayOfYear() {
            return dayOfYear;
        }
 
        public void setDayOfYear(int dayOfYear) {
            this.dayOfYear = dayOfYear;
        }
 
        public int getWeekOfYear() {
            return weekOfYear;
        }
 
        public void setWeekOfYear(int weekOfYear) {
            this.weekOfYear = weekOfYear;
        }
 
        public int getWeekOfYearIgnoreLastYear() {
            return weekOfYearIgnoreLastYear;
        }
 
        public void setWeekOfYearIgnoreLastYear(int weekOfYearIgnoreLastYear) {
            this.weekOfYearIgnoreLastYear = weekOfYearIgnoreLastYear;
        }
 
        public long getSecondStamp() {
            return secondStamp;
        }
 
        public void setSecondStamp(long secondStamp) {
            this.secondStamp = secondStamp;
        }
 
        public long getMillisecondStamp() {
            return millisecondStamp;
        }
 
        public void setMillisecondStamp(long millisecondStamp) {
            this.millisecondStamp = millisecondStamp;
        }
 
        public String getTime() {
            return time;
        }
 
        public void setTime(String time) {
            this.time = time;
        }
 
    }
}

到此这篇关于Java常用时间工具类总结(珍藏版)的文章就介绍到这了,更多相关Java时间工具类内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章