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()方法有两种重 ...
随机推荐
- JavaScript 之 JavaScript 对象
重新看JavaScript对象,参考资料: RUNOOB.COM:http://www.runoob.com/jsref/jsref-fromcharcode.html: CodePlayer:htt ...
- [转]Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置
转自:http://www.trinea.cn/android/maven/ 两年半前写的关于Maven的介绍,现在看来都还是不错的,自己转下.写博客的一大好处就是方便自己以后查阅,自己总结的总是最靠 ...
- Max SPA
Stingray Response_ContentType Stingray javascript 其实是支持返回原生html的, 有了这个事情就简单了 RHTML - Response_Conten ...
- python 大小端数据转换
"6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000".decode('hex')[::-1].enc ...
- Android学习笔记二:activity的理解
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7513290.html 一:activity定义了app的页面 一个app有很多个页面组成,一个页面其实就是一个 ...
- Linux进程共享通信 -- mmap实现
https://blog.csdn.net/y396397735/article/details/50651633 使用mmap内存映射实现一端写,另一端读的进程间通信 写端代码write.c /*w ...
- 【Shell】获取设置日期和延时
1.读取日期 Linux:~ # date Mon Dec 17 03:16:53 EST 2018 2.格式日期并打印 Linux:~ # date "+%d %B %Y" 17 ...
- V-rep学习笔记:并联机构正逆运动学
Solving the FK problem of simple kinematic chains is trivial (just apply the desired joint values to ...
- CentOS7统计某个进程当前的线程数
方式一: cat /proc/[pid]/status 展示结果中,Threads后边对应的数字就是进程拥有的线程数量 方式二: |wc -l
- MySQL存储引擎--MYSIAM和INNODB引擎区别
参考:http://blog.csdn.net/memray/article/details/8914042 MYSIAM和INNODB引擎区别主要有以下几点: 1.MyISAM查询性能比InnoDB ...