|
@@ -14,11 +14,10 @@ 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";
|
|
@@ -29,35 +28,32 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
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",
|
|
|
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM",
|
|
|
"EEE MMM dd HH:mm:ss zzz yyyy"};
|
|
|
|
|
|
/**
|
|
|
* 获取当前Date型日期
|
|
|
- *
|
|
|
+ *
|
|
|
* @return Date() 当前日期
|
|
|
*/
|
|
|
- public static Date getNowDate()
|
|
|
- {
|
|
|
+ public static Date getNowDate() {
|
|
|
return new Date();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取当前日期, 默认格式为yyyy-MM-dd
|
|
|
- *
|
|
|
+ *
|
|
|
* @return String
|
|
|
*/
|
|
|
- public static String getDate()
|
|
|
- {
|
|
|
+ public static String getDate() {
|
|
|
return dateTimeNow(YYYY_MM_DD);
|
|
|
}
|
|
|
|
|
|
- public static String getDateNow()
|
|
|
- {
|
|
|
+ public static String getDateNow() {
|
|
|
return dateTimeNow(YYYYMMDD);
|
|
|
}
|
|
|
|
|
@@ -66,44 +62,34 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
*
|
|
|
* @return String
|
|
|
*/
|
|
|
- public static String getYear()
|
|
|
- {
|
|
|
+ public static String getYear() {
|
|
|
return dateTimeNow(YYYY);
|
|
|
}
|
|
|
|
|
|
- public static final String getTime()
|
|
|
- {
|
|
|
+ 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);
|
|
|
}
|
|
|
}
|
|
@@ -111,8 +97,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");
|
|
|
}
|
|
@@ -120,8 +105,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");
|
|
|
}
|
|
@@ -129,18 +113,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;
|
|
|
}
|
|
|
}
|
|
@@ -149,6 +128,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
|
|
|
/**
|
|
|
* "EEE MMM dd HH:mm:ss zzz yyyy", Locale.US
|
|
|
+ *
|
|
|
* @param str
|
|
|
* @return
|
|
|
*/
|
|
@@ -163,12 +143,11 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 获取服务器启动时间
|
|
|
*/
|
|
|
- public static Date getServerStartDate()
|
|
|
- {
|
|
|
+ public static Date getServerStartDate() {
|
|
|
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
|
|
|
return new Date(time);
|
|
|
}
|
|
@@ -176,8 +155,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
/**
|
|
|
* 计算两个时间差
|
|
|
*/
|
|
|
- public static String getDatePoor(Date endDate, Date nowDate)
|
|
|
- {
|
|
|
+ public static String getDatePoor(Date endDate, Date nowDate) {
|
|
|
long nd = 1000 * 24 * 60 * 60;
|
|
|
long nh = 1000 * 60 * 60;
|
|
|
long nm = 1000 * 60;
|
|
@@ -197,6 +175,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
|
|
|
/**
|
|
|
* LocalDate转Date
|
|
|
+ *
|
|
|
* @param localDate
|
|
|
* @return
|
|
|
*/
|
|
@@ -210,12 +189,22 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
|
|
|
/**
|
|
|
* Date转LocalDate
|
|
|
+ *
|
|
|
* @param date
|
|
|
*/
|
|
|
public static LocalDate date2LocalDate(Date date) {
|
|
|
- if(null == date) {
|
|
|
+ if (null == date) {
|
|
|
return null;
|
|
|
}
|
|
|
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取时间戳
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getUnix() {
|
|
|
+ return Long.toString(System.currentTimeMillis() / 1000L);
|
|
|
+ }
|
|
|
}
|