Java Code Examples for java.time.format.DateTimeFormatter#withChronology()

The following examples show how to use java.time.format.DateTimeFormatter#withChronology() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JapanEraNameCompatTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testFormatParseEraName() {
    LocalDate date = LocalDate.of(2019, 5, 1);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd GGGG");
    formatter = formatter.withChronology(JapaneseChronology.INSTANCE);

    int num = 0;
    for (Locale locale : Calendar.getAvailableLocales()) {
        formatter = formatter.withLocale(locale);
        try {
            LocalDate.parse(date.format(formatter), formatter);
        } catch (DateTimeParseException e) {
            // If an array is defined for Japanese eras in java.time resource,
            // but an era entry is missing, format fallback to English name
            // while parse throw DateTimeParseException.
            num++;
            System.out.println("Missing java.time resource data for locale: " + locale);
        }
    }
    if (num > 0) {
        throw new RuntimeException("Missing java.time data for " + num + " locales");
    }
}
 
Example 2
Source File: JapanEraNameCompatTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testFormatParseEraName() {
    LocalDate date = LocalDate.of(2019, 5, 1);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd GGGG");
    formatter = formatter.withChronology(JapaneseChronology.INSTANCE);

    int num = 0;
    for (Locale locale : Calendar.getAvailableLocales()) {
        formatter = formatter.withLocale(locale);
        try {
            LocalDate.parse(date.format(formatter), formatter);
        } catch (DateTimeParseException e) {
            // If an array is defined for Japanese eras in java.time resource,
            // but an era entry is missing, format fallback to English name
            // while parse throw DateTimeParseException.
            num++;
            System.out.println("Missing java.time resource data for locale: " + locale);
        }
    }
    if (num > 0) {
        throw new RuntimeException("Missing java.time data for " + num + " locales");
    }
}
 
Example 3
Source File: TCKDateTimeParseResolver.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_fieldResolvesToChronoZonedDateTime_overrideChrono_matches() {
    MinguoDate mdt = MinguoDate.of(100, 6, 30);
    ChronoZonedDateTime<MinguoDate> mzdt = mdt.atTime(LocalTime.NOON).atZone(EUROPE_PARIS);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(mzdt)).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    TemporalAccessor accessor = f.parse("1234567890");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.from(mdt));
    assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.NOON);
    assertEquals(accessor.query(TemporalQueries.chronology()), MinguoChronology.INSTANCE);
    assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_PARIS);
}
 
Example 4
Source File: TCKDateTimeParseResolver.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_fieldResolvesToChronoZonedDateTime_overrideChrono_matches() {
    MinguoDate mdt = MinguoDate.of(100, 6, 30);
    ChronoZonedDateTime<MinguoDate> mzdt = mdt.atTime(LocalTime.NOON).atZone(EUROPE_PARIS);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(mzdt)).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    TemporalAccessor accessor = f.parse("1234567890");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.from(mdt));
    assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.NOON);
    assertEquals(accessor.query(TemporalQueries.chronology()), MinguoChronology.INSTANCE);
    assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_PARIS);
}
 
Example 5
Source File: TCKDateTimeParseResolver.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_fieldResolvesToChronoLocalDate_overrideChrono_matches() {
    MinguoDate mdt = MinguoDate.of(100, 6, 30);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(mdt)).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    TemporalAccessor accessor = f.parse("1234567890");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.from(mdt));
    assertEquals(accessor.query(TemporalQueries.localTime()), null);
    assertEquals(accessor.query(TemporalQueries.chronology()), MinguoChronology.INSTANCE);
}
 
Example 6
Source File: TCKDateTimeParseResolver.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions = DateTimeParseException.class)
public void test_fieldResolvesToChronoLocalDate_overrideChrono_wrongChrono() {
    ChronoLocalDate cld = ThaiBuddhistChronology.INSTANCE.dateNow();
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cld)).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    f.parse("1234567890");
}
 
Example 7
Source File: TCKDateTimeFormatter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withChronology() {
    DateTimeFormatter test = fmt;
    assertEquals(test.getChronology(), null);
    test = test.withChronology(IsoChronology.INSTANCE);
    assertEquals(test.getChronology(), IsoChronology.INSTANCE);
    test = test.withChronology(null);
    assertEquals(test.getChronology(), null);
}
 
Example 8
Source File: TCKDateTimeParseResolver.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withChronology_override() {
    DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    TemporalAccessor accessor = f.parse("");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
    assertEquals(accessor.query(TemporalQueries.localTime()), null);
    assertEquals(accessor.query(TemporalQueries.chronology()), MinguoChronology.INSTANCE);
}
 
Example 9
Source File: TCKDateTimeParseResolver.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withChronology_parsedChronology_override() {
    DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).appendChronologyId().toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    TemporalAccessor accessor = f.parse("ThaiBuddhist");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
    assertEquals(accessor.query(TemporalQueries.localTime()), null);
    assertEquals(accessor.query(TemporalQueries.chronology()), ThaiBuddhistChronology.INSTANCE);
}
 
