一 获取DateTimeFormatter对象的三种方式

  • 直接使用静态常量创建DateTimeFormatter格式器
  • 使用代码不同风格的枚举值来创建DateTimeFormatter格式器
  • 根据模式字符串来创建DateTimeFormatter格式器

二 DateTimeFormatter完成格式化
1 代码示例

package com.sf.code.concurrent;

import java.time.*;
import java.time.format.*; public class NewFormatterTest {
public static void main(String[] args) {
DateTimeFormatter[] formatters = new DateTimeFormatter[] {
// 直接使用常量创建DateTimeFormatter格式器
DateTimeFormatter.ISO_LOCAL_DATE, DateTimeFormatter.ISO_LOCAL_TIME,
DateTimeFormatter.ISO_LOCAL_DATE_TIME,
// 使用本地化的不同风格来创建DateTimeFormatter格式器
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.MEDIUM),
DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG),
// 根据模式字符串来创建DateTimeFormatter格式器
DateTimeFormatter.ofPattern("Gyyyy%%MMM%%dd HH:mm:ss"),
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") };
LocalDateTime date = LocalDateTime.now();
// 依次使用不同的格式器对LocalDateTime进行格式化
for (int i = 0; i < formatters.length; i++) {
// 下面两行代码的作用相同
System.out.println(date.format(formatters[i]));
// System.out.println(formatters[i].format(date));
}
}
}

2017-03-30
16:14:41.748
2017-03-30T16:14:41.748
2017年3月30日 星期四 16:14:41
下午04时14分41秒
公元2017%%三月%%30 16:14:41
2017-03-30 16:14:41

 

3 代码说明

上面代码使用3种方式创建了6个DateTimeFormatter对象,然后程序中使用不同方式来格式化日期。

三 DateTimeFormatter解析字符串
1 代码示例

package com.sf.code.concurrent;

import java.time.*;
import java.time.format.*; public class NewFormatterParse {
public static void main(String[] args) {
// 定义一个任意格式的日期时间字符串
String str1 = "2014==04==12 01时06分09秒";
// 根据需要解析的日期、时间字符串定义解析所用的格式器
DateTimeFormatter fomatter1 = DateTimeFormatter.ofPattern("yyyy==MM==dd HH时mm分ss秒");
// 执行解析
LocalDateTime dt1 = LocalDateTime.parse(str1, fomatter1);
System.out.println(dt1); // 输出 2014-04-12T01:06:09
// ---下面代码再次解析另一个字符串---
String str2 = "2014$$$四月$$$13 20小时";
DateTimeFormatter fomatter2 = DateTimeFormatter.ofPattern("yyy$$$MMM$$$dd HH小时");
LocalDateTime dt2 = LocalDateTime.parse(str2, fomatter2);
System.out.println(dt2); // 输出 2014-04-13T20:00
}
}

2 运行结果

2014-04-12T01:06:09
2014-04-13T20:00

3 代码说明

上面代码定义了两个不同格式日期、时间字符串。为了解析他们,代码分别使用对应的格式字符串创建了DateTimeFormatter对象,这样DateTimeFormatter即可按照格式化字符串将日期、时间字符串解析成LocalDateTime对象。

Java 8新增的日期、时间格式器的更多相关文章

  1. JDK8 新增的日期时间API

    背景 JDK8中增加了一套全新的日期时间API,这里进行总结下,方便查询使用. 新的时间及日期API位于 java.time 包中,下面是一些关键类. Instant:代表的是时间戳. LocalDa ...

  2. 详解 JDK8 新增的日期时间类

    JDK8 新增的日期时间类 在本人之前的博文<处理时间的类 -- System类.Date类 .SimpleDateFormat类 与 Calendar类>中,讲到过表示时间的类,有三类: ...

  3. 全网最全!彻底弄透Java处理GMT/UTC日期时间

    目录 前言 本文提纲 版本约定 正文 Date类型实现 时区/偏移量TimeZone 设置默认时区 让人恼火的夏令时 Date时区无关性 读取字符串为Date类型 SimpleDateFormat格式 ...

  4. 一起Polyfill系列:让Date识别ISO 8601日期时间格式

    一.什么是ISO 8601日期时间格式 ISO 8601是国际标准化组织制定的日期时间表示规范,全称是<数据存储和交换形式·信息交换·日期和时间的表示方法>. 示例: 1. 2014-12 ...

  5. Android日期时间格式国际化

    公共类 的DateFormatSymbols 扩展对象 实现 Serializable接口 Cloneable接口 java.lang.Object的    ↳ java.text.DateForma ...

  6. Eclipse 改动凝视的 date time 日期时间格式,即${date}变量格式

    Eclipse 改动凝视的 date time 日期时间格式,即${date}变量格式 找到eclipse安装文件夹以下的plugins文件夹,搜索 org.eclipse.text ,找到一个jar ...

  7. python 日期、时间处理,各种日期时间格式/字符串之间的相互转换究竟是怎样的?

    模块函数说明 ''' date 日期对象,常用的属性有year,month,day time 时间对象,常用的属性有hour,minute,second,毫秒 datetime 日期时间对象,常用的属 ...

  8. db2 日期时间格式

    db2日期和时间常用汇总 1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2 ...

  9. SQL Server日期时间格式转换字符串详解 (详询请加qq:2085920154)

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  10. SQL Server日期时间格式转换字符串

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

随机推荐

  1. [RK3288][Android6.0] 调试笔记 --- user版本默认显示开发者选项【转】

    本文转载自:https://blog.csdn.net/kris_fei/article/details/70157137 Platform: ROCKCHIPOS: Android 6.0Kerne ...

  2. dfs枚举

    深度优先搜索(DFS,Depth-First Search)是搜索手段之一.它从某个状态开始,不断的转移状态知道无法转移,然后退回到前一步的状态,继续转移到其他状态,如此不断重复,直到找到最终的解. ...

  3. 基于jetty镜像的ossfs镜像docker镜像构建

    阿里云ossfs:https://help.aliyun.com/document_detail/32196.html?spm=5176.product31815.6.514.yVI0xM 以上是阿里 ...

  4. Android 报错Android - Performing stop of activity that is not resumed

    [原文] FROM STACKOVERFLOW: Just giving my 50 cents on the issue. Catching the exception is indeed one ...

  5. JavaWeb -- sevlet 监听器

    1.三个域对象的监听(创建和销毁) servletContext,  session, request 监听器 ServletContext监听器: public class MyServletCon ...

  6. shell学习之杂项

    ? 表示任意一个字符. > 重写 >> 追加 &> 将错误信息一并写入 Ctrl+Z 暂停 fg 恢复 jobs 查看所有已暂停任务 bg 丢到后台 env 查看系统环 ...

  7. DelphiXE

    1. http://blog.csdn.net/tp26021340/article/details/45953669 2. 3. 4. 5.

  8. [转载]JDBC读写Oracle的CLOB、BLOB

    JDBC读写Oracle10g的CLOB.BLOB http://lavasoft.blog.51cto.com/62575/321882/ 在Oracle中存取BLOB对象实现文件的上传和下载 ht ...

  9. 第一个Python程序hello.py提示出现File "<stdin>",line 1错误

    写第一个Python程序hello.py,内容仅有一句,print 'hello world', 运行 Python hello.py 出错,提示: File "<stdin>& ...

  10. mysql数据库优化课程---5、要索引和不要索引的区别是什么

    mysql数据库优化课程---5.要索引和不要索引的区别是什么 一.总结 一句话总结: 索引速度快,就是查表的时候,操作的话设置索引就好了 1.数据库设计的时候不允许字段为null的好处是什么? nu ...