|
@@ -5,9 +5,14 @@ import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
import java.time.*;
|
|
import java.time.*;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
+import java.time.format.DateTimeFormatterBuilder;
|
|
|
|
+import java.time.temporal.ChronoField;
|
|
import java.time.temporal.TemporalAdjusters;
|
|
import java.time.temporal.TemporalAdjusters;
|
|
import java.util.Calendar;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
+import java.util.Locale;
|
|
|
|
+import java.util.TimeZone;
|
|
|
|
+
|
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -15,8 +20,7 @@ import org.apache.commons.lang3.time.DateFormatUtils;
|
|
*
|
|
*
|
|
* @author ruoyi
|
|
* @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 = "yyyy";
|
|
|
|
|
|
public static String YYYY_MM = "yyyy-MM";
|
|
public static String YYYY_MM = "yyyy-MM";
|
|
@@ -39,8 +43,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
*
|
|
*
|
|
* @return Date() 当前日期
|
|
* @return Date() 当前日期
|
|
*/
|
|
*/
|
|
- public static Date getNowDate()
|
|
|
|
- {
|
|
|
|
|
|
+ public static Date getNowDate() {
|
|
return new Date();
|
|
return new Date();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -49,44 +52,34 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
*
|
|
*
|
|
* @return String
|
|
* @return String
|
|
*/
|
|
*/
|
|
- public static String getDate()
|
|
|
|
- {
|
|
|
|
|
|
+ public static String getDate() {
|
|
return dateTimeNow(YYYY_MM_DD);
|
|
return dateTimeNow(YYYY_MM_DD);
|
|
}
|
|
}
|
|
|
|
|
|
- public static final String getTime()
|
|
|
|
- {
|
|
|
|
|
|
+ public static final String getTime() {
|
|
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
|
|
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
|
|
}
|
|
}
|
|
|
|
|
|
- public static final String dateTimeNow()
|
|
|
|
- {
|
|
|
|
|
|
+ public static final String dateTimeNow() {
|
|
return dateTimeNow(YYYYMMDDHHMMSS);
|
|
return dateTimeNow(YYYYMMDDHHMMSS);
|
|
}
|
|
}
|
|
|
|
|
|
- public static final String dateTimeNow(final String format)
|
|
|
|
- {
|
|
|
|
|
|
+ public static final String dateTimeNow(final String format) {
|
|
return parseDateToStr(format, new Date());
|
|
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);
|
|
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);
|
|
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);
|
|
return new SimpleDateFormat(format).parse(ts);
|
|
- }
|
|
|
|
- catch (ParseException e)
|
|
|
|
- {
|
|
|
|
|
|
+ } catch (ParseException e) {
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -94,8 +87,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
/**
|
|
/**
|
|
* 日期路径 即年/月/日 如2018/08/08
|
|
* 日期路径 即年/月/日 如2018/08/08
|
|
*/
|
|
*/
|
|
- public static final String datePath()
|
|
|
|
- {
|
|
|
|
|
|
+ public static final String datePath() {
|
|
Date now = new Date();
|
|
Date now = new Date();
|
|
return DateFormatUtils.format(now, "yyyy/MM/dd");
|
|
return DateFormatUtils.format(now, "yyyy/MM/dd");
|
|
}
|
|
}
|
|
@@ -103,8 +95,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
/**
|
|
/**
|
|
* 日期路径 即年/月/日 如20180808
|
|
* 日期路径 即年/月/日 如20180808
|
|
*/
|
|
*/
|
|
- public static final String dateTime()
|
|
|
|
- {
|
|
|
|
|
|
+ public static final String dateTime() {
|
|
Date now = new Date();
|
|
Date now = new Date();
|
|
return DateFormatUtils.format(now, "yyyyMMdd");
|
|
return DateFormatUtils.format(now, "yyyyMMdd");
|
|
}
|
|
}
|
|
@@ -112,18 +103,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;
|
|
return null;
|
|
}
|
|
}
|
|
- try
|
|
|
|
- {
|
|
|
|
|
|
+ try {
|
|
return parseDate(str.toString(), parsePatterns);
|
|
return parseDate(str.toString(), parsePatterns);
|
|
- }
|
|
|
|
- catch (ParseException e)
|
|
|
|
- {
|
|
|
|
|
|
+ } catch (ParseException e) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -131,8 +117,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
/**
|
|
/**
|
|
* 获取服务器启动时间
|
|
* 获取服务器启动时间
|
|
*/
|
|
*/
|
|
- public static Date getServerStartDate()
|
|
|
|
- {
|
|
|
|
|
|
+ public static Date getServerStartDate() {
|
|
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
|
|
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
|
|
return new Date(time);
|
|
return new Date(time);
|
|
}
|
|
}
|
|
@@ -140,20 +125,18 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
/**
|
|
/**
|
|
* 计算相差天数
|
|
* 计算相差天数
|
|
*/
|
|
*/
|
|
- public static int differentDaysByMillisecond(Date date1, Date date2)
|
|
|
|
- {
|
|
|
|
|
|
+ public static int differentDaysByMillisecond(Date date1, Date date2) {
|
|
return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
|
|
return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 计算时间差
|
|
* 计算时间差
|
|
*
|
|
*
|
|
- * @param endDate 最后时间
|
|
|
|
|
|
+ * @param endDate 最后时间
|
|
* @param startTime 开始时间
|
|
* @param startTime 开始时间
|
|
* @return 时间差(天/小时/分钟)
|
|
* @return 时间差(天/小时/分钟)
|
|
*/
|
|
*/
|
|
- public static String timeDistance(Date endDate, Date startTime)
|
|
|
|
- {
|
|
|
|
|
|
+ public static String timeDistance(Date endDate, Date startTime) {
|
|
long nd = 1000 * 24 * 60 * 60;
|
|
long nd = 1000 * 24 * 60 * 60;
|
|
long nh = 1000 * 60 * 60;
|
|
long nh = 1000 * 60 * 60;
|
|
long nm = 1000 * 60;
|
|
long nm = 1000 * 60;
|
|
@@ -174,8 +157,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
/**
|
|
/**
|
|
* 增加 LocalDateTime ==> Date
|
|
* 增加 LocalDateTime ==> Date
|
|
*/
|
|
*/
|
|
- public static Date toDate(LocalDateTime temporalAccessor)
|
|
|
|
- {
|
|
|
|
|
|
+ public static Date toDate(LocalDateTime temporalAccessor) {
|
|
ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
|
|
ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
|
|
return Date.from(zdt.toInstant());
|
|
return Date.from(zdt.toInstant());
|
|
}
|
|
}
|
|
@@ -183,8 +165,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
/**
|
|
/**
|
|
* 增加 LocalDate ==> Date
|
|
* 增加 LocalDate ==> Date
|
|
*/
|
|
*/
|
|
- public static Date toDate(LocalDate temporalAccessor)
|
|
|
|
- {
|
|
|
|
|
|
+ public static Date toDate(LocalDate temporalAccessor) {
|
|
LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
|
|
LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
|
|
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
|
|
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
|
|
return Date.from(zdt.toInstant());
|
|
return Date.from(zdt.toInstant());
|
|
@@ -192,44 +173,47 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取指定日期的下周一
|
|
* 获取指定日期的下周一
|
|
|
|
+ *
|
|
* @param date
|
|
* @param date
|
|
* @param format
|
|
* @param format
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static String getNextMonday(String date,String format){
|
|
|
|
|
|
+ public static String getNextMonday(String date, String format) {
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
- LocalDate localDate = LocalDate.parse(date,formatter);
|
|
|
|
- DayOfWeek dayWeek = localDate.getDayOfWeek();
|
|
|
|
- int number = DayOfWeek.MONDAY.getValue()-dayWeek.getValue();
|
|
|
|
- number=number<=0?7+number:number;
|
|
|
|
|
|
+ LocalDate localDate = LocalDate.parse(date, formatter);
|
|
|
|
+ DayOfWeek dayWeek = localDate.getDayOfWeek();
|
|
|
|
+ int number = DayOfWeek.MONDAY.getValue() - dayWeek.getValue();
|
|
|
|
+ number = number <= 0 ? 7 + number : number;
|
|
LocalDate nextDay = localDate.plusDays(number);
|
|
LocalDate nextDay = localDate.plusDays(number);
|
|
return nextDay.format(formatter);
|
|
return nextDay.format(formatter);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取指定日期的下周日
|
|
* 获取指定日期的下周日
|
|
|
|
+ *
|
|
* @param date
|
|
* @param date
|
|
* @param format
|
|
* @param format
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static String getNextSunday(String date,String format){
|
|
|
|
|
|
+ public static String getNextSunday(String date, String format) {
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
- LocalDate localDate = LocalDate.parse(date,formatter);
|
|
|
|
- DayOfWeek dayWeek = localDate.getDayOfWeek();
|
|
|
|
- int number = DayOfWeek.SUNDAY.getValue()-dayWeek.getValue();
|
|
|
|
- number=number<=0?7+number:number;
|
|
|
|
|
|
+ LocalDate localDate = LocalDate.parse(date, formatter);
|
|
|
|
+ DayOfWeek dayWeek = localDate.getDayOfWeek();
|
|
|
|
+ int number = DayOfWeek.SUNDAY.getValue() - dayWeek.getValue();
|
|
|
|
+ number = number <= 0 ? 7 + number : number;
|
|
LocalDate nextDay = localDate.plusDays(number);
|
|
LocalDate nextDay = localDate.plusDays(number);
|
|
return nextDay.format(formatter);
|
|
return nextDay.format(formatter);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取指定日期前后N天的日期
|
|
* 获取指定日期前后N天的日期
|
|
|
|
+ *
|
|
* @param dateStr 日期字符串
|
|
* @param dateStr 日期字符串
|
|
- * @param format 格式
|
|
|
|
- * @param number 天数
|
|
|
|
|
|
+ * @param format 格式
|
|
|
|
+ * @param number 天数
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static String getNextDateStr(String dateStr,String format,int number){
|
|
|
|
|
|
+ public static String getNextDateStr(String dateStr, String format, int number) {
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
LocalDate localDate = LocalDate.parse(dateStr, formatter);
|
|
LocalDate localDate = LocalDate.parse(dateStr, formatter);
|
|
// 获取后N天的日期
|
|
// 获取后N天的日期
|
|
@@ -239,10 +223,11 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取上个月的第一天
|
|
* 获取上个月的第一天
|
|
|
|
+ *
|
|
* @param format
|
|
* @param format
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static String getFirstDateLastMonthStr(String format){
|
|
|
|
|
|
+ public static String getFirstDateLastMonthStr(String format) {
|
|
LocalDate now = LocalDate.now();
|
|
LocalDate now = LocalDate.now();
|
|
LocalDate firstDayOfLastMonth = now.with(TemporalAdjusters.firstDayOfMonth()).minusMonths(1);
|
|
LocalDate firstDayOfLastMonth = now.with(TemporalAdjusters.firstDayOfMonth()).minusMonths(1);
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
@@ -251,10 +236,11 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取上个月的最后一天
|
|
* 获取上个月的最后一天
|
|
|
|
+ *
|
|
* @param format
|
|
* @param format
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static String getLastDateLastMonthStr(String format){
|
|
|
|
|
|
+ public static String getLastDateLastMonthStr(String format) {
|
|
LocalDate now = LocalDate.now();
|
|
LocalDate now = LocalDate.now();
|
|
LocalDate previousMonth = now.minusMonths(1);
|
|
LocalDate previousMonth = now.minusMonths(1);
|
|
int lastDayOfPreviousMonth = previousMonth.lengthOfMonth();
|
|
int lastDayOfPreviousMonth = previousMonth.lengthOfMonth();
|
|
@@ -263,9 +249,24 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
return lastDateOfPreviousMonth.format(formatter);
|
|
return lastDateOfPreviousMonth.format(formatter);
|
|
}
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
|
|
|
+ public static String covertStrToLocalTime(String dateTimeStr) {
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z' (中国标准时间)'", Locale.ENGLISH);
|
|
|
|
+ SimpleDateFormat sdf2 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
|
|
|
|
+ try {
|
|
|
|
+ Date date=sdf.parse(dateTimeStr);
|
|
|
|
+ return sdf2.format(date);
|
|
|
|
+ //Date newStartDate =sdf2.parse(stDate);
|
|
|
|
+ //return new java.sql.Date(newStartDate.getTime());
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args)throws Exception {
|
|
//System.out.println(getNextMonday("20241125",YYYYMMDD));
|
|
//System.out.println(getNextMonday("20241125",YYYYMMDD));
|
|
- //System.out.println(getLastDateLastMonthStr("yyyyMMdd"));
|
|
|
|
|
|
+ System.out.println(covertStrToLocalTime("Fri Jan 17 2025 00:00:00 GMT+0800 (中国标准时间)"));
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|