Example 10
Source File: TCKDateTimeParseResolver.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions = DateTimeParseException.class)
public void test_fieldResolvesToChronoLocalDateTime_overrideChrono_wrongChrono() {
    ChronoLocalDateTime<?> cldt = ThaiBuddhistChronology.INSTANCE.dateNow().atTime(LocalTime.NOON);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cldt)).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    f.parse("1234567890");
}
 
Example 11
Source File: TCKDateTimeFormatter.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withChronology() {
    DateTimeFormatter test = fmt;
    assertEquals(test.getChronology(), null);
    test = test.withChronology(IsoChronology.INSTANCE);
    assertEquals(test.getChronology(), IsoChronology.INSTANCE);
    test = test.withChronology(null);
    assertEquals(test.getChronology(), null);
}
 
Example 12
Source File: TCKDateTimeParseResolver.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withChronology_parsedChronology_override() {
    DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).appendChronologyId().toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    TemporalAccessor accessor = f.parse("ThaiBuddhist");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
    assertEquals(accessor.query(TemporalQueries.localTime()), null);
    assertEquals(accessor.query(TemporalQueries.chronology()), ThaiBuddhistChronology.INSTANCE);
}
 
Example 13
Source File: TCKDateTimeParseResolver.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions = DateTimeParseException.class)
public void test_fieldResolvesToChronoZonedDateTime_overrideChrono_wrongChrono() {
    ChronoZonedDateTime<?> cldt = ThaiBuddhistChronology.INSTANCE.dateNow().atTime(LocalTime.NOON).atZone(EUROPE_PARIS);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cldt)).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    f.parse("1234567890");
}
 
Example 14
Source File: TCKDateTimeParseResolver.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_fieldResolvesToChronoZonedDateTime_overrideChrono_matches() {
    MinguoDate mdt = MinguoDate.of(100, 6, 30);
    ChronoZonedDateTime<MinguoDate> mzdt = mdt.atTime(LocalTime.NOON).atZone(EUROPE_PARIS);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(mzdt)).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    TemporalAccessor accessor = f.parse("1234567890");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.from(mdt));
    assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.NOON);
    assertEquals(accessor.query(TemporalQueries.chronology()), MinguoChronology.INSTANCE);
    assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_PARIS);
}
 
Example 15
Source File: TCKDateTimeParseResolver.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withChronology_parsedChronology_override() {
    DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).appendChronologyId().toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    TemporalAccessor accessor = f.parse("ThaiBuddhist");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
    assertEquals(accessor.query(TemporalQueries.localTime()), null);
    assertEquals(accessor.query(TemporalQueries.chronology()), ThaiBuddhistChronology.INSTANCE);
}
 
Example 16
Source File: TCKDateTimeParseResolver.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_fieldResolvesToChronoLocalDateTime_overrideChrono_matches() {
    MinguoDate mdt = MinguoDate.of(100, 6, 30);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(mdt.atTime(LocalTime.NOON))).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    TemporalAccessor accessor = f.parse("1234567890");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.from(mdt));
    assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.NOON);
    assertEquals(accessor.query(TemporalQueries.chronology()), MinguoChronology.INSTANCE);
}
 
Example 17
Source File: TCKDateTimeParseResolver.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions = DateTimeParseException.class)
public void test_fieldResolvesToChronoLocalDate_overrideChrono_wrongChrono() {
    ChronoLocalDate cld = ThaiBuddhistChronology.INSTANCE.dateNow();
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cld)).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    f.parse("1234567890");
}
 
Example 18
Source File: TCKDateTimeParseResolver.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions = DateTimeParseException.class)
public void test_fieldResolvesToChronoLocalDateTime_overrideChrono_wrongChrono() {
    ChronoLocalDateTime<?> cldt = ThaiBuddhistChronology.INSTANCE.dateNow().atTime(LocalTime.NOON);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cldt)).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    f.parse("1234567890");
}
 
Example 19
Source File: TCKDateTimeParseResolver.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_fieldResolvesToChronoZonedDateTime_overrideChrono_matches() {
    MinguoDate mdt = MinguoDate.of(100, 6, 30);
    ChronoZonedDateTime<MinguoDate> mzdt = mdt.atTime(LocalTime.NOON).atZone(EUROPE_PARIS);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(mzdt)).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    TemporalAccessor accessor = f.parse("1234567890");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.from(mdt));
    assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.NOON);
    assertEquals(accessor.query(TemporalQueries.chronology()), MinguoChronology.INSTANCE);
    assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_PARIS);
}
 
Example 20
Source File: TCKDateTimeParseResolver.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions = DateTimeParseException.class)
public void test_fieldResolvesToChronoZonedDateTime_overrideChrono_wrongChrono() {
    ChronoZonedDateTime<?> cldt = ThaiBuddhistChronology.INSTANCE.dateNow().atTime(LocalTime.NOON).atZone(EUROPE_PARIS);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cldt)).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    f.parse("1234567890");
}