Search in sources :

Example 91 with TemporalField

use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.

the class TCKJapaneseChronology method test_resolve_yd_strict.

@Test(dataProvider = "resolve_yd")
public void test_resolve_yd_strict(int y, int d, JapaneseDate expected, boolean smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d);
    if (strict) {
        JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
            fail("Should have failed");
        } catch (DateTimeException ex) {
        // expected
        }
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) HashMap(java.util.HashMap) JapaneseDate(java.time.chrono.JapaneseDate) Test(org.testng.annotations.Test)

Example 92 with TemporalField

use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.

the class TCKMinguoChronology method test_resolve_yd_lenient.

@Test(dataProvider = "resolve_yd")
public void test_resolve_yd_lenient(int y, int d, MinguoDate expected, boolean smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d);
    MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT);
    assertEquals(date, expected);
    assertEquals(fieldValues.size(), 0);
}
Also used : MinguoDate(java.time.chrono.MinguoDate) TemporalField(java.time.temporal.TemporalField) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 93 with TemporalField

use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.

the class TCKJapaneseChronology method test_resolve_yearOfEra_eraOnly_invalidTooSmall.

public void test_resolve_yearOfEra_eraOnly_invalidTooSmall() {
    for (ResolverStyle style : ResolverStyle.values()) {
        Map<TemporalField, Long> fieldValues = new HashMap<>();
        fieldValues.put(ChronoField.ERA, JapaneseEra.MEIJI.getValue() - 1L);
        try {
            JapaneseChronology.INSTANCE.resolveDate(fieldValues, style);
            fail("Should have failed: " + style);
        } catch (DateTimeException ex) {
        // expected
        }
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) HashMap(java.util.HashMap) ResolverStyle(java.time.format.ResolverStyle)

Example 94 with TemporalField

use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.

the class TCKDateTimeFormatter method test_format_withChronology_nonChronoFieldMapLink.

@Test
public void test_format_withChronology_nonChronoFieldMapLink() {
    TemporalAccessor temporal = new TemporalAccessor() {

        @Override
        public boolean isSupported(TemporalField field) {
            return field == IsoFields.WEEK_BASED_YEAR;
        }

        @Override
        public long getLong(TemporalField field) {
            if (field == IsoFields.WEEK_BASED_YEAR) {
                return 2345;
            }
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
        }
    };
    DateTimeFormatter test = new DateTimeFormatterBuilder().appendValue(IsoFields.WEEK_BASED_YEAR, 4).toFormatter(Locale.ENGLISH).withChronology(IsoChronology.INSTANCE);
    String result = test.format(temporal);
    assertEquals(result, "2345");
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 95 with TemporalField

use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.

the class TCKLocalizedFieldParser method test_parse_textField.

@Test(dataProvider = "FieldPatterns")
public void test_parse_textField(String pattern, String text, int pos, int expectedPos, long expectedValue) {
    WeekFields weekDef = WeekFields.of(locale);
    TemporalField field = null;
    switch(pattern.charAt(0)) {
        case 'e':
            field = weekDef.dayOfWeek();
            break;
        case 'w':
            field = weekDef.weekOfWeekBasedYear();
            break;
        case 'W':
            field = weekDef.weekOfMonth();
            break;
        case 'Y':
            field = weekDef.weekBasedYear();
            break;
        default:
            throw new IllegalStateException("bad format letter from pattern");
    }
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern);
    DateTimeFormatter dtf = b.toFormatter(locale);
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
        long value = parsed.getLong(field);
        assertEquals(value, expectedValue, "Value incorrect for " + field);
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) TemporalAccessor(java.time.temporal.TemporalAccessor) WeekFields(java.time.temporal.WeekFields) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Aggregations

TemporalField (java.time.temporal.TemporalField)104 Test (org.testng.annotations.Test)95 HashMap (java.util.HashMap)56 DateTimeException (java.time.DateTimeException)48 LocalDate (java.time.LocalDate)38 WeekFields (java.time.temporal.WeekFields)25 AbstractTCKTest (tck.java.time.AbstractTCKTest)25 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)20 DateTimeFormatter (java.time.format.DateTimeFormatter)19 TemporalAccessor (java.time.temporal.TemporalAccessor)13 JapaneseDate (java.time.chrono.JapaneseDate)12 HijrahDate (java.time.chrono.HijrahDate)11 MinguoDate (java.time.chrono.MinguoDate)10 ThaiBuddhistDate (java.time.chrono.ThaiBuddhistDate)10 Chronology (java.time.chrono.Chronology)4 IsoChronology (java.time.chrono.IsoChronology)4 ChronoLocalDate (java.time.chrono.ChronoLocalDate)3 HijrahChronology (java.time.chrono.HijrahChronology)3 JapaneseChronology (java.time.chrono.JapaneseChronology)3 MinguoChronology (java.time.chrono.MinguoChronology)3