import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class LocalDateUtil {
private static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
private static DateTimeFormatter localDateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static DateTimeFormatter localDateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static DateTimeFormatter localTimeFormat = DateTimeFormatter.ofPattern("HH:mm:ss");
public static String getLocalDateTimeFormat(LocalDateTime localDateTime){
String format = localDateTimeFormat.format(localDateTime);
return format;
}
public static String getLocalDateFormat(LocalDate localDate){
String format = localDateFormat.format(localDate);
return format;
}
public static String getLocalTimeFormat(LocalTime localTime){
String format = localTimeFormat.format(localTime);
return format;
}
public static String getDateTimeFormat(Date date){
String format = dateTimeFormat.format(date);
return format;
}
public static String getDateFormat(Date date){
String format = dateFormat.format(date);
return format;
}
public static String getTimeFormat(Date date){
String format = timeFormat.format(date);
return format;
}
public static Long getLocalDateTimeCurrentSec(){
//获取当前时间戳,单位秒
Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
return second;
}
public static Long getLocalDateTimeCurrentMilliSec(){
//获取当前时间戳,单位毫秒
Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
return milliSecond;
}
public static LocalDateTime dateConvertToLocalDateTime(Date date) {
//将java.util.Date 转换为java8 的java.time.LocalDateTime,默认时区为东8区
return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
}
public static Date localDateTimeConvertToDate(LocalDateTime localDateTime) {
//将java8 的 java.time.LocalDateTime 转换为 java.util.Date,默认时区为东8区
return Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
}
public static LocalDateTime secondToLocalDateTime(Long second) {
//时间戳转LocalDateTime (单位:秒)
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.ofHours(8));
return localDateTime;
}
public static LocalDateTime millisecondsToLocalDateTime(Long milliseconds) {
//时间戳转LocalDateTime (单位:毫秒)
LocalDateTime localDateTime = Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
return localDateTime;
}
public static Date timestampToDate(Long timestamp){
return new Date(timestamp);
}
public static Date timeStrToDate(String timeStr){
//字符串格式 yyyy-MM-dd HH:mm:ss
Date date = null;
try {
date = dateTimeFormat.parse(timeStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static LocalDateTime timeStrToLocalDateTime(String timeStr){
//字符串格式 yyyy-MM-dd HH:mm:ss
LocalDateTime parse = LocalDateTime.parse(timeStr, localDateTimeFormat);
return parse;
}
}
测试:
public static void localDateTimeTest(){
String localDateTimeFormat = LocalDateUtil.getLocalDateTimeFormat(LocalDateTime.now());
System.out.println(localDateTimeFormat);
String localDateFormat = LocalDateUtil.getLocalDateFormat(LocalDate.now());
System.out.println(localDateFormat);
String localTimeFormat = LocalDateUtil.getLocalTimeFormat(LocalTime.now());
System.out.println(localTimeFormat);
String dateTimeFormat = LocalDateUtil.getDateTimeFormat(new Date());
System.out.println(dateTimeFormat);
String dateFormat = LocalDateUtil.getDateFormat(new Date());
System.out.println(dateFormat);
String timeFormat = LocalDateUtil.getTimeFormat(new Date());
System.out.println(timeFormat);
System.out.println("---------------------------------------");
System.out.println(LocalDateUtil.getLocalDateTimeCurrentSec());
System.out.println(LocalDateUtil.getLocalDateTimeCurrentMilliSec());
System.out.println(LocalDateUtil.dateConvertToLocalDateTime(new Date()));
System.out.println(LocalDateUtil.localDateTimeConvertToDate(LocalDateTime.now()));
System.out.println("---------------------------------------");
System.out.println(LocalDateUtil.secondToLocalDateTime(System.currentTimeMillis()/1000));
System.out.println(LocalDateUtil.millisecondsToLocalDateTime(System.currentTimeMillis()));
System.out.println(LocalDateUtil.timestampToDate(System.currentTimeMillis()));
System.out.println(LocalDateUtil.timeStrToDate("2022-08-26 14:18:23"));
System.out.println(LocalDateUtil.timeStrToLocalDateTime("2022-08-26 14:18:23"));
}