java 解析日期格式

自Java 几乎 开始以来,Java开发人员就通过java.util.Date类(自JDK 1.0起)和java.util.Calendar类(自JDK 1.1起 )来处理日期和时间。 在这段时间内,成千上万(甚至数百万)的Java开发人员已使用java.text.DateFormat和java.text.SimpleDateFormat格式化并解析了Java日期和时间。 鉴于多年来这样做的频率,不足为奇的是,有很多关于这些类的日期和时间的解析和格式设置的 在线 示例和教程 。 经典的Java教程在“ 格式设置”课程 ( Dates and Times )中介绍了这些java.util和java.text类。 Java教程中新的Date Time路径涵盖了Java 8中有关日期和时间以及它们的格式和解析的新类。 这篇文章提供了一些实际的例子。

在通过示例演示Java 8样式的日期/时间解析/格式化之前,先比较一下DateFormat / SimpleDateFormat和DateTimeFormatter的Javadoc描述。 下表包含可区分的信息,这些信息可通过比较每个格式类的Javadoc来直接或间接收集。 从此表中可能最重要的观察结果是,新的DateTimeFormatter是线程安全的且不可变的,并且DateTimeFormatter提供了用于解析和格式化日期和时间的API的概述。

特性 DateFormat / SimpleDateFormat DateTimeFormatter
目的 “以与语言无关的方式格式化和解析日期或时间” “用于打印和解析日期时间对象的格式化程序。”
主要用于 java.util.Date
java.util.Calendar
java.time.LocalDate
java.time.LocalTime
java.time.LocalDateTime java.time.OffsetTime java.time.OffsetDateTime java.time.ZonedDateTime java.time.Instant
线程安全 “日期格式不同步。” “此类是不可变的并且是线程安全的。”
直接格式化 格式(日期) 格式(TemporalAccessor)
直接解析 parse(String) 解析(CharSequence,TemporalQuery)
间接格式化 无[除非您使用Groovy的Date.format(String)扩展名) LocalDate.format(DateTimeFormatter)
LocalTime.format(DateTimeFormatter)
LocalDateTime.format(DateTimeFormatter) OffsetTime.format(DateTimeFormatter) OffsetDateTime.format(DateTimeFormatter) ZonedDateTime.format(DateTimeFormatter)
间接解析 无[除非您使用不推荐使用的Date.parse(String)或Groovy的Date.parse(String,String)扩展名] LocalDate.parse(CharSequence,DateTimeFormatter)
LocalTime.parse(CharSequence,DateTimeFormatter)
LocalDateTime.parse(CharSequence,DateTimeFormatter) OffsetTime.parse(CharSequence,DateTimeFormatter) OffsetDateTime.parse(CharSequence,DateTimeFormatter) ZonedDateTime.parse(CharSequence,DateTimeFormatter)
国际化 java.util.Locale java.util.Locale
时区 java.util.TimeZone java.time.ZoneId
java.time.ZoneOffset
预定义格式器 没有,但是为常见实例提供了静态便利方法:
getDateInstance()
getDateInstance(int) getDateInstance(int,Locale) getDateTimeInstance() getDateTimeInstance(int,int) getDateTimeInstance(int,int,Locale) getInstance() getTimeInstance() getTimeInstance(int) getTimeInstance(int,Locale)
ISO_LOCAL_DATE
ISO_LOCAL_TIME
ISO_LOCAL_DATE_TIME ISO_OFFSET_DATE ISO_OFFSET_TIME ISO_OFFSET_DATE_TIME ISO_ZONED_DATE_TIME BASIC_ISO_DATE ISO_DATE ISO_DATE_TIME ISO_ORDINAL_DATE ISO_INSTANT ISO_WEEK_DATE RFC_1123_DATE_TIME

这篇文章的其余部分将使用示例演示如何使用java.time构造在Java 8中格式化和解析日期。 这些示例将使用以下字符串模式和实例。

/** Pattern to use for String representation of Dates/Times. */
private final String dateTimeFormatPattern = "yyyy/MM/dd HH:mm:ss z";/*** java.util.Date instance representing now that can* be formatted using SimpleDateFormat based on my* dateTimeFormatPattern field.*/
private final Date now = new Date();/*** java.time.ZonedDateTime instance representing now that can* be formatted using DateTimeFormatter based on my* dateTimeFormatPattern field.** Note that ZonedDateTime needed to be used in this example* instead of java.time.LocalDateTime or java.time.OffsetDateTime* because there is zone information in the format provided by* my dateTimeFormatPattern field and attempting to have* DateTimeFormatter.format(TemporalAccessor) instantiated* with a format pattern that includes time zone details* will lead to DateTimeException for instances of* TemporalAccessor that do not have time zone information* (such as LocalDateTime and OffsetDateTime).*/
private final ZonedDateTime now8 = ZonedDateTime.now();/*** String that can be used by both SimpleDateFormat and* DateTimeFormatter to parse respective date/time instances* from this String.*/
private final String dateTimeString = "2014/09/03 13:59:50 MDT";

