Java常用時(shí)間工具類(lèi)總結(jié)(珍藏版)
肝了兩天,重新整理了下時(shí)間工具類(lèi),以后我就以該時(shí)間工具類(lèi)進(jìn)行項(xiàng)目開(kāi)發(fā)了,后會(huì)不定期更新功能,也歡迎留言需求,讓工具類(lèi)不斷的完善。
常量介紹

相關(guān)方法

工具類(lèi)源碼
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;
/**
* 創(chuàng)作者鏈接:https://blog.csdn.net/sunnyzyq/article/details/125377621
* @author zyq
* @since 2022/6/17
*/
public class DateUtils {
/** 一星期的天數(shù) */
public static final int WEEK_DAYS = 7;
/** 一年的月份數(shù) */
public static final int YEAR_MONTHS = 12;
/** 一天的小時(shí)數(shù) */
public static final int DAY_HOURS = 24;
/** 一小時(shí)分鐘數(shù) */
public static final int HOUR_MINUTES = 60;
/** 一天分鐘數(shù) (24 * 60) */
public static final int DAY_MINUTES = 1440;
/** 一分鐘的秒數(shù) */
public static final int MINUTE_SECONDS = 60;
/** 一個(gè)小時(shí)的秒數(shù) (60 * 60) */
public static final int HOUR_SECONDS = 3600;
/** 一天的秒數(shù) (24 * 60 * 60) */
public static final int DAY_SECONDS = 86400;
/** 一秒的毫秒數(shù) */
public static final long SECOND_MILLISECONDS = 1000L;
/** 一分鐘的毫秒數(shù)(60 * 1000) */
public static final long MINUTE_MILLISECONDS = 60000L;
/** 一小時(shí)的毫秒數(shù)(60 * 60 * 1000) */
public static final long HOUR_MILLISECONDS = 3600000L;
/** 一天的毫秒數(shù)(24 * 60* 60* 1000) */
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";
/** 顯示到小時(shí) */
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";
/** 顯示到日期(數(shù)字格式) */
public static final String FORMAT_NO_DATE = "yyyyMMdd";
/** 顯示到小時(shí)(數(shù)字格式) */
public static final String FORMAT_NO_HOUR = "yyyyMMddHH";
/** 顯示到分(數(shù)字格式) */
public static final String FORMAT_NO_MINUTE = "yyyyMMddHHmm";
/** 顯示到秒(數(shù)字格式) */
public static final String FORMAT_NO_SECOND = "yyyyMMddHHmmss";
/** 顯示到毫秒(數(shù)字格式) */
public static final String FORMAT_NO_MILLISECOND = "yyyyMMddHHmmssSSS";
/** 時(shí)間格式化器集合 */
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));
}
/**
* 獲取指定時(shí)間格式化器
*
* @param formatStyle 時(shí)間格式
* @return 時(shí)間格式化器
*/
private static SimpleDateFormat getSimpleDateFormat(String formatStyle) {
SimpleDateFormat dateFormat = simpleDateFormatMap.get(formatStyle);
if (Objects.nonNull(dateFormat)) {
return dateFormat;
}
return new SimpleDateFormat(formatStyle);
}
/**
* 將 Date 格式時(shí)間轉(zhuǎn)化為指定格式時(shí)間
*
* @param date Date 格式時(shí)間
* @param formatStyle 轉(zhuǎn)化指定格式(如: yyyy-MM-dd HH:mm:ss)
* @return 轉(zhuǎn)化格式時(shí)間
*/
public static String format(Date date, String formatStyle) {
if (Objects.isNull(date)) {
return "";
}
return getSimpleDateFormat(formatStyle).format(date);
}
/**
* 將 Date 格式時(shí)間轉(zhuǎn)化為 yyyy-MM-dd 格式時(shí)間
*
* @param date Date 格式時(shí)間
* @return yyyy-MM-dd 格式時(shí)間(如:2022-06-17)
*/
public static String formatDate(Date date) {
return format(date, FORMAT_DATE);
}
/**
* 將 Date 格式時(shí)間轉(zhuǎn)化為 yyyy-MM-dd HH:mm:ss 格式時(shí)間
*
* @param date Date 格式時(shí)間
* @return yyyy-MM-dd HH:mm:ss 格式時(shí)間(如:2022-06-17 16:06:17)
*/
public static String formatDateTime(Date date) {
return format(date, FORMAT_SECOND);
}
/**
* 將 Date 格式時(shí)間轉(zhuǎn)化為 yyyy-MM-dd HH:mm:ss:SSS 格式時(shí)間
*
* @param date Date 格式時(shí)間
* @return yyyy-MM-dd HH:mm:ss:SSS 格式時(shí)間(如:2022-06-17 16:06:17:325)
*/
public static String formatDateTimeStamp(Date date) {
return format(date, FORMAT_MILLISECOND);
}
/**
* 將 yyyy-MM-dd 格式時(shí)間轉(zhuǎn)化為 Date 格式時(shí)間
*
* @param dateString yyyy-MM-dd 格式時(shí)間(如:2022-06-17)
* @return Date 格式時(shí)間
*/
public static Date parseDate(String dateString) {
return parse(dateString, FORMAT_DATE);
}
/**
* 將 yyyy-MM-dd HH:mm:ss 格式時(shí)間轉(zhuǎn)化為 Date 格式時(shí)間
*
* @param dateTimeStr yyyy-MM-dd HH:mm:ss 格式時(shí)間(如:2022-06-17 16:06:17)
* @return Date 格式時(shí)間
*/
public static Date parseDateTime(String dateTimeStr) {
return parse(dateTimeStr, FORMAT_SECOND);
}
/**
* 將 yyyy-MM-dd HH:mm:ss:SSS 格式時(shí)間轉(zhuǎn)化為 Date 格式時(shí)間
*
* @param dateTimeStr yyyy-MM-dd HH:mm:ss:SSS 格式時(shí)間(如:2022-06-17 16:06:17)
* @return Date 格式時(shí)間
*/
public static Date parseDateTimeStamp(String dateTimeStampStr) {
return parse(dateTimeStampStr, FORMAT_MILLISECOND);
}
/**
* 將字符串格式時(shí)間轉(zhuǎn)化為 Date 格式時(shí)間
*
* @param dateString 字符串時(shí)間(如:2022-06-17 16:06:17)
* @return formatStyle 格式內(nèi)容
* @return Date 格式時(shí)間
*/
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;
}
}
/**
* 獲取字符串有效內(nèi)容
*
* @param s 字符串
* @return 有效內(nèi)容
*/
private static String getString(String s) {
return Objects.isNull(s) ? "" : s.trim();
}
/**
* 獲取一天的開(kāi)始時(shí)間(即:0 點(diǎn) 0 分 0 秒 0 毫秒)
*
* @param date 指定時(shí)間
* @return 當(dāng)天的開(kāi)始時(shí)間
*/
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();
}
/**
* 獲取一天的截止時(shí)間(即:23 點(diǎn) 59 分 59 秒 999 毫秒)
*
* @param date 指定時(shí)間
* @return 當(dāng)天的開(kāi)始時(shí)間
*/
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();
}
/**
* 獲取日期數(shù)字
*
* @param date 日期
* @return 日期數(shù)字
*/
public static int getDateNo(Date date) {
if (Objects.isNull(date)) {
return 0;
}
return Integer.valueOf(format(date, FORMAT_NO_DATE));
}
/**
* 獲取日期時(shí)間數(shù)字(到秒)
*
* @param date 日期
* @return 日期數(shù)字
*/
public static long getDateTimeNo(Date date) {
if (Objects.isNull(date)) {
return 0L;
}
return Long.valueOf(format(date, FORMAT_NO_SECOND));
}
/**
* 獲取日期時(shí)間數(shù)字(到毫秒)
*
* @param date 日期
* @return 日期數(shù)字
*/
public static long getDateTimeStampNo(Date date) {
if (Objects.isNull(date)) {
return 0L;
}
return Long.valueOf(format(date, FORMAT_NO_MILLISECOND));
}
/**
* 獲取星期幾
*
* @param date 時(shí)間
* @return 0(時(shí)間為空), 1(周一), 2(周二),3(周三),4(周四),5(周五),6(周六),7(周日)
*/
public static int getWeek(Date date) {
if (Objects.isNull(date)) {
return 0;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return getWeek(calendar);
}
/**
* 獲取星期幾
*
* @param date 時(shí)間
* @return 0(時(shí)間為空), 1(周一), 2(周二),3(周三),4(周四),5(周五),6(周六),7(周日)
*/
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;
}
}
/**
* 獲取該日期是今年的第幾周(以本年的周一為第1周,詳見(jiàn)下面說(shuō)明)<br>
*
* 【說(shuō)明】<br>
* 比如 2022-01-01(周六)和 2022-01-02(周日)雖然在 2022 年里,但他們兩天則屬于 2021 年最后一周,<br>
* 那么這兩天不會(huì)算在 2022 年第 1 周里,此時(shí)會(huì)返回 0 ;而 2022 年第 1 周將從 2022-01-03(周一) 開(kāi)始計(jì)算。<br>
*
* @param date 時(shí)間
* @return -1(時(shí)間為空), 0(為上個(gè)年的最后一周),其他數(shù)字(今年的第幾周)
*/
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;
}
/**
* 獲取今年的第幾周(以本年的1月1日為第1周第1天)<br>
*
* @param date 時(shí)間
* @return -1(時(shí)間為空),其他數(shù)字(今年的第幾周)
*/
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 的倍數(shù),則表示恰好是多少周
if (days % 7 == 0) {
return weeks;
}
// 如果有余數(shù),則需要再加 1
return weeks + 1;
}
/**
* 獲取時(shí)間節(jié)點(diǎn)對(duì)象
*
* @param date 時(shí)間對(duì)象
* @return DateNode
*/
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;
}
/**
* 日期變更
*
* @param date 指定日期
* @param field 變更屬性(如變更年份,則該值為 Calendar.DAY_OF_YEAR)
* @param amount 變更大?。ù笥?0 時(shí)增加,小于 0 時(shí)減少)
* @return 變更后的日期時(shí)間
*/
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();
}
/**
* 指定日期加減年份
*
* @param date 指定日期
* @param year 變更年份(大于 0 時(shí)增加,小于 0 時(shí)減少)
* @return 變更年份后的日期
*/
public static Date addYear(Date date, int year) {
return add(date, Calendar.YEAR, year);
}
/**
* 指定日期加減月份
*
* @param date 指定日期
* @param month 變更月份(大于 0 時(shí)增加,小于 0 時(shí)減少)
* @return 變更月份后的日期
*/
public static Date addMonth(Date date, int month) {
return add(date, Calendar.MONTH, month);
}
/**
* 指定日期加減天數(shù)
*
* @param date 指定日期
* @param day 變更天數(shù)(大于 0 時(shí)增加,小于 0 時(shí)減少)
* @return 變更天數(shù)后的日期
*/
public static Date addDay(Date date, int day) {
return add(date, Calendar.DAY_OF_YEAR, day);
}
/**
* 指定日期加減星期
*
* @param date 指定日期
* @param week 變更星期數(shù)(大于 0 時(shí)增加,小于 0 時(shí)減少)
* @return 變更星期數(shù)后的日期
*/
public static Date addWeek(Date date, int week) {
return add(date, Calendar.WEEK_OF_YEAR, week);
}
/**
* 指定日期加減小時(shí)
*
* @param date 指定日期時(shí)間
* @param hour 變更小時(shí)數(shù)(大于 0 時(shí)增加,小于 0 時(shí)減少)
* @return 變更小時(shí)數(shù)后的日期時(shí)間
*/
public static Date addHour(Date date, int hour) {
return add(date, Calendar.HOUR_OF_DAY, hour);
}
/**
* 指定日期加減分鐘
*
* @param date 指定日期時(shí)間
* @param minute 變更分鐘數(shù)(大于 0 時(shí)增加,小于 0 時(shí)減少)
* @return 變更分鐘數(shù)后的日期時(shí)間
*/
public static Date addMinute(Date date, int minute) {
return add(date, Calendar.MINUTE, minute);
}
/**
* 指定日期加減秒
*
* @param date 指定日期時(shí)間
* @param second 變更秒數(shù)(大于 0 時(shí)增加,小于 0 時(shí)減少)
* @return 變更秒數(shù)后的日期時(shí)間
*/
public static Date addSecond(Date date, int second) {
return add(date, Calendar.SECOND, second);
}
/**
* 指定日期加減秒
*
* @param date 指定日期時(shí)間
* @param minute 變更毫秒數(shù)(大于 0 時(shí)增加,小于 0 時(shí)減少)
* @return 變更毫秒數(shù)后的日期時(shí)間
*/
public static Date addMillisecond(Date date, int millisecond) {
return add(date, Calendar.MILLISECOND, millisecond);
}
/**
* 獲取該日期所在周指定星期的日期
*
* @param date 日期所在時(shí)間
* @return index 指定星期(1 - 7 分別對(duì)應(yīng)星期一到星期天)
*/
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);
}
/**
* 獲取該日期所在周開(kāi)始日期
*
* @param date 日期所在時(shí)間
* @return 所在周開(kāi)始日期
*/
public static Date getWeekDateStart(Date date) {
return getDateStart(getWeekDate(date, WEEK_1_MONDAY));
}
/**
* 獲取該日期所在周開(kāi)始日期
*
* @param date 日期所在時(shí)間
* @return 所在周開(kāi)始日期
*/
public static Date getWeekDateEnd(Date date) {
return getWeekDateEnd(getWeekDate(date, WEEK_7_SUNDAY));
}
/**
* 獲取該日期所在周的所有日期(周一到周日)
*
* @param Date 日期
* @return 該日照所在周的所有日期
*/
public static List<Date> getWeekDateList(Date date) {
if (Objects.isNull(date)) {
return Collections.emptyList();
}
// 獲取本周開(kāi)始時(shí)間
Date weekFromDate = getWeekDateStart(date);
// 獲取本周截止時(shí)間
Date weekeEndDate = getWeekDateEnd(date);
return getBetweenDateList(weekFromDate, weekeEndDate, true);
}
/**
* 獲取該日期所在周的所有日期(周一到周日)
*
* @param dateString
* @return 該日照所在周的所有日期
*/
public static List<String> getWeekDateList(String dateString) {
Date date = parseDate(dateString);
if (Objects.isNull(date)) {
return Collections.emptyList();
}
return getDateStrList(getWeekDateList(date));
}
/**
* 獲取該日期所在月的所有日期
*
* @param dateString
* @return 該日照所月的所有日期
*/
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);
}
/**
* 獲取該日期所在月的所有日期
*
* @param dateString
* @return 該日照所月的所有日期
*/
public static List<String> getMonthDateList(String dateString) {
Date date = parseDate(dateString);
if (Objects.isNull(date)) {
return Collections.emptyList();
}
return getDateStrList(getMonthDateList(date));
}
/**
* 獲取本日期所在月第一天
*
* @param date 日期
* @return 本日期所在月第一天
*/
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());
}
/**
* 獲取本日期所在月最后一天
*
* @param date 日期
* @return 本日期所在月最后一天
*/
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));
}
/**
* 獲取兩個(gè)日期相差的天數(shù)(以日期為單位計(jì)算,不以24小時(shí)制計(jì)算,詳見(jiàn)下面說(shuō)明)<br>
*
* 【說(shuō)明】比如 2022-06-17 23:00:00 和 2022-06-17 01:00:00,兩者雖然只相差 2 個(gè)小時(shí),但也算相差 1 天 <br>
*
* @param date1 日期1
* @param date2 日期2
* @return 相差天數(shù)(若返回 -1,則至少有一個(gè)日期存在為空,此時(shí)不能進(jìn)行比較)
*/
public static int countBetweenDays(Date date1, Date date2) {
if (Objects.isNull(date1) || Objects.isNull(date2)) {
return -1;
}
// 獲取兩個(gè)日期 0 點(diǎn) 0 時(shí) 0 分 0 秒 0 毫秒時(shí)的時(shí)間戳(毫秒級(jí))
long t1 = getDateStart(date1).getTime();
long t2 = getDateStart(date2).getTime();
// 相差天數(shù) = 相差的毫秒數(shù) / 一天的毫秒數(shù)
return (int) (Math.abs(t1 - t2) / DAY_MILLISECONDS);
}
/**
* 獲取兩個(gè)日期之間的所有日期
*
* @param date1 日期1
* @param date2 日期2
* @return 兩個(gè)日期之間的所有日期的開(kāi)始時(shí)間
*/
public static List<Date> getBetweenDateList(Date date1, Date date2) {
return getBetweenDateList(date1, date2, false);
}
/**
* 獲取兩個(gè)日期之間的所有日期
*
* @param date1 日期1
* @param date2 日期2
* @return 兩個(gè)日期之間的所有日期的開(kāi)始時(shí)間
*/
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;
}
// 獲取兩個(gè)日期每天的開(kāi)始時(shí)間
Date from = getDateStart(fromDate);
Date to = getDateStart(toDate);
// 獲取日期,開(kāi)始循環(huán)
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;
}
/**
* 獲取兩個(gè)日期之間的所有日期
*
* @param dateString1 日期1(如:2022-06-20)
* @param dateString2 日期2(如:2022-07-15)
* @return 兩個(gè)日期之間的所有日期(不包含參數(shù)日期)
*/
public static List<String> getBetweenDateList(String dateString1, String dateString2) {
return getBetweenDateList(dateString1, dateString2, false);
}
/**
* 獲取兩個(gè)日期之間的所有日期
*
* @param dateString1 日期1(如:2022-06-20)
* @param dateString2 日期2(如:2022-07-15)
* @param isContainParams 是否包含參數(shù)的兩個(gè)日期
* @return 兩個(gè)日期之間的所有日期的開(kāi)始時(shí)間
*/
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);
}
/**
* List<Date> 轉(zhuǎn) List<String>
*
* @param dates 日期集合
* @return 日期字符串集合
*/
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;
/** 時(shí) */
private int hour;
/** 分 */
private int minute;
/** 秒 */
private int second;
/** 毫秒 */
private int millisecond;
/** 星期幾( 1 - 7 對(duì)應(yīng)周一到周日) */
private int week;
/** 當(dāng)年第幾天 */
private int dayOfYear;
/** 當(dāng)年第幾周(本年周 1 為第 1 周,0 則表示屬于去年最后一周) */
private int weekOfYear;
/** 當(dāng)年第幾周(本年周 1 為第 1 周,0 則表示屬于去年最后一周) */
private int weekOfYearIgnoreLastYear;
/** 時(shí)間戳(秒級(jí)) */
private long secondStamp;
/** 時(shí)間戳(毫秒級(jí)) */
private long millisecondStamp;
/** 顯示時(shí)間 */
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;
}
}
}到此這篇關(guān)于Java常用時(shí)間工具類(lèi)總結(jié)(珍藏版)的文章就介紹到這了,更多相關(guān)Java時(shí)間工具類(lèi)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot中shiro使用自定義注解屏蔽接口鑒權(quán)實(shí)現(xiàn)
本文主要介紹了springboot中shiro使用自定義注解屏蔽接口鑒權(quán)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Mybatis動(dòng)態(tài)Sql標(biāo)簽使用小結(jié)
本文主要介紹了Mybatis動(dòng)態(tài)Sql標(biāo)簽使用,常用的動(dòng)態(tài)sql標(biāo)簽包括?if、choose(when、otherwise)、trim(where、set)、foreach,下面就來(lái)介紹一下2024-04-04
MyBatis @Param注解的實(shí)現(xiàn)
本文主要介紹了MyBatis @Param注解的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
SpringBoot使用CommandLineRunner和ApplicationRunner執(zhí)行初始化業(yè)務(wù)方式
這篇文章主要介紹了SpringBoot使用CommandLineRunner和ApplicationRunner執(zhí)行初始化業(yè)務(wù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Java 使用keytool創(chuàng)建CA證書(shū)的操作
這篇文章主要介紹了Java 使用keytool創(chuàng)建CA證書(shū)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
SpringBoot整合Canal+RabbitMQ監(jiān)聽(tīng)數(shù)據(jù)變更詳解
在現(xiàn)代分布式系統(tǒng)中,實(shí)時(shí)獲取數(shù)據(jù)庫(kù)的變更信息是一個(gè)常見(jiàn)的需求,本文將介紹SpringBoot如何通過(guò)整合Canal和RabbitMQ監(jiān)聽(tīng)數(shù)據(jù)變更,需要的可以參考下2024-12-12
Spring Security 自定義短信登錄認(rèn)證的實(shí)現(xiàn)
這篇文章主要介紹了Spring Security 自定義短信登錄認(rèn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Java中樹(shù)的存儲(chǔ)結(jié)構(gòu)實(shí)現(xiàn)示例代碼
本篇文章主要介紹了Java中樹(shù)的存儲(chǔ)結(jié)構(gòu)實(shí)現(xiàn)示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09

