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日 ...
随机推荐
- Go 书单
一.<Go语言学习笔记> (未找到对应版本的电子书,大家可以去作者github:https://github.com/qyuhen/book) 推荐理由:作为时下流行的一种系统编程语言,G ...
- RDLC 微软报表 导出Excel时产生多个工作表 (worksheet)
. I have added two obejcts data source to Report Viewer. 2. in RDLC i have created two tables and in ...
- window.open、window.showModalDialog和window.showModelessDialog 的区别[转]
一.前言 要打开一个可以载入页面的子窗口有三种方法,分别是window.open.window.showModalDialog和window.showModelessDialog. open方法就是打 ...
- hive学习7(条件函数case)
case函数 语法: CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END 说明:如果a为TRUE,则返回b:如果c为TRUE,则返回d:否则返回e 实例 ...
- lua 写逻辑打印log时,打印到一半后停止不再打印,程序停止
问题描述:ubuntu下用lua开发游戏电子邮件模块,自己测试时向用户推送100封,而用户最多只能有50封.这是调用sysdelete删除一些邮件.当打印log时,打印到一半后程序中途停止.将打印lo ...
- vc 判断当前用户是否在管理员组以及是否SYSTEM权限运行
BOOL IsUserInAdminGroup() //判断是否在管理员组 { BOOL fInAdminGroup = FALSE; HANDLE hToken = NULL; HANDLE hTo ...
- vc 自删除
// delself.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include <windows.h>#include &l ...
- hdoj1003--Max Sum
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...
- C++两种字符串传参构造函数
第一种: #include"iostream" #include"string" using namespace std; class Motor{ prote ...
- 0.00-050613_boot.s
! boot.s ! ! It then loads the system at 0x10000, using BIOS interrupts. Thereafter ! it disables al ...