在Java 8之前,用于日期和时间的标准Java方法是通过Date和Calendar类,而用于解析和格式化日期的标准方法是通过DateFormat和SimpleDateFormat 。 下一个代码清单演示了这些经典方法。

使用SimpleDateFormat格式化和解析Java日期

/*** Demonstrate presenting java.util.Date as String matching* provided pattern via use of SimpleDateFormat.*/
public void demonstrateSimpleDateFormatFormatting()
{final DateFormat format = new SimpleDateFormat(dateTimeFormatPattern);final String nowString = format.format(now);out.println("Date '" + now + "' formatted with SimpleDateFormat and '"+ dateTimeFormatPattern + "': " + nowString);
}/*** Demonstrate parsing a java.util.Date from a String* via SimpleDateFormat.*/
public void demonstrateSimpleDateFormatParsing()
{final DateFormat format = new SimpleDateFormat(dateTimeFormatPattern);try{final Date parsedDate = format.parse(dateTimeString);out.println("'" + dateTimeString + "' is parsed with SimpleDateFormat as " + parsedDate);}// DateFormat.parse(String) throws a checked exceptioncatch (ParseException parseException){out.println("ERROR: Unable to parse date/time String '"+ dateTimeString + "' with pattern '"+ dateTimeFormatPattern + "'.");}
}

在Java 8中,首选日期/时间类不再位于java.util包中,并且首选日期/时间处理类现在位于java.time包中。 同样,首选的日期/时间格式/解析类不再位于java.text包中,而是来自java.time.format包。

java.time包提供了许多用于对日期和/或时间进行建模的类。 这些包括仅对日期建模的类(无时间信息),仅对时间建模的类(无日期信息),对日期和时间信息进行建模的类,使用时区信息的类以及不包含时区信息的类。 尽管类的特性(例如,是否支持日期或时间或时区信息)会影响可以应用的模式,但格式化和解析这些方法的方法通常是相似的。 在本文中,我将ZonedDateTime类用作示例。 之所以选择该选项,是因为它包含日期,时间和时区信息,并允许我使用一种匹配模式,该模式涉及所有三个特征,例如Date或Calendar实例。 这使得比较新旧方法变得更加容易。

DateTimeFormatter类提供了ofPattern方法,以基于所提供的日期/时间模式String提供DateTimeFormatter的实例。 然后可以在该DateTimeFormatter实例上调用一种格式方法,以获取日期和/或时间信息,格式为与提供的模式匹配的String。 下一个代码清单说明了基于提供的模式从ZonedDateTime格式化String这种方法。

将ZonedDateTime格式化为字符串

/*** Demonstrate presenting ZonedDateTime as a String matching* provided pattern via DateTimeFormatter and its* ofPattern(String) method.*/
public void demonstrateDateTimeFormatFormatting()
{final DateTimeFormatter formatter =DateTimeFormatter.ofPattern(dateTimeFormatPattern);final String nowString = formatter.format(now8);out.println(now8 + " formatted with DateTimeFormatter and '"+ dateTimeFormatPattern + "': " + nowString);
}

基于模式从字符串中解析日期/时间类很容易完成。 有两种方法可以实现。 一种方法是将DateTimeFormatter的实例传递给静态ZonedDateTime.parse(CharSequence,DateTimeFormatter)方法,该方法返回从提供的字符序列派生并基于提供的模式的ZonedDateTime实例。 下一个代码清单对此进行了说明。

使用静态ZonedDateTime.parse方法从字符串解析ZonedDateTime

/*** Demonstrate parsing ZonedDateTime from provided String* via ZonedDateTime's parse(String, DateTimeFormatter) method.*/
public void demonstrateDateTimeFormatParsingTemporalStaticMethod()
{final DateTimeFormatter formatter =DateTimeFormatter.ofPattern(dateTimeFormatPattern);final ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeString, formatter);out.println("'" + dateTimeString+ "' is parsed with DateTimeFormatter and ZonedDateTime.parse as "+ zonedDateTime);
}

从字符串解析ZonedDateTime第二种方法是通过DateTimeFormatter的parse(CharSequence,TemporalQuery <T>)方法。 在下面的代码清单中对此进行了说明,该清单也提供了演示Java 8 方法引用的用法的机会(请参见ZonedDateTime::from )。

