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);
posted @ 2020-11-16 15:04  阿狸哥哥  阅读(1462)  评论(0编辑  收藏  举报