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.797

After  : 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: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的更多相关文章

  1. JAVA字符串格式化String.format()的使用

    JAVA字符串格式化-String.format()的使用常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprin ...

  2. java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

    此方法为Timestamp的 转换方法. 这几天做到excel导入功能,其中里面有几个时间时段,所以用了这个类来将导入的字符串格式转换Timestamp格式. 不慎出现了 java.lang.Ille ...

  3. java日期格式大全 format SimpleDateFormat(转)

    java日期格式大全 format SimpleDateFormat   /**    * 字符串转换为java.util.Date<br>    * 支持格式为 yyyy.MM.dd G ...

  4. JAVA字符串格式化-String.format()的使用 【生成随机数补0操作】

    转: JAVA字符串格式化-String.format()的使用 常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的s ...

  5. Java中强大的format

    Java中强大的format Java中允许我们对指定的对象进行某种格式化,从而得到我们想要的格式化样式. Format 首先介绍java.text包中的Format Foramt是一个抽象基类,其具 ...

  6. Java 线程安全LocalTime 和LocaldateTime 新的Date和Time类 -JDK8新时间类的简单使用

    不可变类且线程安全 LocalDate .java.time.LocalTime 和LocaldateTime  新的Date和Time类 DateTimeFormatter ==https://ww ...

  7. java日期互转:LocalDateTime、String、TimeStamp、Long、Instant、Date

    由于java版本的迭代,一个使用java开发的项目中可能出现多种日期对象,例如LocalDateTime.LocalDate.Date,不像C#只有一个DateTime,因此在各种日期格式或者对象之间 ...

  8. JAVA中JDK1.8的LocalDateTime日期类的操作方法

    LocalDateTime与Date相互转换参考:https://www.cnblogs.com/pxblog/p/13745972.html 关键类 Instant:瞬时时间. LocalDate: ...

  9. Java系列: JAVA字符串格式化-String.format()的使用(zz)

    常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...

随机推荐

  1. 深度学习-Windows平台下的Caffe编译教程

    一.安装CUDA7.5 Cuda是英伟达推出的GPU加速运算平台 我这里安装的是cuda7.5,已经安装过的忽略,还没有安装过的这里有安装教程.windows下面安装还是非常简单的. https:// ...

  2. Go语言使用Beego的ORM插入Mysql后,时区不一致的解决方案

    一.解决方案: 方案一: orm.Debug = false orm.DefaultTimeLoc = time.UTC orm.RegisterDriver("mysql", o ...

  3. 浅谈mysql中utf8和utf8mb4区别

    转自:http://ourmysql.com/archives/1402  实践过程中发现有时mysql的字符集会引起故障,所以需要了解下这个知识点. 一.简介 MySQL在5.5.3之后增加了这个u ...

  4. WIP 005 - Implement the search result page

    Need to show the search result in a tablewalker You can show the tablewalker in the search page whic ...

  5. SpringMVC,Ehcache

    来自:http://blog.csdn.net/jadyer/article/details/12257865 http://jinnianshilongnian.iteye.com/blog/200 ...

  6. SSM项目的数据库密码加密方案

    项目主要采用:SpringMVC4.3.2.RELEASE +Spring4.3.2.RELEASE + Maven 3.3.3 + druid 1.0.29 + Mybatis 3.2.8 + My ...

  7. 记:青岛理工ACM交流赛筹备工作总结篇

    这几天筹备青岛理工ACM交流赛的过程中遇到了不少问题也涨了不少经验.对非常多事也有了和曾经不一样的看法, ​一直在想事后把这几天的流水帐记一遍,一直没空直到今天考完C++才坐下来開始动笔.将这几天的忙 ...

  8. 【Linux】命令写在文件中并调用awk -f

    我们在使用awk命令的时候,有时候命令特别长,在终端写出来格式太乱,难以阅读,以下是一个将命令写在文件中,并使用awk调用的具体案例 1.现在有文件file3.txt,内容如下: 2.ak2.awk脚 ...

  9. 关于继承中的super()调用父类构造方法

    super super可以理解为是指向自己超(父)类对象的一个指针,而这个超类指的是离自己最近的一个父类. 每当你new一个子类对象的时候,子类对象都隐式内置了一个父类对象.所以说,为了那个内置的父类 ...

  10. 理解 LDA 主题模型

    前言 gamma函数 0 整体把握LDA 1 gamma函数 beta分布 1 beta分布 2 Beta-Binomial 共轭 3 共轭先验分布 4 从beta分布推广到Dirichlet 分布 ...