Java 8新增的日期、时间格式器
一 获取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新增的日期、时间格式器的更多相关文章
- JDK8 新增的日期时间API
背景 JDK8中增加了一套全新的日期时间API,这里进行总结下,方便查询使用. 新的时间及日期API位于 java.time 包中,下面是一些关键类. Instant:代表的是时间戳. LocalDa ...
- 详解 JDK8 新增的日期时间类
JDK8 新增的日期时间类 在本人之前的博文<处理时间的类 -- System类.Date类 .SimpleDateFormat类 与 Calendar类>中,讲到过表示时间的类,有三类: ...
- 全网最全!彻底弄透Java处理GMT/UTC日期时间
目录 前言 本文提纲 版本约定 正文 Date类型实现 时区/偏移量TimeZone 设置默认时区 让人恼火的夏令时 Date时区无关性 读取字符串为Date类型 SimpleDateFormat格式 ...
- 一起Polyfill系列:让Date识别ISO 8601日期时间格式
一.什么是ISO 8601日期时间格式 ISO 8601是国际标准化组织制定的日期时间表示规范,全称是<数据存储和交换形式·信息交换·日期和时间的表示方法>. 示例: 1. 2014-12 ...
- Android日期时间格式国际化
公共类 的DateFormatSymbols 扩展对象 实现 Serializable接口 Cloneable接口 java.lang.Object的 ↳ java.text.DateForma ...
- Eclipse 改动凝视的 date time 日期时间格式,即${date}变量格式
Eclipse 改动凝视的 date time 日期时间格式,即${date}变量格式 找到eclipse安装文件夹以下的plugins文件夹,搜索 org.eclipse.text ,找到一个jar ...
- python 日期、时间处理,各种日期时间格式/字符串之间的相互转换究竟是怎样的?
模块函数说明 ''' date 日期对象,常用的属性有year,month,day time 时间对象,常用的属性有hour,minute,second,毫秒 datetime 日期时间对象,常用的属 ...
- db2 日期时间格式
db2日期和时间常用汇总 1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2 ...
- SQL Server日期时间格式转换字符串详解 (详询请加qq:2085920154)
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
- SQL Server日期时间格式转换字符串
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
随机推荐
- eclipse 中英文切换
第一种方法: 在Eclipse安装目录下找到它的配置文件"eclipse.ini",用UE或者EditPlus等工具打开该配置文件,截图显示如下: 然后在最后一行添加如下相应命令: ...
- Centos6.5使用yum安装软件的时候 Another app is currently holding the yum lock; waiting for it to exit...
Loaded plugins: fastestmirror, refresh-packagekit, security Existing . Another app is currently hold ...
- Spark总结1
安装jdk 下载spark安装包 解压 重点来了: 配置 spark: 进入 conf ----> spark-env.sh.template文件 cd conf/ mv spark-env ...
- mysql删除重复记录
Solution 1: Add Unique Index on your table: ALTER IGNORE TABLE `TableA` ADD UNIQUE INDEX (`member_id ...
- 分布式技术 memcached
memcached 是一个高性能的分布式内存对象缓存系统,用于动态web应用,以减轻数据库负载,它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.memcache ...
- Qt QSortFilterProxyModel示例代码, 使用方法
1. QSortFilterProxyModel不能单独使用,它只是一个"代理",真正的数据需要另外的一个model提供,而且它是用来排序和过滤的. 2. 实现代码 #ifndef ...
- BZOJ3244/UOJ122 [Noi2013]树的计数
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- tiff/tfw, jpg/jpgw坐标文件的格式(6个参数)
tiff/tfw, jpg/jpgw坐标文件的格式(6个参数) 0.100-0.13999904400510 以上每行对应的含义: 1 地图单元中的一个象素在X方向上的X分辨率尺度. 2 平移量. 3 ...
- 智课雅思词汇---二十一、名词性后缀acity是什么意思
智课雅思词汇---二十一.名词性后缀acity是什么意思 一.总结 一句话总结:后缀:-acity [名词后缀] 构成抽象名词,表示性质.状态.情况.与形容词后缀-acious相对应 rapacity ...
- 英语发音规则---U字母-[复习中]
英语发音规则---U字母 一.总结 一句话总结:(注:本文所有//的音标为英音音标,[]的音标为美音音标) 1.U在开音节中发[ju ]/ ju: /? duty /'djuːtɪ/ ['dʊti] ...