部分功能演示
LocalDateTime parseTime = TimeUtils.parse("2023-02-02 00:00:00");
LocalDateTime utcNow = TimeUtils.UTC.now();
String defaultZoneUTC = TimeUtils.Zone.getDefaultZoneUTC();
时间工具类代码
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.TimeZone;
public final class TimeUtils {
public static final String FULL_TIME_PATTERN = "yyyyMMddHHmmss";
public static final String FULL_TIME_SPLIT_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static final String TIME_SPLIT_PATTERN = "HH:mm:ss";
public static final String MONTH_TIME_SPLIT_PATTERN = "yyyy-MM";
public static final String MONTH_DAY_SPLIT_PATTERN = "MM-dd";
public static LocalDateTime parse(String timeStr, String timeFormat) {
return LocalDateTime.parse(timeStr, DateTimeFormatter.ofPattern(timeFormat));
}
public static LocalDateTime parse(String timeStr) {
return LocalDateTime.parse(timeStr, DateTimeFormatter.ofPattern(FULL_TIME_SPLIT_PATTERN));
}
public static LocalDateTime toZone(LocalDateTime time, ZoneId fromZone, ZoneId toZone) {
ZonedDateTime zonedDateTime = time.atZone(fromZone).withZoneSameInstant(toZone);
return zonedDateTime.toLocalDateTime();
}
public static long timestamp() {
return System.currentTimeMillis();
}
public static long toTimestamp(LocalDateTime time, ZoneOffset zone) {
return time.toInstant(zone).toEpochMilli();
}
public static class UTC {
public static LocalDateTime now() {
return LocalDateTime.now(ZoneOffset.UTC);
}
public static LocalDateTime toUtc(LocalDateTime time, ZoneId fromZone) {
ZonedDateTime zonedDateTime = time.atZone(fromZone).withZoneSameInstant(ZoneOffset.UTC);
return zonedDateTime.toLocalDateTime();
}
public static LocalDateTime utcToZone(LocalDateTime utcTime, ZoneId toZone) {
Objects.requireNonNull(utcTime, "utcTime");
Objects.requireNonNull(toZone, "toZone");
ZonedDateTime zonedDateTime = utcTime.atZone(ZoneOffset.UTC).withZoneSameInstant(toZone);
return zonedDateTime.toLocalDateTime();
}
public static long timestamp() {
return System.currentTimeMillis();
}
public static long toTimestamp(LocalDateTime utcTime) {
return utcTime.toInstant(ZoneOffset.UTC).toEpochMilli();
}
public static LocalDateTime parseUtc(String timeStr, String timeFormat) {
return LocalDateTime.parse(timeStr, DateTimeFormatter.ofPattern(timeFormat)).atZone(ZoneOffset.UTC).toLocalDateTime();
}
public static LocalDateTime parseUtc(String timeStr) {
return LocalDateTime.parse(timeStr, DateTimeFormatter.ofPattern(FULL_TIME_SPLIT_PATTERN)).atZone(ZoneOffset.UTC).toLocalDateTime();
}
}
public static class Zone {
public static String getDefaultZoneGMT(boolean full) {
int offset = TimeZone.getDefault().getOffset(System.currentTimeMillis());
int h = offset / 3600000;
int hr = offset % 3600000;
int m = hr / 60000;
int mr = hr % 60000;
int s = mr / 1000;
StringBuilder gmt = new StringBuilder("GMT");
gmt.append(offset >= 0 ? "+" : "-");
gmt.append((h >= 10 ? h : ("0" + h)));
if (m > 0 || s > 0 || full) gmt.append(":").append(m >= 10 ? m : ("0" + m));
if (s > 0 || full) gmt.append(":").append(s >= 10 ? s : ("0" + s));
return gmt.toString();
}
public static String getDefaultZoneGMT() {
return getDefaultZoneGMT(false);
}
public static String getDefaultZoneUTC(boolean full) {
int offset = TimeZone.getDefault().getRawOffset();
int h = offset / 3600000;
int hr = offset % 3600000;
int m = hr / 60000;
int mr = hr % 60000;
int s = mr / 1000;
StringBuilder gmt = new StringBuilder("UTC");
gmt.append(offset >= 0 ? "+" : "-");
gmt.append((h >= 10 ? h : ("0" + h)));
if (m > 0 || s > 0 || full) gmt.append(":").append(m >= 10 ? m : ("0" + m));
if (s > 0 || full) gmt.append(":").append(s >= 10 ? s : ("0" + s));
return gmt.toString();
}
public static String getDefaultZoneUTC() {
return getDefaultZoneUTC(false);
}
}
}