使用DateTimeFormatter.parse方法从字符串解析ZonedDateTime

/*** Demonstrate parsing ZonedDateTime from String* via DateTimeFormatter.parse(String, TemporaryQuery)* with the Temple Query in this case being ZonedDateTime's* from(TemporalAccessor) used as a Java 8 method reference.*/
public void demonstrateDateTimeFormatParsingMethodReference()
{final DateTimeFormatter formatter =DateTimeFormatter.ofPattern(dateTimeFormatPattern);final ZonedDateTime zonedDateTime = formatter.parse(dateTimeString, ZonedDateTime::from);out.println("'" + dateTimeString+ "' is parsed with DateTimeFormatter and ZoneDateTime::from as "+ zonedDateTime);
}

很少有项目可以作为可以从Java 8开始的未开发项目。因此,将JDK 8之前的日期/时间类与JDK 8中引入的新日期/时间类连接在一起的类将很有帮助。例如,JDK 8的DateTimeFormatter可以通过DateTimeFormatter.toFormat()方法提供JDK 8之前的抽象Format类的降序实例。 下一个代码清单中对此进行了演示。

从JDK 8的DateTimeFormatter访问JDK 8之前的格式

/*** Demonstrate formatting ZonedDateTime via DateTimeFormatter,* but using implementation of Format.*/
public void demonstrateDateTimeFormatAndFormatFormatting()
{final DateTimeFormatter formatter =DateTimeFormatter.ofPattern(dateTimeFormatPattern);final Format format = formatter.toFormat();final String nowString = format.format(now8);out.println("ZonedDateTime " + now + " formatted with DateTimeFormatter/Format (and "+ format.getClass().getCanonicalName() + ") and '"+ dateTimeFormatPattern + "': " + nowString);
}

该即时与两个前JDK 8工作时类是特别重要的DateCalendar结合类与JDK 8之所以引入新的日期和时间类Instant是如此重要的是, java.util.Date有方法从(即时)和toInstant()分别从Instant获取Date和从Date获取Instant 。 因为Instant在将Java 8之前的日期/时间处理迁移到Java 8基线非常重要,所以下一个代码清单演示了Instant实例的格式和解析。

格式化和解析Instant实例

/*** Demonstrate formatting and parsing an instance of Instant.*/
public void demonstrateDateTimeFormatFormattingAndParsingInstant()
{// Instant instances don't have timezone informationfinal Instant instant = now.toInstant();final DateTimeFormatter formatter =DateTimeFormatter.ofPattern(dateTimeFormatPattern).withZone(ZoneId.systemDefault());final String formattedInstance = formatter.format(instant);out.println("Instant " + instant + " formatted with DateTimeFormatter and '"+ dateTimeFormatPattern + "' to '" + formattedInstance + "'");final Instant instant2 =formatter.parse(formattedInstance, ZonedDateTime::from).toInstant();out.println(formattedInstance + " parsed back to " + instant2);
}

为了完整起见,所有上述示例均来自下一个代码清单中显示的示例类。

DateFormatDemo.java

