一、来源
Apache Commons-Lang3
二、SpringBoot集成
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
三、常用方法总结
1、parseDate(String str, String… parsePatterns)
将字符串解析为Date对象,根据指定的格式解析日期字符串。
String dateString = "2022-05-01";
Date date = DateUtils.parseDate(dateString, "yyyy-MM-dd");
System.out.println(date); // Output: Mon May 01 00:00:00 GMT 2022
2、formatDate(Date date, String pattern)
将Date对象格式化为指定格式的日期字符串。
Date date = new Date();
String formattedDate = DateUtils.formatDate(date, "yyyy/MM/dd");
System.out.println(formattedDate); // Output: 2022/09/15
3、isSameDay(Date date1, Date date2)
检查两个日期是否是同一天。
Date date1 = new Date();
Date date2 = new Date();
boolean sameDay = DateUtils.isSameDay(date1, date2);
System.out.println(sameDay); // Output: true
4、addYears(Date date, int amount)
给指定的日期增加指定的年份。
Date date = new Date();
Date newDate = DateUtils.addYears(date, 5);
System.out.println(newDate); // Output: Sat Sep 15 13:55:27 GMT 2027
5、addMonths(Date date, int amount)
给指定的日期增加指定的月份。
Date date = new Date();
Date newDate = DateUtils.addMonths(date, 3);
System.out.println(newDate); // Output: Sat Dec 15 13:55:27 GMT 2022
6、addDays(Date date, int amount)
给指定的日期增加指定的天数。
Date date = new Date();
Date newDate = DateUtils.addDays(date, 7);
System.out.println(newDate); // Output: Thu Sep 22 13:55:27 GMT 2022
7、addHours(Date date, int amount)
给指定的日期增加指定的小时数。
Date date = new Date();
Date newDate = DateUtils.addHours(date, 2);
System.out.println(newDate); // Output: Thu Sep 15 15:55:27 GMT 2022
8、addMinutes(Date date, int amount)
给指定的日期增加指定的分钟数。
Date date = new Date();
Date newDate = DateUtils.addMinutes(date, 30);
System.out.println(newDate); // Output: Thu Sep 15 14:25:27 GMT 2022
9、addSeconds(Date date, int amount)
给指定的日期增加指定的秒数。
Date date = new Date();
Date newDate = DateUtils.addSeconds(date, 45);
System.out.println(newDate); // Output: Thu Sep 15 13:56:12 GMT 2022
10、truncate(Date date, int field)
将指定日期的时间部分截断到指定的字段。
Date date = new Date();
Date truncatedDate = DateUtils.truncate(date, Calendar.HOUR_OF_DAY);
System.out.println(truncatedDate); // Output: Thu Sep 15 13:00:00 GMT 2022
11、round(Date date, int field)
将指定日期的时间部分四舍五入到指定的字段。
Date date = new Date();
Date roundedDate = DateUtils.round(date, Calendar.MINUTE);
System.out.println(roundedDate); // Output: Thu Sep 15 13:56:00 GMT 2022
12、ceiling(Date date, int field)
将指定日期的时间部分向上舍入到指定的字段。
Date date = new Date();
Date ceiledDate = DateUtils.ceiling(date, Calendar.DAY_OF_MONTH);
System.out.println(ceiledDate); // Output: Fri Sep 16 00:00:00 GMT 2022
13、getFragmentInMilliseconds(Date date, int fragment)
获取指定日期的指定字段的值,以毫秒为单位。
Date date = new Date();
int milliseconds = DateUtils.getFragmentInMilliseconds(date, Calendar.MILLISECOND);
System.out.println(milliseconds); // Output: 0
14、getFragmentInSeconds(Date date, int fragment)
获取指定日期的指定字段的值,以秒为单位。
Date date = new Date();
int seconds = DateUtils.getFragmentInSeconds(date, Calendar.SECOND);
System.out.println(seconds); // Output: 27
15、getFragmentInMinutes(Date date, int fragment)
获取指定日期的指定字段的值,以分钟为单位。
Date date = new Date();
int minutes = DateUtils.getFragmentInMinutes(date, Calendar.MINUTE);
System.out.println(minutes); // Output: 55
16、getFragmentInHours(Date date, int fragment)
获取指定日期的指定字段的值,以小时为单位。
Date date = new Date();
int hours = DateUtils.getFragmentInHours(date, Calendar.HOUR_OF_DAY);
System.out.println(hours); // Output: 13
17、getFragmentInDays(Date date, int fragment)
获取指定日期的指定字段的值,以天为单位。
Date date = new Date();
int days = DateUtils.getFragmentInDays(date, Calendar.DAY_OF_MONTH);
System.out.println(days); // Output: 15
四、扩展
直接上扩展类。
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
public static String YYYY = "yyyy";
public static String YYYY_MM = "yyyy-MM";
public static String YYYY_MM_DD = "yyyy-MM-dd";
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private static String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
/**
* 获取当前Date型日期
*
* @return Date() 当前日期
*/
public static Date getNowDate() {
return new Date();
}
/**
* 获取当前日期, 默认格式为yyyy-MM-dd
*
* @return String
*/
public static String getDate() {
return dateTimeNow(YYYY_MM_DD);
}
public static final String getTime() {
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
}
public static final String dateTimeNow() {
return dateTimeNow(YYYYMMDDHHMMSS);
}
public static final String dateTimeNow(final String format) {
return parseDateToStr(format, new Date());
}
public static final String dateTime(final Date date) {
return parseDateToStr(YYYY_MM_DD, date);
}
public static final String parseDateToStr(final String format, final Date date) {
return new SimpleDateFormat(format).format(date);
}
public static final Date dateTime(final String format, final String ts) {
try {
return new SimpleDateFormat(format).parse(ts);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* 日期路径 即年/月/日 如2018/08/08
*/
public static final String datePath() {
Date now = new Date();
return DateFormatUtils.format(now, "yyyy/MM/dd");
}
/**
* 日期路径 即年/月/日 如20180808
*/
public static final String dateTime() {
Date now = new Date();
return DateFormatUtils.format(now, "yyyyMMdd");
}
/**
* 日期型字符串转化为日期 格式
*/
public static Date parseDate(Object str) {
if (str == null) {
return null;
}
try {
return parseDate(str.toString(), parsePatterns);
} catch (ParseException e) {
return null;
}
}
/**
* 获取服务器启动时间
*/
public static Date getServerStartDate() {
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
return new Date(time);
}
/**
* 计算两个时间差
*/
public static String getDatePoor(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟";
}
}
扩展类参考(若依开源项目 - Gitee.com)