博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 8 – How to format LocalDateTime
阅读量:6905 次
发布时间:2019-06-27

本文共 1505 字,大约阅读时间需要 5 分钟。

Few examples to show you how to format java.time.LocalDateTime in Java 8.

1. LocalDateTime + DateTimeFormatter

To format a LocalDateTime object, uses DateTimeFormatter

TestDate1.java
package com.mkyong.time; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class TestDate1 { public static void main(String[] args) { //Get current date time LocalDateTime now = LocalDateTime.now(); System.out.println("Before : " + now); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formatDateTime = now.format(formatter); System.out.println("After : " + formatDateTime); } }
 

Output

Before : 2016-11-09T11:44:44.797After  : 2016-11-09 11:44:44

2. String -> LocalDateTime

Another example to convert a String to LocalDateTime

TestDate2.java
package com.mkyong.time; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class TestDate2 { public static void main(String[] args) { String now = "2016-11-09 10:30"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); LocalDateTime formatDateTime = LocalDateTime.parse(now, formatter); System.out.println("Before : " + now); System.out.println("After : " + formatDateTime); System.out.println("After : " + formatDateTime.format(formatter)); } }
 

Output

Before : 2016-11-09 10:30After : 2016-11-09T10:30After : 2016-11-09 10:30

http://www.mkyong.com/java8/java-8-how-to-format-localdatetime/

http://www.mkyong.com/tutorials/java-date-time-tutorials/

转载地址:http://qpldl.baihongyu.com/

你可能感兴趣的文章
scala sortBy and sortWith
查看>>
请求合并哪家强
查看>>
nodejs检查已安装模块
查看>>
solr联合多个字段进行检索(multivalued和copyfield的使用)
查看>>
准备PPT过程中的一些文档记录
查看>>
Catel(翻译)-为什么选择Catel
查看>>
SQL Server 数据库备份和还原
查看>>
微信小程序 - 贝塞尔曲线(购物车效果)
查看>>
重启Oracle服务
查看>>
服务器意外关机,如何查看关机起因?
查看>>
杭电2049
查看>>
翻译的问题
查看>>
iOS的异步绘制--YYAsyncLayer源码分析
查看>>
RedHat Linux tftp服务器配置
查看>>
CS系统框架二(数据库配置)
查看>>
C#跨线程更改用户界面
查看>>
理解JS回调函数
查看>>
WCF返回null超时
查看>>
通过纯代码方式发布WCF服务
查看>>
【C#学习笔记】Dictionary容器使用
查看>>