|
@@ -117,6 +117,52 @@ public final class DateUtils {
|
|
|
private DateUtils() {
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 日期文本解析器
|
|
|
+ */
|
|
|
+ @FunctionalInterface
|
|
|
+ public interface Parser {
|
|
|
+ /**
|
|
|
+ * 日期文本解析
|
|
|
+ *
|
|
|
+ * @param text 日期文本
|
|
|
+ * @return 日期对象实例
|
|
|
+ */
|
|
|
+ Date parse(String text);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期文本解析实现
|
|
|
+ */
|
|
|
+ public static class DateParser implements Parser {
|
|
|
+ private final DateTimeFormatter formatter;
|
|
|
+
|
|
|
+ public DateParser(@NonNull DateTimeFormatter formatter) {
|
|
|
+ this.formatter = formatter;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Date parse(String text) {
|
|
|
+ return convert(LocalDate.parse(text, this.formatter));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期时间文本解析实现
|
|
|
+ */
|
|
|
+ public static class DatetimeParser implements Parser {
|
|
|
+ private final DateTimeFormatter formatter;
|
|
|
+
|
|
|
+ public DatetimeParser(@NonNull DateTimeFormatter formatter) {
|
|
|
+ this.formatter = formatter;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Date parse(String text) {
|
|
|
+ return convert(LocalDateTime.parse(text, this.formatter));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 查找日期文本对应的解析器
|
|
|
*
|
|
@@ -144,7 +190,7 @@ public final class DateUtils {
|
|
|
* @param date 日期时间对象实例
|
|
|
* @return true/false
|
|
|
*/
|
|
|
- public static boolean hasTime(Date date) {
|
|
|
+ public static boolean isTemporal(Date date) {
|
|
|
if (date == null) {
|
|
|
return false;
|
|
|
}
|
|
@@ -160,7 +206,7 @@ public final class DateUtils {
|
|
|
* @param datetime 日期时间对象实例
|
|
|
* @return true/false
|
|
|
*/
|
|
|
- public static boolean hasTime(LocalDateTime datetime) {
|
|
|
+ public static boolean isTemporal(LocalDateTime datetime) {
|
|
|
return datetime != null && (datetime.getHour() > 0 || datetime.getMinute() > 0 || datetime.getSecond() > 0
|
|
|
|| datetime.getNano() > 0);
|
|
|
}
|
|
@@ -203,7 +249,22 @@ public final class DateUtils {
|
|
|
* @return 日期实例
|
|
|
*/
|
|
|
public static Date tomorrow() {
|
|
|
- return tomorrow(new Date());
|
|
|
+ return tomorrow(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取明天日期
|
|
|
+ *
|
|
|
+ * @param temporal 是否包含时间
|
|
|
+ * @return 日期实例
|
|
|
+ */
|
|
|
+ public static Date tomorrow(boolean temporal) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.add(Calendar.DATE, 1);
|
|
|
+ if (!temporal) {
|
|
|
+ clearTime(calendar);
|
|
|
+ }
|
|
|
+ return calendar.getTime();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -213,10 +274,23 @@ public final class DateUtils {
|
|
|
* @return 日期实例
|
|
|
*/
|
|
|
public static Date tomorrow(Date datetime) {
|
|
|
+ return tomorrow(datetime, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取明天日期
|
|
|
+ *
|
|
|
+ * @param datetime 相对日期时间
|
|
|
+ * @param temporal 是否包含时间
|
|
|
+ * @return 日期实例
|
|
|
+ */
|
|
|
+ public static Date tomorrow(Date datetime, boolean temporal) {
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
calendar.setTime(datetime);
|
|
|
calendar.add(Calendar.DATE, 1);
|
|
|
- clearTime(calendar);
|
|
|
+ if (!temporal) {
|
|
|
+ clearTime(calendar);
|
|
|
+ }
|
|
|
return calendar.getTime();
|
|
|
}
|
|
|
|
|
@@ -226,7 +300,22 @@ public final class DateUtils {
|
|
|
* @return 日期实例
|
|
|
*/
|
|
|
public static Date yesterday() {
|
|
|
- return yesterday(new Date());
|
|
|
+ return yesterday(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取昨天日期
|
|
|
+ *
|
|
|
+ * @param temporal 是否包含时间
|
|
|
+ * @return 日期实例
|
|
|
+ */
|
|
|
+ public static Date yesterday(boolean temporal) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.add(Calendar.DATE, -1);
|
|
|
+ if (!temporal) {
|
|
|
+ clearTime(calendar);
|
|
|
+ }
|
|
|
+ return calendar.getTime();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -236,10 +325,23 @@ public final class DateUtils {
|
|
|
* @return 日期实例
|
|
|
*/
|
|
|
public static Date yesterday(Date datetime) {
|
|
|
+ return yesterday(datetime, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取昨天日期
|
|
|
+ *
|
|
|
+ * @param datetime 相对日期时间
|
|
|
+ * @param temporal 是否包含时间
|
|
|
+ * @return 日期实例
|
|
|
+ */
|
|
|
+ public static Date yesterday(Date datetime, boolean temporal) {
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
calendar.setTime(datetime);
|
|
|
calendar.add(Calendar.DATE, -1);
|
|
|
- clearTime(calendar);
|
|
|
+ if (!temporal) {
|
|
|
+ clearTime(calendar);
|
|
|
+ }
|
|
|
return calendar.getTime();
|
|
|
}
|
|
|
|
|
@@ -365,19 +467,19 @@ public final class DateUtils {
|
|
|
* @return 日期文本
|
|
|
*/
|
|
|
public static String format(Date date) {
|
|
|
- return date == null ? null : format(date, hasTime(date));
|
|
|
+ return date == null ? null : format(date, isTemporal(date));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 日期格式化
|
|
|
*
|
|
|
- * @param date 日期对象实例
|
|
|
- * @param timely 是否格式化时间
|
|
|
+ * @param date 日期对象实例
|
|
|
+ * @param temporal 是否格式化时间
|
|
|
* @return 日期文本
|
|
|
*/
|
|
|
- public static String format(Date date, boolean timely) {
|
|
|
+ public static String format(Date date, boolean temporal) {
|
|
|
LocalDateTime datetime = convert(date);
|
|
|
- return datetime == null ? null : timely ? format(datetime) : format(datetime.toLocalDate());
|
|
|
+ return datetime == null ? null : temporal ? format(datetime) : format(datetime.toLocalDate());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -757,7 +859,18 @@ public final class DateUtils {
|
|
|
* @return 运算后的日期对象实例
|
|
|
*/
|
|
|
public static Date add(Duration duration) {
|
|
|
- return duration == null ? null : add(Calendar.SECOND, (int) duration.getSeconds());
|
|
|
+ return add(duration, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期加减
|
|
|
+ *
|
|
|
+ * @param duration 有效时间
|
|
|
+ * @param temporal 是否包含时间
|
|
|
+ * @return 运算后的日期对象实例
|
|
|
+ */
|
|
|
+ public static Date add(Duration duration, boolean temporal) {
|
|
|
+ return duration == null ? null : add(Calendar.SECOND, (int) duration.getSeconds(), temporal);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -768,10 +881,22 @@ public final class DateUtils {
|
|
|
* @return 运算后的日期对象实例
|
|
|
*/
|
|
|
public static Date add(Date date, Duration duration) {
|
|
|
+ return add(date, duration, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期加减
|
|
|
+ *
|
|
|
+ * @param date 原始日期
|
|
|
+ * @param duration 有效时间
|
|
|
+ * @param temporal 是否包含时间
|
|
|
+ * @return 运算后的日期对象实例
|
|
|
+ */
|
|
|
+ public static Date add(Date date, Duration duration, boolean temporal) {
|
|
|
if (date == null || duration == null) {
|
|
|
return null;
|
|
|
}
|
|
|
- return add(date, Calendar.SECOND, (int) duration.getSeconds());
|
|
|
+ return add(date, Calendar.SECOND, (int) duration.getSeconds(), temporal);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -782,8 +907,23 @@ public final class DateUtils {
|
|
|
* @return 运算后的日期对象实例
|
|
|
*/
|
|
|
public static Date add(int field, int amount) {
|
|
|
+ return add(field, amount, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期加减
|
|
|
+ *
|
|
|
+ * @param field 加减字段
|
|
|
+ * @param amount 加减数量
|
|
|
+ * @param temporal 是否包含时间
|
|
|
+ * @return 运算后的日期对象实例
|
|
|
+ */
|
|
|
+ public static Date add(int field, int amount, boolean temporal) {
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
calendar.add(field, amount);
|
|
|
+ if (!temporal) {
|
|
|
+ clearTime(calendar);
|
|
|
+ }
|
|
|
return calendar.getTime();
|
|
|
}
|
|
|
|
|
@@ -796,12 +936,28 @@ public final class DateUtils {
|
|
|
* @return 运算后的日期对象实例
|
|
|
*/
|
|
|
public static Date add(Date date, int field, int amount) {
|
|
|
+ return add(date, field, amount, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期加减
|
|
|
+ *
|
|
|
+ * @param date 原始日期
|
|
|
+ * @param field 加减字段
|
|
|
+ * @param amount 加减数量
|
|
|
+ * @param temporal 是否包含时间
|
|
|
+ * @return 运算后的日期对象实例
|
|
|
+ */
|
|
|
+ public static Date add(Date date, int field, int amount, boolean temporal) {
|
|
|
if (date == null) {
|
|
|
return null;
|
|
|
}
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
calendar.setTime(date);
|
|
|
calendar.add(field, amount);
|
|
|
+ if (!temporal) {
|
|
|
+ clearTime(calendar);
|
|
|
+ }
|
|
|
return calendar.getTime();
|
|
|
}
|
|
|
|
|
@@ -977,50 +1133,4 @@ public final class DateUtils {
|
|
|
int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
|
|
return hour >= 9 && hour < 18;
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 日期文本解析器
|
|
|
- */
|
|
|
- @FunctionalInterface
|
|
|
- public interface Parser {
|
|
|
- /**
|
|
|
- * 日期文本解析
|
|
|
- *
|
|
|
- * @param text 日期文本
|
|
|
- * @return 日期对象实例
|
|
|
- */
|
|
|
- Date parse(String text);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 日期文本解析实现
|
|
|
- */
|
|
|
- public static class DateParser implements Parser {
|
|
|
- private final DateTimeFormatter formatter;
|
|
|
-
|
|
|
- public DateParser(@NonNull DateTimeFormatter formatter) {
|
|
|
- this.formatter = formatter;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Date parse(String text) {
|
|
|
- return convert(LocalDate.parse(text, this.formatter));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 日期时间文本解析实现
|
|
|
- */
|
|
|
- public static class DatetimeParser implements Parser {
|
|
|
- private final DateTimeFormatter formatter;
|
|
|
-
|
|
|
- public DatetimeParser(@NonNull DateTimeFormatter formatter) {
|
|
|
- this.formatter = formatter;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Date parse(String text) {
|
|
|
- return convert(LocalDateTime.parse(text, this.formatter));
|
|
|
- }
|
|
|
- }
|
|
|
}
|