非 ISO 日期转换

本教程不会详细讨论java.time.chrono软件包。但是,知道此程序包提供了几种不基于 ISO 的 预定义时间 Sequences 可能会很有用,例如日语,回历,民国和泰国佛教徒。您也可以使用此包创建自己的年表。

本节说明如何在基于 ISO 的日期和其他 预定义时间 Sequences 之一中的日期之间进行转换。

转换为基于非 ISO 的日期

您可以使用from\(TemporalAccessor\)方法(例如JapaneseDate.from(TemporalAccessor))将基于 ISO 的日期转换为另一种按时间 Sequences 排列的日期。如果无法将日期转换为有效实例,则此方法将引发DateTimeException。以下代码将LocalDateTime实例转换为几个 预定义的非 ISOcalendar 日期:

LocalDateTime date = LocalDateTime.of(2013, Month.JULY, 20, 19, 30);
JapaneseDate jdate     = JapaneseDate.from(date);
HijrahDate hdate       = HijrahDate.from(date);
MinguoDate mdate       = MinguoDate.from(date);
ThaiBuddhistDate tdate = ThaiBuddhistDate.from(date);

StringConverter示例从LocalDate转换为ChronoLocalDate再转换为String并返回。 toString方法采用LocalDateChronology的实例,并使用提供的Chronology返回转换后的字符串。 DateTimeFormatterBuilder用于构建可用于打印日期的字符串:

/**
 * Converts a LocalDate (ISO) value to a ChronoLocalDate date
 * using the provided Chronology, and then formats the
 * ChronoLocalDate to a String using a DateTimeFormatter with a
 * SHORT pattern based on the Chronology and the current Locale.
 *
 * @param localDate - the ISO date to convert and format.
 * @param chrono - an optional Chronology. If null, then IsoChronology is used.
 */
public static String toString(LocalDate localDate, Chronology chrono) {
    if (localDate != null) {
        Locale locale = Locale.getDefault(Locale.Category.FORMAT);
        ChronoLocalDate cDate;
        if (chrono == null) {
            chrono = IsoChronology.INSTANCE;
        }
        try {
            cDate = chrono.date(localDate);
        } catch (DateTimeException ex) {
            System.err.println(ex);
            chrono = IsoChronology.INSTANCE;
            cDate = localDate;
        }
        DateTimeFormatter dateFormatter =
            DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
                             .withLocale(locale)
                             .withChronology(chrono)
                             .withDecimalStyle(DecimalStyle.of(locale));
        String pattern = "M/d/yyyy GGGGG";
        return dateFormatter.format(cDate);
    } else {
        return "";
    }
}

在以下日期为 预定义的时间 Sequences 调用该方法时:

LocalDate date = LocalDate.of(1996, Month.OCTOBER, 29);
System.out.printf("%s%n",
     StringConverter.toString(date, JapaneseChronology.INSTANCE));
System.out.printf("%s%n",
     StringConverter.toString(date, MinguoChronology.INSTANCE));
System.out.printf("%s%n",
     StringConverter.toString(date, ThaiBuddhistChronology.INSTANCE));
System.out.printf("%s%n",
     StringConverter.toString(date, HijrahChronology.INSTANCE));

输出如下所示:

10/29/0008 H
10/29/0085 1
10/29/2539 B.E.
6/16/1417 1

转换为基于 ISO 的日期

您可以使用静态LocalDate.from方法将非 ISO 日期转换为LocalDate实例,如以下示例所示:

LocalDate date = LocalDate.from(JapaneseDate.now());

其他基于时间的类也提供此方法,如果无法转换日期,则抛出DateTimeException

StringConverter示例中的fromString方法将解析包含非 ISO 日期的String并返回LocalDate实例。

/**
 * Parses a String to a ChronoLocalDate using a DateTimeFormatter
 * with a short pattern based on the current Locale and the
 * provided Chronology, then converts this to a LocalDate (ISO)
 * value.
 *
 * @param text   - the input date text in the SHORT format expected
 *                 for the Chronology and the current Locale.
 *
 * @param chrono - an optional Chronology. If null, then IsoChronology
 *                 is used.
 */
public static LocalDate fromString(String text, Chronology chrono) {
    if (text != null && !text.isEmpty()) {
        Locale locale = Locale.getDefault(Locale.Category.FORMAT);
        if (chrono == null) {
           chrono = IsoChronology.INSTANCE;
        }
        String pattern = "M/d/yyyy GGGGG";
        DateTimeFormatter df = new DateTimeFormatterBuilder().parseLenient()
                              .appendPattern(pattern)
                              .toFormatter()
                              .withChronology(chrono)
                              .withDecimalStyle(DecimalStyle.of(locale));
        TemporalAccessor temporal = df.parse(text);
        ChronoLocalDate cDate = chrono.date(temporal);
        return LocalDate.from(cDate);
    }
return null;
}

用以下字符串 调用该方法时:

System.out.printf("%s%n", StringConverter.fromString("10/29/0008 H",
    JapaneseChronology.INSTANCE));
System.out.printf("%s%n", StringConverter.fromString("10/29/0085 1",
    MinguoChronology.INSTANCE));
System.out.printf("%s%n", StringConverter.fromString("10/29/2539 B.E.",
    ThaiBuddhistChronology.INSTANCE));
System.out.printf("%s%n", StringConverter.fromString("6/16/1417 1",
    HijrahChronology.INSTANCE));

打印的字符串 应全部转换回 1996 年 10 月 29 日:

1996-10-29
1996-10-29
1996-10-29
1996-10-29