Java的日期与时间(十六)java.time.format.DateTimeFormatter
来源:互联网 发布:5g网络基站建设 编辑:程序博客网 时间:2024/05/21 09:57
Java的日期与时间
原文链接
作者:Jakob Jenkov
译者:阿为
目录:http://blog.csdn.net/tjgykhulj/article/details/68952451
(所有译者备注将以此形式出现,删除线表示有争议或不明确的地方
)
DateTimeFormatter类是Java 8中日期时间功能里,用于解析和格式化日期时间的类,位于java.time.format
包下。
预定义的DateTimeFormatter实例
DateTimeFormatter类包含一系列预定义(常量)的实例,可以解析和格式化一些标准时间格式。这将让你免除麻烦的时间格式定义,类中包含如下预定义的实例:
BASIC_ISO_DATEISO_LOCAL_DATEISO_LOCAL_TIMEISO_LOCAL_DATE_TIMEISO_OFFSET_DATEISO_OFFSET_TIMEISO_OFFSET_DATE_TIMEISO_ZONED_DATE_TIMEISO_INSTANTISO_DATEISO_TIMEISO_DATE_TIMEISO_ORDINAL_TIMEISO_WEEK_DATERFC_1123_DATE_TIME
每个预定义的DateTimeFormatter实例都有不同的日期格式,我就不解释全部的了。具体的可以查阅Java官方文档,但我在这篇的后续中会解释其中几个,以方便理解。
格式化日期
当你获取一个DateTimeFormatter实例后,就可以用format()方便来将一个日期格式化为某种字符串,例如:
DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;String formattedDate = formatter.format(LocalDate.now());System.out.println(formattedDate);
这个样例把LocalDate对象格式化了,并输出20150703,这个输出表示现在2015年,7月3日。
再举一个关于ZonedDateTime的例子:
DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;String formattedZonedDate = formatter.format(ZonedDateTime.now());System.out.println("formattedZonedDate = " + formattedZonedDate);
这个例子会输出:20150703+0200
表示今年2015年,7月3日,位于UTC+2时区。
1 0
- Java的日期与时间(十六)java.time.format.DateTimeFormatter
- Java的日期与时间(十)java.time.Instant
- Java的日期与时间(十一)java.time.Duration
- Java的日期与时间(十二)java.time.LocalDate
- Java的日期与时间(十三)java.time.LocalTime
- Java的日期与时间(十四)java.time.LocalDateTime
- Java的日期与时间(十五)java.time.ZonedDateTime
- Java日期时间(Date/Time)
- java.time时间/日期API
- java.time时间/日期API
- Java日期时间(Date/Time)
- Java日期时间(Date/Time)
- java 8 LocalDateTime,DateTimeFormatter
- Java的日期与时间(目录)
- 替代Java类的日期和时间【 Joda Time】
- 替代Java类的日期和时间【 Joda Time】
- Java的日期与时间(三)计算运行时间
- java.time.format.DateTimeParseException
- Vim插件之vim-cpp-enhanced-highlight
- 和为K的组合
- PL/SQL开发基础知识
- Java的日期与时间(十五)java.time.ZonedDateTime
- nodejs里mysql的相关操作
- Java的日期与时间(十六)java.time.format.DateTimeFormatter
- hdu 4911求逆序数
- Codeforces Round #306 (Div. 2)
- NOI2015品酒大会 后缀数组
- [log4j]org.apache.log4j.Logger用法
- MFC学习笔记
- 关于面向切面编程的个人理解
- hdu 4920矩阵连乘
- C#基础数据类型