JAVA8 时间格式转换

Posted 阿狸哥哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);















以上是关于JAVA8 时间格式转换的主要内容,如果未能解决你的问题,请参考以下文章

为 Blogger 上的博客格式化代码片段 [关闭]

使用 java 8 和格式将字符串转换为日期 -> 'uuuu-MM-dd'

JAVA8新特性

如何在 Reactjs 中添加丰富的代码片段?

java8日期时间API的一些使用 | 转字符串 | 格式化等

java8日期时间API的一些使用 | 转字符串 | 格式化等