Optimize DateTimeUtils.java

This commit is contained in:
hongqiaowei
2022-09-15 16:44:00 +08:00
parent f3aab0ef2c
commit 5dbfe61536

View File

@@ -37,20 +37,21 @@ import java.util.stream.Stream;
public abstract class DateTimeUtils { public abstract class DateTimeUtils {
private static Map<String, DateTimeFormatter> dateTimeFormatters = new HashMap<>(); private static Map<String, DateTimeFormatter> dateTimeFormatterMap = new HashMap<>();
private static ZoneId defaultZone = ZoneId.systemDefault(); private static ZoneId defaultZone = ZoneId.systemDefault();
private static final String zeroTimeSuffix = " 00:00:00.000"; @Deprecated
private static final String zeroTimeSuffix = " 00:00:00.000";
private DateTimeUtils() { private DateTimeUtils() {
} }
public static DateTimeFormatter getDateTimeFormatter(String pattern) { public static DateTimeFormatter getDateTimeFormatter(String pattern) {
DateTimeFormatter f = dateTimeFormatters.get(pattern); DateTimeFormatter f = dateTimeFormatterMap.get(pattern);
if (f == null) { if (f == null) {
f = DateTimeFormatter.ofPattern(pattern); f = DateTimeFormatter.ofPattern(pattern);
dateTimeFormatters.put(pattern, f); dateTimeFormatterMap.put(pattern, f);
} }
return f; return f;
} }
@@ -59,6 +60,7 @@ public abstract class DateTimeUtils {
return ldt.atZone(defaultZone).toInstant().toEpochMilli(); return ldt.atZone(defaultZone).toInstant().toEpochMilli();
} }
@Deprecated
public static long toMillis(String dateTime, String... pattern) { public static long toMillis(String dateTime, String... pattern) {
if (dateTime.length() == 10) { if (dateTime.length() == 10) {
dateTime += zeroTimeSuffix; dateTime += zeroTimeSuffix;
@@ -72,6 +74,12 @@ public abstract class DateTimeUtils {
return toMillis(ldt); return toMillis(ldt);
} }
public static long toMillis(String dateTime, String pattern) {
DateTimeFormatter f = getDateTimeFormatter(pattern);
LocalDateTime ldt = LocalDateTime.parse(dateTime, f);
return toMillis(ldt);
}
public static LocalDate transform(Date date) { public static LocalDate transform(Date date) {
return date.toInstant().atZone(defaultZone).toLocalDate(); return date.toInstant().atZone(defaultZone).toLocalDate();
} }
@@ -96,6 +104,7 @@ public abstract class DateTimeUtils {
return Date.from(localDateTime.atZone(defaultZone).toInstant()); return Date.from(localDateTime.atZone(defaultZone).toInstant());
} }
@Deprecated
public static String convert(long mills, String... pattern) { public static String convert(long mills, String... pattern) {
String p = DP.DP10; String p = DP.DP10;
if (pattern.length != 0) { if (pattern.length != 0) {
@@ -106,6 +115,13 @@ public abstract class DateTimeUtils {
return ldt.format(f); return ldt.format(f);
} }
public static String convert(long mills, String pattern) {
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(mills), defaultZone);
DateTimeFormatter f = getDateTimeFormatter(pattern);
return ldt.format(f);
}
@Deprecated
public static String convert(LocalDate date, String... pattern) { public static String convert(LocalDate date, String... pattern) {
String p = DP.DP10; String p = DP.DP10;
if (pattern.length != 0) { if (pattern.length != 0) {
@@ -115,6 +131,12 @@ public abstract class DateTimeUtils {
return date.format(f); return date.format(f);
} }
public static String convert(LocalDate date, String pattern) {
DateTimeFormatter f = getDateTimeFormatter(pattern);
return date.format(f);
}
@Deprecated
public static String convert(LocalDateTime localDateTime, String... pattern) { public static String convert(LocalDateTime localDateTime, String... pattern) {
String p = DP.DP23; String p = DP.DP23;
if (pattern.length != 0) { if (pattern.length != 0) {
@@ -124,6 +146,11 @@ public abstract class DateTimeUtils {
return localDateTime.format(f); return localDateTime.format(f);
} }
public static String convert(LocalDateTime localDateTime, String pattern) {
DateTimeFormatter f = getDateTimeFormatter(pattern);
return localDateTime.format(f);
}
public static List<String> datesBetween(String start, String end) { public static List<String> datesBetween(String start, String end) {
LocalDate sd = LocalDate.parse(start); LocalDate sd = LocalDate.parse(start);
LocalDate ed = LocalDate.parse(end); LocalDate ed = LocalDate.parse(end);
@@ -146,7 +173,7 @@ public abstract class DateTimeUtils {
long numOfDaysBetween = ChronoUnit.DAYS.between(sd, ed); long numOfDaysBetween = ChronoUnit.DAYS.between(sd, ed);
return IntStream.iterate(0, i -> i + 1) return IntStream.iterate(0, i -> i + 1)
.limit(numOfDaysBetween) .limit(numOfDaysBetween)
.mapToObj(i -> sd.plusDays(i)) .mapToObj(sd::plusDays)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@@ -155,9 +182,10 @@ public abstract class DateTimeUtils {
} }
public static LocalDateTime beforeNowNoTime(long offsetDays) { public static LocalDateTime beforeNowNoTime(long offsetDays) {
return LocalDate.now().minusDays(offsetDays).atTime(0, 0, 0, 0); return LocalDate.now().minusDays(offsetDays).atStartOfDay();
} }
@Deprecated
public static LocalDateTime time2zero(LocalDateTime ldt) { public static LocalDateTime time2zero(LocalDateTime ldt) {
return ldt.withHour(0).withMinute(0).withSecond(0).with(ChronoField.MILLI_OF_SECOND, 0); return ldt.withHour(0).withMinute(0).withSecond(0).with(ChronoField.MILLI_OF_SECOND, 0);
} }