|
@@ -9,6 +9,7 @@ import java.time.LocalTime;
|
|
|
import java.time.ZoneId;
|
|
|
import java.time.ZonedDateTime;
|
|
|
import java.util.Date;
|
|
|
+
|
|
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
|
|
|
/**
|
|
@@ -16,8 +17,7 @@ import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
*
|
|
|
* @author ruoyi
|
|
|
*/
|
|
|
-public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
-{
|
|
|
+public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
|
|
public static String YYYY = "yyyy";
|
|
|
|
|
|
public static String YYYY_MM = "yyyy-MM";
|
|
@@ -38,8 +38,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
*
|
|
|
* @return Date() 当前日期
|
|
|
*/
|
|
|
- public static Date getNowDate()
|
|
|
- {
|
|
|
+ public static Date getNowDate() {
|
|
|
return new Date();
|
|
|
}
|
|
|
|
|
@@ -48,44 +47,44 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
*
|
|
|
* @return String
|
|
|
*/
|
|
|
- public static String getDate()
|
|
|
- {
|
|
|
+ public static String getDate() {
|
|
|
return dateTimeNow(YYYY_MM_DD);
|
|
|
}
|
|
|
|
|
|
- public static final String getTime()
|
|
|
- {
|
|
|
+ /**
|
|
|
+ * 获取当前年份,分表时使用
|
|
|
+ *
|
|
|
+ * @return 年份
|
|
|
+ */
|
|
|
+ public static Long getCurrentYear() {
|
|
|
+ Integer currentYear = LocalDate.now().getYear();
|
|
|
+ return (long) currentYear;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String getTime() {
|
|
|
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
|
|
|
}
|
|
|
|
|
|
- public static final String dateTimeNow()
|
|
|
- {
|
|
|
+ public static final String dateTimeNow() {
|
|
|
return dateTimeNow(YYYYMMDDHHMMSS);
|
|
|
}
|
|
|
|
|
|
- public static final String dateTimeNow(final String format)
|
|
|
- {
|
|
|
+ public static final String dateTimeNow(final String format) {
|
|
|
return parseDateToStr(format, new Date());
|
|
|
}
|
|
|
|
|
|
- public static final String dateTime(final Date 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)
|
|
|
- {
|
|
|
+ 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
|
|
|
- {
|
|
|
+ public static final Date dateTime(final String format, final String ts) {
|
|
|
+ try {
|
|
|
return new SimpleDateFormat(format).parse(ts);
|
|
|
- }
|
|
|
- catch (ParseException e)
|
|
|
- {
|
|
|
+ } catch (ParseException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
}
|
|
@@ -93,8 +92,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
/**
|
|
|
* 日期路径 即年/月/日 如2018/08/08
|
|
|
*/
|
|
|
- public static final String datePath()
|
|
|
- {
|
|
|
+ public static final String datePath() {
|
|
|
Date now = new Date();
|
|
|
return DateFormatUtils.format(now, "yyyy/MM/dd");
|
|
|
}
|
|
@@ -102,8 +100,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
/**
|
|
|
* 日期路径 即年/月/日 如20180808
|
|
|
*/
|
|
|
- public static final String dateTime()
|
|
|
- {
|
|
|
+ public static final String dateTime() {
|
|
|
Date now = new Date();
|
|
|
return DateFormatUtils.format(now, "yyyyMMdd");
|
|
|
}
|
|
@@ -111,18 +108,13 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
/**
|
|
|
* 日期型字符串转化为日期 格式
|
|
|
*/
|
|
|
- public static Date parseDate(Object str)
|
|
|
- {
|
|
|
- if (str == null)
|
|
|
- {
|
|
|
+ public static Date parseDate(Object str) {
|
|
|
+ if (str == null) {
|
|
|
return null;
|
|
|
}
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
return parseDate(str.toString(), parsePatterns);
|
|
|
- }
|
|
|
- catch (ParseException e)
|
|
|
- {
|
|
|
+ } catch (ParseException e) {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
@@ -130,8 +122,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
/**
|
|
|
* 获取服务器启动时间
|
|
|
*/
|
|
|
- public static Date getServerStartDate()
|
|
|
- {
|
|
|
+ public static Date getServerStartDate() {
|
|
|
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
|
|
|
return new Date(time);
|
|
|
}
|
|
@@ -139,18 +130,17 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
/**
|
|
|
* 计算时间差
|
|
|
*
|
|
|
- * @param endTime 最后时间
|
|
|
+ * @param endTime 最后时间
|
|
|
* @param startTime 开始时间
|
|
|
* @return 时间差(天/小时/分钟)
|
|
|
*/
|
|
|
- public static String timeDistance(Date endDate, Date startTime)
|
|
|
- {
|
|
|
+ public static String timeDistance(Date endTime, Date startTime) {
|
|
|
long nd = 1000 * 24 * 60 * 60;
|
|
|
long nh = 1000 * 60 * 60;
|
|
|
long nm = 1000 * 60;
|
|
|
// long ns = 1000;
|
|
|
// 获得两个时间的毫秒时间差异
|
|
|
- long diff = endDate.getTime() - startTime.getTime();
|
|
|
+ long diff = endTime.getTime() - startTime.getTime();
|
|
|
// 计算差多少天
|
|
|
long day = diff / nd;
|
|
|
// 计算差多少小时
|
|
@@ -165,8 +155,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
/**
|
|
|
* 增加 LocalDateTime ==> Date
|
|
|
*/
|
|
|
- public static Date toDate(LocalDateTime temporalAccessor)
|
|
|
- {
|
|
|
+ public static Date toDate(LocalDateTime temporalAccessor) {
|
|
|
ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
|
|
|
return Date.from(zdt.toInstant());
|
|
|
}
|
|
@@ -174,8 +163,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
/**
|
|
|
* 增加 LocalDate ==> Date
|
|
|
*/
|
|
|
- public static Date toDate(LocalDate temporalAccessor)
|
|
|
- {
|
|
|
+ public static Date toDate(LocalDate temporalAccessor) {
|
|
|
LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
|
|
|
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
|
|
|
return Date.from(zdt.toInstant());
|