您的位置:首页技术文章
文章详情页

JAVA 格式化日期、时间的方法

【字号: 日期:2022-08-30 16:51:33浏览:6作者:猪猪

使用 DateFormat 格式化日期、时间

DateFormat 也是一个抽象类,它也提供了如下几个类方法用于获取 DateFormat 对象。

getDateInstance():返回一个日期格式器,它格式化后的字符串只有日期,没有时间。该方法可以传入多个参数,用于指定日期样式和 Locale 等参数;如果不指定这些参数,则使用默认参数。 getTimeInstance():返回一个时间格式器,它格式化后的字符串只有时间,没有日期。该方法可以传入多个参数,用于指定时间样式和 Locale 等参数;如果不指定这些参数,则使用默认参数。 getDateTimeInstance():返回一个日期、时间格式器,它格式化后的字符串既有日期,也有时间。该方法可以传入多个参数,用于指定日期样式、时间样式和 Locale 等参数;如果不指定这些参数,则使用默认参数。

上面三个方法可以指定日期样式、时间样式参数,它们是的4个静态常量:FULL、LONG、MEDIUM 和 SHORT,通过这4个样式参数可以控制生成的格式化字符串。看如下例子程序。

import java.util.*;import java.text.*;import static java.text.DateFormat.*;public class DateFormatTest { public static void main(String[] args) throws ParseException { // 需要被格式化的时间 Date dt = new Date(); // 创建两个Locale,分别代表中国、美国 Locale[] locales = { Locale.CHINA, Locale.US }; DateFormat[] df = new DateFormat[16]; // 为上面两个Locale创建16个DateFormat对象 for (int i = 0; i < locales.length; i++) { df[i * 8] = DateFormat.getDateInstance(SHORT, locales[i]); df[i * 8 + 1] = DateFormat.getDateInstance(MEDIUM, locales[i]); df[i * 8 + 2] = DateFormat.getDateInstance(LONG, locales[i]); df[i * 8 + 3] = DateFormat.getDateInstance(FULL, locales[i]); df[i * 8 + 4] = DateFormat.getTimeInstance(SHORT, locales[i]); df[i * 8 + 5] = DateFormat.getTimeInstance(MEDIUM, locales[i]); df[i * 8 + 6] = DateFormat.getTimeInstance(LONG, locales[i]); df[i * 8 + 7] = DateFormat.getTimeInstance(FULL, locales[i]); } for (int i = 0; i < locales.length; i++) { String tip = i == 0 ? '----中国日期格式----' : '----美国日期格式----'; System.out.println(tip); System.out.println('SHORT格式的日期格式:' + df[i * 8].format(dt)); System.out.println('MEDIUM格式的日期格式:' + df[i * 8 + 1].format(dt)); System.out.println('LONG格式的日期格式:' + df[i * 8 + 2].format(dt)); System.out.println('FULL格式的日期格式:' + df[i * 8 + 3].format(dt)); System.out.println('SHORT格式的时间格式:' + df[i * 8 + 4].format(dt)); System.out.println('MEDIUM格式的时间格式:' + df[i * 8 + 5].format(dt)); System.out.println('LONG格式的时间格式:' + df[i * 8 + 6].format(dt)); System.out.println('FULL格式的时间格式:' + df[i * 8 + 7].format(dt)); } }}

上面程序共创建了16个 DateFormat 对象,分别为中国、美国两个 Locale 各创建8个 DateFormat 对象,分别是 SHORT、MEDIUM、LONG、FULL 四种样式的日期格式器、时间格式器。运行上面程序,会看到如下所示的效果。

----中国日期格式----SHORT格式的日期格式:20-2-11MEDIUM格式的日期格式:2020-2-11LONG格式的日期格式:2020年2月11日FULL格式的日期格式:2020年2月11日 星期二SHORT格式的时间格式:下午2:26MEDIUM格式的时间格式:14:26:21LONG格式的时间格式:下午02时26分21秒FULL格式的时间格式:下午02时26分21秒 CST----美国日期格式----SHORT格式的日期格式:2/11/20MEDIUM格式的日期格式:Feb 11, 2020LONG格式的日期格式:February 11, 2020FULL格式的日期格式:Tuesday, February 11, 2020SHORT格式的时间格式:2:26 PMMEDIUM格式的时间格式:2:26:21 PMLONG格式的时间格式:2:26:21 PM CSTFULL格式的时间格式:2:26:21 PM CST

DateFormat 具有国际化的能力,同一个日期使用不同的 Locale 格式器格式化的效果完全不同,格式化后的字符串正好符合 Locale 对应的本地习惯。

获得了 DateFormat 之后,还可以调用它的 setLenient(boolean lenient) 方法来设置该格式器是否采用严格语法。举例来说,如果采用不严格的日期语法(该方法的参数为true),对于字符串'2004-2-31'将会转换成2004年3月2日:如果采用严格的日期语法,解析该字符串时将抛出异常。

DateFormat 的 parse() 方法可以把一个字符串解析成 Date 对象,但它要求被解析的字符串必须符合日期字符串的要求,否则可能抛出 ParseExcepuon 异常。例如,如下代码片段:

String str1 = '2017/10/07';String str2 = '2017年10月07日';// 下面输出 Sat Oct 07 00:00:00 CST 2017System.out.println(DateFormat.getDateInstance().parse(str2));// 下面输出 Sat Oct 07 00:00:00 CST 2017System.out.println(DateFormat.getDateInstance(SHORT).parse(str1));// 下面抛出 ParseException异常System.out.println(DateFormat.getDateInstance().parse(str1));

上面代码中最后一行代码解析日期字符串时引发 ParseException 异常,因为'2017/10/07'是一个 SHORT 样式的日期字符串,必须用 SHORT 样式的 DateFormat 实例解析,否则将抛出异常。

使用 SimpleDateFormat 格式化日期

前面介绍的 DateFormat 的方法可以把字符串解析成 Date 对象,但实际上 DateFormat 的 parse() 方法不够灵活一一它要求被解析的字符串必须满足特定的格式!为了更好地格式化日期、解析日期字符串,Java 提供了 SimpleDateFormat 类。

SimpleDateFormat 是 DateFormat 的子类,正如它的名字所暗示的,它是“简单”的日期格式器。很多读者对“简单”的日期格式器不屑一顾,实际上 SimpleDateFormat 比 DateFormat 更简单,功能更强大。

SimpleDateFormat 可以非常灵活地格式化 Date,也可以用于解析各种格式的日期字符串。创建 SimpleDateFormat 对象时需要传入一个 pattern 字符串,这个 pattern 不是正则表达式,而是一个日期模板字符串。

import java.text.*;import java.util.*;public class SimpleDateFormatTest { public static void main(String[] args) throws ParseException { Date d = new Date(); // 创建一个SimpleDateFormat对象 SimpleDateFormat sdf1 = new SimpleDateFormat('Gyyyy年中第D天'); // 将d格式化成日期,输出:公元2017年中第282天 String dateStr = sdf1.format(d); System.out.println(dateStr); // 一个非常特殊的日期字符串 String str = '14###3月##21'; SimpleDateFormat sdf2 = new SimpleDateFormat('y###MMM##d'); // 将日期字符串解析成日期,输出:Fri Mar 21 00:00:00 CST 2014 System.out.println(sdf2.parse(str)); }}

从上面程序中可以看出,使用 SimpleDateFormat 可以将日期格式化成形如“公元2014年中第101天”这样的字符串,也可以把形如“14###3月##21”这样的字符串解析成日期,功能非常强大。

SimpleDateFormat 把日期格式化成怎样的字符串,以及能把怎样的字符串解析成 Date,完全取决于创建该对象时指定的 pattern 参数,pattern 是一个使用日期字段占位符的日期模板。

如果读者想知道 SimpleDateFormat 支持哪些日期、时间占位符,可以查阅 API 文档中 SimpleDateFormat 类的说明,此处不再赘述。

Java 8 新增的日期、时间格式器

Java 8 新增的日期、时间 API 里不仅包括了 Instant、LocalDate、LocalDateTime、LocalTime 等代表日期、时间的类,而且在 java.time.format 包下提供了一个 DateTimeFormatter 格式器类,该类相当于前面介绍的 DateFormat 和 SimpleDateFormat 的合体,功能非常强大。

与 DateFormat、SimpleDateFormat 类似,DateTimeFormatter 不仅可以将日期、时间对象格式化成字符串,也可以将特定格式的字符串解析成日期、时间对象。

为了使用 DateTimeFormatter 进行格式化或解析,必须先获取 DateTimeFormatter 对象,获取 DateTimeFormatter 对象有如下三种常见的方式。

直接使用静态常量创建 DateTimeFormatter 格式器。DateTimeFormatter 类中包含了大量形如 ISO_LOCAL_DATE、ISO_LOCAL_TIME、ISO_LOCAL_DATE_TIME 等静态常量,这些静态常量本身就是 DateTimeFormatter 实例。 使用代表不同风格的枚举值来创建 DateTimeFormatter 格式器。在 FormatStyle 枚举类中定义了 FULL、LONG、MEDIUM、SHORT 四个枚举值,它们代表日期、时间的不同风格。 根据模式字符串来创建 DateTmeFormatter 格式器。类似于 SimpleDateFormat,可以采用模式字符串来创建 DateTimeFormatter,如果需要了解支持哪些模式字符串,则需要参该类的 API 文档。

使用 DateTimeFormatter 完成格式化

使用 DateTimeFormatter 将日期、时间(LocalDate、LocalDateTime、LocalTime等实例)格式化为字符串,可通过如下两种方式。

调用 DateTimeFormatter 的 format(TemporalAccessor temporal) 方法执行格式化,其中 LocalDate、LocalDateTime、LocalTime 等类都是 TemporalAccessor 接口的实现类。 调用 LocalDate、LocalDateTime、LocalTime 等日期、时间对象的 format(DateTimeFormatter formatter) 方法执行格式化。

上面两种方式的功能相同,用法也基本相似,如下程序示范了使用 DateTimeFormatter 来格式化日期、时间。

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.ofLocalizedDate(FormatStyle.LONG), // 根据模式字符串来创建DateTimeFormatter格式器 DateTimeFormatter.ofPattern('Gyyyy%%MMM%%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)); } }}

上面程序使用三种方式创建了6个 DateTimeFormatter 对象,然后程序中两行粗体字代码分别使用不同方式来格式化日期。运行上面程序,会看到如下所示的效果。

2020-02-112020-02-1117:13:00.30317:13:00.3032020-02-11T17:13:00.3032020-02-11T17:13:00.3032020年2月11日 星期二 17:13:002020年2月11日 星期二 17:13:002020年2月11日2020年2月11日公元2020%%二月%%11 17:13:00公元2020%%二月%%11 17:13:00

可以看出,使用 DateTimeFormatter 进行格式化,也可使用模式字符串对日期、时间进行自定义格式化,由此可见,功能完全覆盖了传统的 DateFormat、SimpleDateFormate 的功能。

提示:有些时候,读者可能还需要使用传统的 DateFormat 来执行格式化,DateTimeFormatter 则提供了一个 toFormat() 方法,该方法可以获取 DateTimeFormatter 对应的 Format 对象。

使用 DateTimeFormatter 解析字符串

为了使用 DateTimeFormatter 将指定格式的字符串解析成日期、时间对象(LocalDate、LocalDateTime、LocalTime 等实例),可通过日期、时间对象提供的 parse(CharSequence text,DateTimeFormatter formatter) 方法进行解析。

如下程序示范了使用 DateTimeFormatter 解析日期、时间字符串。

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$$$4月$$$13 20小时'; DateTimeFormatter fomatter2 = DateTimeFormatter.ofPattern('yyy$$$MMM$$$dd HH小时'); LocalDateTime dt2 = LocalDateTime.parse(str2, fomatter2); System.out.println(dt2); // 输出 2014-04-13T20:00 }}

上面程序中定义了两个不同格式的日期、时间字符串,为了解析它们,程序分别使用对应的格式字符串创建了 DateTmeFormatter 对象,这样 DateTimeFormatter 即可按该格式字符串将日期、时间字符串解析成 LocalDateTime 对象。编译、运行该程序,即可看到两个日期、时间字符串都被成功地解析成 LocalDateTime。

以上就是JAVA 格式化日期、时间的方法的详细内容,更多关于JAVA 格式化日期、时间的资料请关注好吧啦网其它相关文章!

标签: Java
相关文章: