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()方法有两种重 ...
随机推荐
- 微信小程序 - 日期(起止)选择器组件
2019-01-03 : 修复了日期day-1,新增了年月日(除去时分秒),删除了不必要的touchmove 新增: column: ""(年月日) 配置: pickerConfi ...
- vSphere Data Protection – a new backup product included with vSphere 5.1
August 27, 2012 By Vladan SEGET This new backup product replaces VMware Data Recovery, which has bee ...
- 使用JAVA的URL类处理url事例
import java.net.*; import java.io.*; public class ParseURL { public static void main(String[] args) ...
- Mysql查询优化之 触发器加中间表 方法优化count()统计大数据量总数问题
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6138288.html 在上一篇博文我们提到,分页有三种方法.其中,第三种是我们最常用的.然而,在实际应用过程中 ...
- Java之创建对象>7.Avoid finalizers
1.Finalizers are unpredictable, often dangerous, and generally unnecessary. 2.never do anything time ...
- 〖Linux〗让Kubuntu的“启动栏”与Win7“任务栏”的界面和功能一样
先来展示一下我的桌面效果图: === 是否发现这与Windows 7任务栏非常相似?哈哈- === 背景: 玩久了Unity,想换个品味,就把Ubuntu安装了KDE桌面,发现甚是不错: 这里教大家怎 ...
- 10、java5线程池之返回结果的任务之Callable与Future
JDK文档描述Callable: public interface Callable<V>返回结果并且可能抛出异常的任务.实现者定义了一个不带任何参数的叫做 call 的方法. Calla ...
- 用html.parser抓网页中的超链接,返回list
#python3 from html.parser import HTMLParser class MyHTMLParser(HTMLParser): """ 1.tag ...
- Probability&Statistics 概率论与数理统计(1)
基本概念 样本空间: 随机试验E的所有可能结果组成的集合, 为E的样本空间, 记为S 随机事件: E的样本空间S的子集为E的随机事件, 简称事件, 由一个样本点组成的单点集, 称为基本事件 对立事件/ ...
- (转)Delta3D源码分析
最近学习Delta3D, 2.4版忙着发布,一直不能成功编译SimCore, 索性静下心来看看源码,官网上竟然提供了几个重要组建的软件设计说明书(SDD),虽说基本都是2005版了,不过我看了后觉得 ...