|
|
|
@ -20,8 +20,10 @@ import cn.escheduler.common.Constants;
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
import java.text.ParseException; |
|
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
|
import java.time.Instant; |
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
import java.time.ZoneId; |
|
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
|
import java.util.Calendar; |
|
|
|
|
import java.util.Date; |
|
|
|
|
|
|
|
|
@ -32,6 +34,27 @@ public class DateUtils {
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(DateUtils.class); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <code>java.util.Date</code> to <code>java.time.LocalDateTime</code> |
|
|
|
|
* use default zone |
|
|
|
|
* @param date |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
private static LocalDateTime date2LocalDateTime(Date date) { |
|
|
|
|
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <code>java.time.LocalDateTime</code> to <code>java.util.Date</code> |
|
|
|
|
* use default zone |
|
|
|
|
* @param localDateTime |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
private static Date localDateTime2Date(LocalDateTime localDateTime) { |
|
|
|
|
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant(); |
|
|
|
|
return Date.from(instant); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @return get the formatted date string for the current time |
|
|
|
|
*/ |
|
|
|
@ -44,7 +67,8 @@ public class DateUtils {
|
|
|
|
|
* @return get the date string in the specified format of the current time |
|
|
|
|
*/ |
|
|
|
|
public static String getCurrentTime(String format) { |
|
|
|
|
return new SimpleDateFormat(format).format(new Date()); |
|
|
|
|
// return new SimpleDateFormat(format).format(new Date());
|
|
|
|
|
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(format)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -53,15 +77,25 @@ public class DateUtils {
|
|
|
|
|
* @return get the formatted date string |
|
|
|
|
*/ |
|
|
|
|
public static String format(Date date, String format) { |
|
|
|
|
return new SimpleDateFormat(format).format(date); |
|
|
|
|
// return new SimpleDateFormat(format).format(date);
|
|
|
|
|
return format(date2LocalDateTime(date), format); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param localDateTime |
|
|
|
|
* @param format e.g. yyyy-MM-dd HH:mm:ss |
|
|
|
|
* @return get the formatted date string |
|
|
|
|
*/ |
|
|
|
|
public static String format(LocalDateTime localDateTime, String format) { |
|
|
|
|
return localDateTime.format(DateTimeFormatter.ofPattern(format)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param date |
|
|
|
|
* @return convert time to yyyy-MM-dd HH:mm:ss format |
|
|
|
|
*/ |
|
|
|
|
public static String dateToString(Date date){ |
|
|
|
|
return format(date,Constants.YYYY_MM_DD_HH_MM_SS); |
|
|
|
|
public static String dateToString(Date date) { |
|
|
|
|
return format(date, Constants.YYYY_MM_DD_HH_MM_SS); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -69,9 +103,11 @@ public class DateUtils {
|
|
|
|
|
* @param date |
|
|
|
|
* @return convert string to date and time |
|
|
|
|
*/ |
|
|
|
|
public static Date parse(String date,String format){ |
|
|
|
|
public static Date parse(String date, String format) { |
|
|
|
|
try { |
|
|
|
|
return new SimpleDateFormat(format).parse(date); |
|
|
|
|
// return new SimpleDateFormat(format).parse(date);
|
|
|
|
|
LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(format)); |
|
|
|
|
return localDateTime2Date(ldt); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error("error while parse date:" + date, e); |
|
|
|
|
} |
|
|
|
@ -80,11 +116,12 @@ public class DateUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* convert date str to yyyy-MM-dd HH:mm:ss format |
|
|
|
|
* |
|
|
|
|
* @param str |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static Date stringToDate(String str){ |
|
|
|
|
return parse(str,Constants.YYYY_MM_DD_HH_MM_SS); |
|
|
|
|
public static Date stringToDate(String str) { |
|
|
|
|
return parse(str, Constants.YYYY_MM_DD_HH_MM_SS); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -135,6 +172,7 @@ public class DateUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get the date of the specified date in the days before and after |
|
|
|
|
* |
|
|
|
|
* @param date |
|
|
|
|
* @param day |
|
|
|
|
* @return |
|
|
|
@ -159,10 +197,11 @@ public class DateUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* convert schedule string to date |
|
|
|
|
* |
|
|
|
|
* @param schedule |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static Date getScheduleDate(String schedule){ |
|
|
|
|
public static Date getScheduleDate(String schedule) { |
|
|
|
|
return stringToDate(schedule); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -185,7 +224,7 @@ public class DateUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get monday |
|
|
|
|
* |
|
|
|
|
* <p> |
|
|
|
|
* note: Set the first day of the week to Monday, the default is Sunday |
|
|
|
|
*/ |
|
|
|
|
public static Date getMonday(Date date) { |
|
|
|
@ -201,7 +240,7 @@ public class DateUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get sunday |
|
|
|
|
* |
|
|
|
|
* <p> |
|
|
|
|
* note: Set the first day of the week to Monday, the default is Sunday |
|
|
|
|
*/ |
|
|
|
|
public static Date getSunday(Date date) { |
|
|
|
@ -257,10 +296,11 @@ public class DateUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* return YYYY-MM-DD 00:00:00 |
|
|
|
|
* |
|
|
|
|
* @param inputDay |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static Date getStartOfDay(Date inputDay){ |
|
|
|
|
public static Date getStartOfDay(Date inputDay) { |
|
|
|
|
Calendar cal = Calendar.getInstance(); |
|
|
|
|
cal.setTime(inputDay); |
|
|
|
|
cal.set(Calendar.HOUR_OF_DAY, 0); |
|
|
|
@ -271,10 +311,11 @@ public class DateUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* return YYYY-MM-DD 23:59:59 |
|
|
|
|
* |
|
|
|
|
* @param inputDay |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static Date getEndOfDay(Date inputDay){ |
|
|
|
|
public static Date getEndOfDay(Date inputDay) { |
|
|
|
|
Calendar cal = Calendar.getInstance(); |
|
|
|
|
cal.setTime(inputDay); |
|
|
|
|
cal.set(Calendar.HOUR_OF_DAY, 23); |
|
|
|
@ -285,10 +326,11 @@ public class DateUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* return YYYY-MM-DD 00:00:00 |
|
|
|
|
* |
|
|
|
|
* @param inputDay |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static Date getStartOfHour(Date inputDay){ |
|
|
|
|
public static Date getStartOfHour(Date inputDay) { |
|
|
|
|
Calendar cal = Calendar.getInstance(); |
|
|
|
|
cal.setTime(inputDay); |
|
|
|
|
cal.set(Calendar.MINUTE, 0); |
|
|
|
@ -298,10 +340,11 @@ public class DateUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* return YYYY-MM-DD 23:59:59 |
|
|
|
|
* |
|
|
|
|
* @param inputDay |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static Date getEndOfHour(Date inputDay){ |
|
|
|
|
public static Date getEndOfHour(Date inputDay) { |
|
|
|
|
Calendar cal = Calendar.getInstance(); |
|
|
|
|
cal.setTime(inputDay); |
|
|
|
|
cal.set(Calendar.MINUTE, 59); |
|
|
|
|