Java 8 – How to format LocalDateTime
Few examples to show you how to format java.time.LocalDateTime
in Java 8.
1. LocalDateTime + DateTimeFormatter
To format a LocalDateTime object, uses DateTimeFormatter
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.797
After : 2016-11-09 11:44:44
2. String -> LocalDateTime
Another example to convert a String to LocalDateTime
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:30
After : 2016-11-09T10:30
After : 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/
Java 8 – How to format LocalDateTime的更多相关文章
- JAVA字符串格式化String.format()的使用
JAVA字符串格式化-String.format()的使用常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprin ...
- java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
此方法为Timestamp的 转换方法. 这几天做到excel导入功能,其中里面有几个时间时段,所以用了这个类来将导入的字符串格式转换Timestamp格式. 不慎出现了 java.lang.Ille ...
- java日期格式大全 format SimpleDateFormat(转)
java日期格式大全 format SimpleDateFormat /** * 字符串转换为java.util.Date<br> * 支持格式为 yyyy.MM.dd G ...
- JAVA字符串格式化-String.format()的使用 【生成随机数补0操作】
转: JAVA字符串格式化-String.format()的使用 常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的s ...
- Java中强大的format
Java中强大的format Java中允许我们对指定的对象进行某种格式化,从而得到我们想要的格式化样式. Format 首先介绍java.text包中的Format Foramt是一个抽象基类,其具 ...
- Java 线程安全LocalTime 和LocaldateTime 新的Date和Time类 -JDK8新时间类的简单使用
不可变类且线程安全 LocalDate .java.time.LocalTime 和LocaldateTime 新的Date和Time类 DateTimeFormatter ==https://ww ...
- java日期互转:LocalDateTime、String、TimeStamp、Long、Instant、Date
由于java版本的迭代,一个使用java开发的项目中可能出现多种日期对象,例如LocalDateTime.LocalDate.Date,不像C#只有一个DateTime,因此在各种日期格式或者对象之间 ...
- JAVA中JDK1.8的LocalDateTime日期类的操作方法
LocalDateTime与Date相互转换参考:https://www.cnblogs.com/pxblog/p/13745972.html 关键类 Instant:瞬时时间. LocalDate: ...
- Java系列: JAVA字符串格式化-String.format()的使用(zz)
常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...
随机推荐
- Zabbix-Agent在主动模式启动服务后,提示no active checks on server [139.219.xx.xx:10051]: host [139.219.xx.xx] not found
一.解决方法
- websocket与canvas[转]
server端还是用tomcat7的方式客户端 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ...
- nyoj 119士兵杀敌(三)(线段树区间最值查询,RMQ算法)
题目119 题目信息 执行结果 本题排行 讨论区 士兵杀敌(三) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描写叙述 南将军统率着N个士兵,士兵分别编号为1~N,南将军常 ...
- RN API备忘
1:Alert:启动一个提示对话框,包含对应的标题和信息. 2:Animated:动画效果. 3:AppRegistry:React Native应用的入口.应用的根组件应当通过AppRegistry ...
- OdiSendMail
在Package中使用ODI自带的发送邮件OdiSendMail,生成的场景迁移到正式环境中,提示 javax.mail.AuthenticationFailedException: failed t ...
- Dubbo源码解读:appendAnnotation [01]
import java.lang.reflect.Method; public class AppendAnnotation { /** * 获取某个Annotation实例的所有方法值(实际是Ann ...
- java 判断是否为纯数字
java 判断是否为数字格式 CreateTime--2017年12月1日10:37:00 Author:Marydon java 判断是否为数字格式 /** * 判断是否为数字格式不限制位数 * ...
- Axure 地区选择(选择省份之后可以选择对应的地级市)
百度网盘:http://pan.baidu.com/s/1c1ZjUPq
- 在spring boot微服务中使用JWS发布webService
发布时间:2018-11-22 技术:Java+spring+maven 概述 在springboot微服务中使用JWS发布webService,在服务启动时自动发布webservice接口. ...
- 编程算法 - 迷宫的最短路径 代码(C++)
迷宫的最短路径 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定一个大小为N*M的迷宫. 迷宫由通道和墙壁组成, 每一步能够向邻接的上下 ...