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. 在浏览器判断是否安装app,并打开相应的页面

    1.代码功能: 判断手机/平板是否安装app 如果安装 则调用app的scheme,传入url当作参数,来做后续操作 如果没有安装 则跳转到app store/google play 下载app 2. ...

  2. C# delegate (1)

    delegate是委托,本身不能解决跨线程访问控件的问题,直接调用委托还是会报错 Invoke指定用主线程中的控件去调用这个委托,相当于主线程来执行这个函数 在codeProject网站中找到的. C ...

  3. java线程同步实的现方式

    为何要使用同步? java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查), 将会导致数据不准确,相互之间产生冲突,因此加入同步锁以避免在该线程没有完成操作之前,被其他 ...

  4. Java 基础【14】@注解

    1.注解简介 JDK 1.5 中引入的 java.lang.annotation 包提供注解编程支持,可以让类在编译.类加载.运行时被读取,并执行相应的处理. 在 Java EE应用的时候,总是免不了 ...

  5. oracle 12c 官方文档 及软件下载

    oracle 12c 官方文档 http://www.oracle.com/pls/db121/homepage oracle 12c 软件下载 http://www.oracle.com/techn ...

  6. Tomcat 的 JDBC 连接池

    JDBC 连接池 org.apache.tomcat.jdbc.pool 是 Apache Commons DBCP 连接池的一种替换或备选方案. 那究竟为何需要一个新的连接池? 原因如下: Comm ...

  7. CPU profiling

    http://gernotklingler.com/blog/gprof-valgrind-gperftools-evaluation-tools-application-level-cpu-prof ...

  8. [转]SVN更新的时候前面字母的意思(U、G、A、R、C)

    U:update 表示从服务器收到文件更新了 G:表示本地文件以及服务器文件都已更新,而且成功的合并了 其他的如下: A:added 表示有文件或者目录添加到工作目录 R:replace 表示文件或者 ...

  9. windows用户态程序的Dump

    熟悉Linux的开发人员都知道,在Linux下开发程序,如果程序崩溃了,可以通过配置Core Dump,来让程序崩溃的瞬间产生一个Dump文件,然后通过dump文件来调试程序为什么崩溃.但是windo ...

  10. 阿里云设置CDN加速访问OSS文件

    快速配置OSS:https://help.aliyun.com/document_detail/31885.html?spm=5176.doc31886.6.97.8iuJo5 快速配置CDN:htt ...