系列中的上一篇
当前教程
解析和格式化
系列中的下一篇

系列中的上一篇: 瞬时

系列中的下一篇: 时间包

解析和格式化

日期时间 API 中基于时间类的提供parse()方法用于解析包含日期和时间信息的字符串。这些类还提供format()方法用于格式化基于时间的对象以供显示。在这两种情况下,过程都类似:您向DateTimeFormatter提供一个模式以创建formatter对象。然后将此formatter传递给parse()format()方法。

DateTimeFormatter类提供了许多预定义的格式化程序,或者您可以定义自己的格式化程序。

如果在转换过程中出现问题,parse()format()方法将抛出异常。因此,您的解析代码应捕获DateTimeParseException错误,您的格式化代码应捕获DateTimeException错误。有关异常处理的更多信息,请参阅部分捕获和处理异常

DateTimeFormatter类是不可变的且线程安全的;它可以在适当的情况下(也应该)分配给静态常量。

版本说明:java.time日期时间对象可以直接与java.util.FormatterString.format()一起使用,方法是使用与传统java.util.Datejava.util.Calendar类一起使用的熟悉模式格式化。

 

解析

parse(CharSequence)方法在LocalDate类中使用 ISO_LOCAL_DATE 格式化程序。要指定不同的格式化程序,您可以使用两个参数的parse(CharSequence, DateTimeFormatter)方法。以下示例使用预定义的 BASIC_ISO_DATE 格式化程序,该格式化程序使用格式 19590709 表示 1959 年 7 月 9 日。

String in = ...;
LocalDate date = LocalDate.parse(in, DateTimeFormatter.BASIC_ISO_DATE);

您还可以使用自己的模式定义格式化程序。以下代码创建了一个格式化程序,该格式化程序应用MMM d yyyy的格式。此格式指定三个字符来表示月份,一位数字来表示月份中的日期,以及四位数字来表示年份。使用此模式创建的格式化程序将识别诸如“Jan 3 2003”或“Mar 23 1994”之类的字符串。但是,要将格式指定为MMM dd yyyy,其中月份中的日期使用两位字符,那么您必须始终使用两位字符,对于一位数字的日期使用零进行填充:“Jun 03 2003”。

String input = ...;
try {
    DateTimeFormatter formatter =
                      DateTimeFormatter.ofPattern("MMM d yyyy");
    LocalDate date = LocalDate.parse(input, formatter);
    System.out.printf("%s%n", date);
}
catch (DateTimeParseException exc) {
    System.out.printf("%s is not parsable!%n", input);
    throw exc;      // Rethrow the exception.
}
// 'date' has been successfully parsed

DateTimeFormatter类的文档指定了您可以用来指定格式化或解析模式的完整符号列表。

StringConverter 示例非 ISO 日期转换页面上提供了日期格式化程序的另一个示例。

 

格式化

format(DateTimeFormatter)方法使用指定的格式将基于时间的对象转换为字符串表示形式。以下代码使用格式MMM d yyy hh:mm a转换ZonedDateTime的实例。日期的定义方式与之前的解析示例相同,但此模式还包括小时、分钟以及上午和下午的组件。

DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy  hh:mm a");

// Leaving from San Francisco on July 20, 2013, at 7:30 p.m.
LocalDateTime leaving = LocalDateTime.of(2013, Month.JULY, 20, 19, 30);
ZoneId leavingZone = ZoneId.of("America/Los_Angeles"); 
ZonedDateTime departure = ZonedDateTime.of(leaving, leavingZone);

try {
    String out1 = departure.format(format);
    System.out.printf("LEAVING:  %s (%s)%n", out1, leavingZone);
} catch (DateTimeException exc) {
    System.out.printf("%s can't be formatted!%n", departure);
    throw exc;
}

// Flight is 10 hours and 50 minutes, or 650 minutes
ZoneId arrivingZone = ZoneId.of("Asia/Tokyo"); 
ZonedDateTime arrival = departure.withZoneSameInstant(arrivingZone)
                                 .plusMinutes(650);

try {
    String out2 = arrival.format(format);
    System.out.printf("ARRIVING: %s (%s)%n", out2, arrivingZone);
} catch (DateTimeException exc) {
    System.out.printf("%s can't be formatted!%n", arrival);
    throw exc;
}

if (arrivingZone.getRules().isDaylightSavings(arrival.toInstant())) 
    System.out.printf("  (%s daylight saving time will be in effect.)%n",
                      arrivingZone);
else
    System.out.printf("  (%s standard time will be in effect.)%n",
                      arrivingZone);

此示例的输出(打印到达和离开时间)如下所示

LEAVING:  Jul 20 2013  07:30 PM (America/Los_Angeles)
ARRIVING: Jul 21 2013  10:20 PM (Asia/Tokyo)

在本教程中


上次更新: 2022 年 1 月 27 日


系列中的上一篇
当前教程
解析和格式化
系列中的下一篇

系列中的上一篇: 瞬时

系列中的下一篇: 时间包