package dustin.examples.numberformatdemo;import static java.lang.System.out;import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;/*** Demonstrates formatting dates as strings and parsing strings* into dates and times using pre-Java 8 (java.text.SimpleDateFormat)* and Java 8 (java.time.format.DateTimeFormatter) mechanisms.*/
public class DateFormatDemo
{/** Pattern to use for String representation of Dates/Times. */private final String dateTimeFormatPattern = "yyyy/MM/dd HH:mm:ss z";/*** java.util.Date instance representing now that can* be formatted using SimpleDateFormat based on my* dateTimeFormatPattern field.*/private final Date now = new Date();/*** java.time.ZonedDateTime instance representing now that can* be formatted using DateTimeFormatter based on my* dateTimeFormatPattern field.** Note that ZonedDateTime needed to be used in this example* instead of java.time.LocalDateTime or java.time.OffsetDateTime* because there is zone information in the format provided by* my dateTimeFormatPattern field and attempting to have* DateTimeFormatter.format(TemporalAccessor) instantiated* with a format pattern that includes time zone details* will lead to DateTimeException for instances of* TemporalAccessor that do not have time zone information* (such as LocalDateTime and OffsetDateTime).*/private final ZonedDateTime now8 = ZonedDateTime.now();/*** String that can be used by both SimpleDateFormat and* DateTimeFormatter to parse respective date/time instances* from this String.*/private final String dateTimeString = "2014/09/03 13:59:50 MDT";/*** Demonstrate presenting java.util.Date as String matching* provided pattern via use of SimpleDateFormat.*/public void demonstrateSimpleDateFormatFormatting(){final DateFormat format = new SimpleDateFormat(dateTimeFormatPattern);final String nowString = format.format(now);out.println("Date '" + now + "' formatted with SimpleDateFormat and '"+ dateTimeFormatPattern + "': " + nowString);}/*** Demonstrate parsing a java.util.Date from a String* via SimpleDateFormat.*/public void demonstrateSimpleDateFormatParsing(){final DateFormat format = new SimpleDateFormat(dateTimeFormatPattern);try{final Date parsedDate = format.parse(dateTimeString);out.println("'" + dateTimeString + "' is parsed with SimpleDateFormat as " + parsedDate);}// DateFormat.parse(String) throws a checked exceptioncatch (ParseException parseException){out.println("ERROR: Unable to parse date/time String '"+ dateTimeString + "' with pattern '"+ dateTimeFormatPattern + "'.");}}/*** Demonstrate presenting ZonedDateTime as a String matching* provided pattern via DateTimeFormatter and its* ofPattern(String) method.*/public void demonstrateDateTimeFormatFormatting(){final DateTimeFormatter formatter =DateTimeFormatter.ofPattern(dateTimeFormatPattern);final String nowString = formatter.format(now8);out.println(now8 + " formatted with DateTimeFormatter and '"+ dateTimeFormatPattern + "': " + nowString);}/*** Demonstrate parsing ZonedDateTime from provided String* via ZonedDateTime's parse(String, DateTimeFormatter) method.*/public void demonstrateDateTimeFormatParsingTemporalStaticMethod(){final DateTimeFormatter formatter =DateTimeFormatter.ofPattern(dateTimeFormatPattern);final ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeString, formatter);out.println("'" + dateTimeString+ "' is parsed with DateTimeFormatter and ZonedDateTime.parse as "+ zonedDateTime);}/*** Demonstrate parsing ZonedDateTime from String* via DateTimeFormatter.parse(String, TemporaryQuery)* with the Temple Query in this case being ZonedDateTime's* from(TemporalAccessor) used as a Java 8 method reference.*/public void demonstrateDateTimeFormatParsingMethodReference(){final DateTimeFormatter formatter =DateTimeFormatter.ofPattern(dateTimeFormatPattern);final ZonedDateTime zonedDateTime = formatter.parse(dateTimeString, ZonedDateTime::from);out.println("'" + dateTimeString+ "' is parsed with DateTimeFormatter and ZoneDateTime::from as "+ zonedDateTime);}/*** Demonstrate formatting ZonedDateTime via DateTimeFormatter,* but using implementation of Format.*/public void demonstrateDateTimeFormatAndFormatFormatting(){final DateTimeFormatter formatter =DateTimeFormatter.ofPattern(dateTimeFormatPattern);final Format format = formatter.toFormat();final String nowString = format.format(now8);out.println("ZonedDateTime " + now + " formatted with DateTimeFormatter/Format (and "+ format.getClass().getCanonicalName() + ") and '"+ dateTimeFormatPattern + "': " + nowString);}/*** Demonstrate formatting and parsing an instance of Instant.*/public void demonstrateDateTimeFormatFormattingAndParsingInstant(){// Instant instances don't have timezone informationfinal Instant instant = now.toInstant();final DateTimeFormatter formatter =DateTimeFormatter.ofPattern(dateTimeFormatPattern).withZone(ZoneId.systemDefault());final String formattedInstance = formatter.format(instant);out.println("Instant " + instant + " formatted with DateTimeFormatter and '"+ dateTimeFormatPattern + "' to '" + formattedInstance + "'");final Instant instant2 =formatter.parse(formattedInstance, ZonedDateTime::from).toInstant();out.println(formattedInstance + " parsed back to " + instant2);}/*** Demonstrate java.text.SimpleDateFormat and* java.time.format.DateTimeFormatter.** @param arguments Command-line arguments; none anticipated.*/public static void main(final String[] arguments){final DateFormatDemo demo = new DateFormatDemo();out.print("\n1: ");demo.demonstrateSimpleDateFormatFormatting();out.print("\n2: ");demo.demonstrateSimpleDateFormatParsing();out.print("\n3: ");demo.demonstrateDateTimeFormatFormatting();out.print("\n4: ");demo.demonstrateDateTimeFormatParsingTemporalStaticMethod();out.print("\n5: ");demo.demonstrateDateTimeFormatParsingMethodReference();out.print("\n6: ");demo.demonstrateDateTimeFormatAndFormatFormatting();out.print("\n7: ");demo.demonstrateDateTimeFormatFormattingAndParsingInstant();}
}

下一个屏幕快照中显示了运行上述演示的输出。

结论

与JDK 8之前的版本相比,JDK 8日期/时间类以及相关的格式和解析类更易于使用。 这篇文章试图演示如何应用这些新类并利用它们的某些优点。

翻译自: https://www.javacodegeeks.com/2014/09/datetime-formattingparsing-java-8-style.html

java 解析日期格式

java 解析日期格式_日期/时间格式/解析,Java 8样式相关推荐

