import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class TimeUtils {
public TimeUtils() {
}
public static String timeOfLongToStr(Long time) {
return timeOfLongToStr(time, "yyyy-MM-dd HH:mm:ss");
}
public static String timeOfLongToStr(Long time, String pattern) {
if (time == null) {
return null;
} else {
DateTimeFormatter ftf = DateTimeFormatter.ofPattern(pattern);
return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
}
}
public static String getNowTimeStr() {
return getNowTimeStr("YYYY-MM-dd HH:mm:ss");
}
public static String getNowTimeStr(String pattern) {
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
return dateTimeFormatter.format(dateTime);
}
public static Long timeOfStrToLong(String strTime, String pattern) {
if (strTime == null) {
return null;
} else {
DateTimeFormatter ftf = DateTimeFormatter.ofPattern(pattern);
LocalDateTime parse = LocalDateTime.parse(strTime, ftf);
return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
}
}