Files
fizz-gateway-node/fizz-common/src/main/java/we/util/DateTimeUtils.java

200 lines
5.7 KiB
Java
Raw Normal View History

2020-09-02 18:35:03 +08:00
/*
* Copyright (C) 2020 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package we.util;
2020-09-02 18:35:03 +08:00
2022-07-09 11:07:15 +08:00
import we.util.Consts.DP;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
2020-09-02 18:35:03 +08:00
import java.time.format.DateTimeFormatter;
2021-09-29 14:13:46 +08:00
import java.time.temporal.ChronoField;
2020-09-02 18:35:03 +08:00
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
2021-09-29 14:13:46 +08:00
import java.util.stream.IntStream;
2020-09-02 18:35:03 +08:00
import java.util.stream.Stream;
/**
* @author hongqiaowei
2020-09-02 18:35:03 +08:00
*/
public abstract class DateTimeUtils {
private static Map<String, DateTimeFormatter> dateTimeFormatters = new HashMap<>();
private static ZoneId defaultZone = ZoneId.systemDefault();
2021-09-29 14:13:46 +08:00
private static final String zeroTimeSuffix = " 00:00:00.000";
private DateTimeUtils() {
}
2020-09-02 18:35:03 +08:00
public static DateTimeFormatter getDateTimeFormatter(String pattern) {
DateTimeFormatter f = dateTimeFormatters.get(pattern);
if (f == null) {
f = DateTimeFormatter.ofPattern(pattern);
dateTimeFormatters.put(pattern, f);
}
return f;
}
public static long toMillis(LocalDateTime ldt) {
return ldt.atZone(defaultZone).toInstant().toEpochMilli();
}
public static long toMillis(String dateTime, String... pattern) {
if (dateTime.length() == 10) {
dateTime += zeroTimeSuffix;
}
2021-09-29 14:13:46 +08:00
String p = DP.DP23;
2020-09-02 18:35:03 +08:00
if (pattern.length != 0) {
p = pattern[0];
}
DateTimeFormatter f = getDateTimeFormatter(p);
LocalDateTime ldt = LocalDateTime.parse(dateTime, f);
2021-09-29 14:13:46 +08:00
return toMillis(ldt);
2020-09-02 18:35:03 +08:00
}
2021-09-29 14:13:46 +08:00
public static LocalDate transform(Date date) {
return date.toInstant().atZone(defaultZone).toLocalDate();
2020-09-02 18:35:03 +08:00
}
2021-09-29 14:13:46 +08:00
public static LocalDateTime transform(long l) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(l), defaultZone);
2020-09-02 18:35:03 +08:00
}
2021-09-29 14:13:46 +08:00
public static LocalDateTime localDateTimeFrom(Date date) {
return date.toInstant().atZone(defaultZone).toLocalDateTime();
2020-09-02 18:35:03 +08:00
}
2021-09-29 14:13:46 +08:00
public static Date from(Instant i) {
return new Date(i.toEpochMilli());
2020-09-02 18:35:03 +08:00
}
2021-09-29 14:13:46 +08:00
public static Date from(LocalDate localDate) {
return Date.from(localDate.atStartOfDay().atZone(defaultZone).toInstant());
2020-09-02 18:35:03 +08:00
}
2021-09-29 14:13:46 +08:00
public static Date from(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(defaultZone).toInstant());
2020-09-02 18:35:03 +08:00
}
2021-09-29 14:13:46 +08:00
public static String convert(long mills, String... pattern) {
String p = DP.DP10;
if (pattern.length != 0) {
p = pattern[0];
}
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(mills), defaultZone);
DateTimeFormatter f = getDateTimeFormatter(p);
return ldt.format(f);
}
public static String convert(LocalDate date, String... pattern) {
String p = DP.DP10;
2020-09-02 18:35:03 +08:00
if (pattern.length != 0) {
p = pattern[0];
}
DateTimeFormatter f = getDateTimeFormatter(p);
return date.format(f);
}
2021-09-29 14:13:46 +08:00
public static String convert(LocalDateTime localDateTime, String... pattern) {
String p = DP.DP23;
2020-09-02 18:35:03 +08:00
if (pattern.length != 0) {
p = pattern[0];
}
DateTimeFormatter f = getDateTimeFormatter(p);
return localDateTime.format(f);
}
2021-09-29 14:13:46 +08:00
2020-09-02 18:35:03 +08:00
public static List<String> datesBetween(String start, String end) {
LocalDate sd = LocalDate.parse(start);
LocalDate ed = LocalDate.parse(end);
long dist = ChronoUnit.DAYS.between(sd, ed);
if (dist == 0) {
2021-09-29 14:13:46 +08:00
return Collections.emptyList();
2020-09-02 18:35:03 +08:00
} else if (dist < 0) {
2021-09-29 14:13:46 +08:00
LocalDate d = ed;
2020-09-02 18:35:03 +08:00
ed = sd;
2021-09-29 14:13:46 +08:00
sd = d;
2020-09-02 18:35:03 +08:00
dist = Math.abs(dist);
}
long max = dist + 1;
return Stream.iterate(sd, d -> {
return d.plusDays(1);
}).limit(max).map(LocalDate::toString).collect(Collectors.toList());
}
2021-09-29 14:13:46 +08:00
public static List<LocalDate> datesBetween(LocalDate sd, LocalDate ed) {
long numOfDaysBetween = ChronoUnit.DAYS.between(sd, ed);
return IntStream.iterate(0, i -> i + 1)
.limit(numOfDaysBetween)
.mapToObj(i -> sd.plusDays(i))
.collect(Collectors.toList());
2020-09-02 18:35:03 +08:00
}
public static LocalDate beforeNow(long offsetDays) {
return LocalDate.now().minusDays(offsetDays);
}
public static LocalDateTime beforeNowNoTime(long offsetDays) {
return LocalDate.now().minusDays(offsetDays).atTime(0, 0, 0, 0);
}
2021-09-29 14:13:46 +08:00
public static LocalDateTime time2zero(LocalDateTime ldt) {
return ldt.withHour(0).withMinute(0).withSecond(0).with(ChronoField.MILLI_OF_SECOND, 0);
}
public static boolean isSameDay(Date date1, Date date2) {
LocalDate localDate1 = date1.toInstant().atZone(defaultZone).toLocalDate();
LocalDate localDate2 = date2.toInstant().atZone(defaultZone).toLocalDate();
return localDate1.isEqual(localDate2);
}
2022-07-09 11:07:15 +08:00
public static long get10sTimeWinStart(int n) {
LocalDateTime now = LocalDateTime.now().with(ChronoField.MILLI_OF_SECOND, 0);
int sec = now.getSecond();
long interval;
if (sec > 49) {
interval = sec - 50;
} else if (sec > 39) {
interval = sec - 40;
} else if (sec > 29) {
interval = sec - 30;
} else if (sec > 19) {
interval = sec - 20;
} else if (sec > 9) {
interval = sec - 10;
} else {
interval = sec;
}
long millis = toMillis(now);
return millis - interval * 1000 - (n - 1) * 10L * 1000;
}
2021-09-29 14:13:46 +08:00
/*
void iterateBetweenDatesJava8(LocalDate start, LocalDate end) {
for (LocalDate date = start; date.isBefore(end); date = date.plusDays(1)) {
processDate(date);
}
}
*/
2020-09-02 18:35:03 +08:00
}