JAVA8 时间格式转换

DateTimeFormatter formatter= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//对象转字符串
String timeStr = formatter.format(LocalDateTime.now());
System.out.println(timeStr);

//字符串转对象
LocalDateTime time = LocalDateTime.parse("2020-10-11 12:23:34",formatter);
System.out.println(time);

//对象转long(秒数)
Long seconds = time.toEpochSecond(ZoneOffset.of("+8"));
System.out.println(seconds);

//long转对象
LocalDateTime time2 = LocalDateTime.ofEpochSecond(1602390214,0,ZoneOffset.of("+8"));
System.out.println(time2);
原文地址:https://www.cnblogs.com/aligege/p/13985114.html