package com.tcm.training.admin.common;
|
|
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
import java.time.LocalTime;
|
import java.time.format.DateTimeFormatter;
|
|
public class DateTimes {
|
public static final String LOCAL_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
public static final String LOCAL_DATE_PATTERN = "yyyy-MM-dd";
|
public static final String LOCAL_TIME_PATTERN = "HH:mm:ss";
|
public static final String PATTERN = LOCAL_DATE_TIME_PATTERN;
|
|
private static final DateTimeFormatter LOCAL_DATE_TIME_FORMATTER =
|
DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_PATTERN);
|
private static final DateTimeFormatter LOCAL_DATE_FORMATTER =
|
DateTimeFormatter.ofPattern(LOCAL_DATE_PATTERN);
|
private static final DateTimeFormatter LOCAL_TIME_FORMATTER =
|
DateTimeFormatter.ofPattern(LOCAL_TIME_PATTERN);
|
|
private DateTimes() {
|
}
|
|
public static LocalDateTime parse(String text) {
|
if (text == null || text.trim().isEmpty()) {
|
return null;
|
}
|
try {
|
return LocalDateTime.parse(text, LOCAL_DATE_TIME_FORMATTER);
|
} catch (Exception e) {
|
throw new IllegalArgumentException("时间格式错误,需 " + LOCAL_DATE_TIME_PATTERN);
|
}
|
}
|
|
public static String format(LocalDateTime dt) {
|
if (dt == null) {
|
return null;
|
}
|
return LOCAL_DATE_TIME_FORMATTER.format(dt);
|
}
|
|
public static String format(LocalDate date) {
|
if (date == null) {
|
return null;
|
}
|
return LOCAL_DATE_FORMATTER.format(date);
|
}
|
|
public static String format(LocalTime time) {
|
if (time == null) {
|
return null;
|
}
|
return LOCAL_TIME_FORMATTER.format(time);
|
}
|
|
public static DateTimeFormatter localDateTimeFormatter() {
|
return LOCAL_DATE_TIME_FORMATTER;
|
}
|
|
public static DateTimeFormatter localDateFormatter() {
|
return LOCAL_DATE_FORMATTER;
|
}
|
|
public static DateTimeFormatter localTimeFormatter() {
|
return LOCAL_TIME_FORMATTER;
|
}
|
}
|