  1. asp.net站点时间格式与系统时间格式不一致。手动修改

    asp.net站点时间格式与系统时间格式不匹配时,当修改系统时间格式,站点时间可能还是旧的格式.则可通过修改注册表更新时间格式. 1.开始-运行-输入regedit,依次找到HKEY-Users\.D ...

  2. html fmt转换日期格式,js通用时间格式转换函数

    js通用时间格式转换函数 //使用函数的方式进行定义 function dateFormat(fmt,date){ var o = { "M+" : date.getMonth() ...

  3. java定义时间格式大全_Java时间格式转换大全

    package com.date; import java.text.DateFormat; import java.text.ParseException; import java.text.Par ...

  4. java时间格式大全_java时间格式大全

    java.util.*; import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...

  5. oracle新增时间格式转换,oracle时间格式转换_oracle时间类型转换

    间-mysql 时间转换字符串|oracle时间格式转换| 428x593 - 59KB - JPEG oracle中to_date详细用法示例(oracle日期格式转 616x795 - 86KB ...

  6. oracle 日期排序_日期居然用字符串保存?我笑了

    点击上方"后端技术精选",选择"置顶公众号" 技术文章第一时间送达! 作者:张乘辉 来自:后端进阶 我发现数据库有些日期居然用字符串保存?于是跟几个小伙伴讨论了 ...

  7. C#把月日年帶有 AM、PM的时间格式转换为正常时间格式

    如何把时间: 5/31/21 12:00:00 AM (PM下午.AM上午)不是字符串格式的日期 转成 2021/5/31 12:00:00 这个不时间区域造成, 12小时制和24小时. System ...

  8. C# 本地时间格式,UTC时间格式,GMT时间格式处理

    一.本地时间显示 DateTime now = DateTime.Now; //本地时间格式 Console.WriteLine(now.ToLongDateString()); Console.Wr ...

  9. java解析dxf文件_浅析JVM方法解析、创建和链接

    一:前言 上周末写了一篇文章<你知道Java类是如何被加载的吗?>,分析了HotSpot是如何加载Java类的,干脆趁热打铁,本周末再来分析下Hotspot又是如何解析.创建和链接类方法的 ...

最新文章

  1. POJO、VO、PO、FormBean区别:
  2. Castle IOC容器快速入门
  3. synchronized关键字以及实例锁 类锁
  4. python进阶书籍的推荐-Python 入门到进阶书籍推荐
  5. Spring(19)——Profile(二)
  6. hashmap取值_一万六千字的HashMap深度剖析
  7. SpringCloud教程-消息总线Bus 服务端(server)刷新(SpringCloud版本Greenwich.SR4)
  8. Visual Studio Code支持JUnit
  9. C语言extern与static修饰变量
  10. js html模板生成图片不显示,网页中图片不显示问题
  11. android——根据MVC框架设计的结构
  12. 【深度学习基础】张量的描述与深度学习常见张量
  13. 单表查询 多表查询 子查询
  14. hera(赫拉)任务调度系统--为数据平台打造的任务调度系统
  15. 帮你举例说明什么是Python鸭子类型
  16. Android集成高德地图实现自定义Marker
  17. 图像处理专业术语中英文对照(不断补充中)
  18. 王小锤学Java:retainAll函数那点儿事
  19. python中如何查一个函数的用法_Python常见内置函数用法(三)
  20. 熬夜慢性自杀全过程曝光 | 程序员入坑必读

热门文章

  1. P1352-没有上司的舞会【树形dp】
  2. 【2018.4.7】模拟赛之四-ssl2385 猜数【水题】
  3. C++STL(set……)
  4. [系统安全]使用OD编写连连看外挂
  5. Arthas - 开源 Java 诊断工具
  6. Hadoop的FileSystem.copyToLocalFile两个参数 空指针异常
  7. Oracle入门(五C)之68个系统变量的key和默认value
  8. Maven精选系列--标准目录结构
  9. Linux入门(一)之权限指令系统管理
  10. 建立Vue脚手架的必要性