java.time.temporal.ChronoField Java Examples

The following examples show how to use java.time.temporal.ChronoField. 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: HijrahDate.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public ValueRange range(TemporalField field) {
    if (field instanceof ChronoField) {
        if (isSupported(field)) {
            ChronoField f = (ChronoField) field;
            switch (f) {
                case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
                case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
                case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 5);  // TODO
                // TODO does the limited range of valid years cause years to
                // start/end part way through? that would affect range
            }
            return getChronology().range(f);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.rangeRefinedBy(this);
}
 
Example #2
Source File: CopticDate.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public CopticDate with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        f.checkValidValue(newValue);        // TODO: validate value
        int nvalue = (int) newValue;
        switch (f) {
            case DAY_OF_WEEK: return plusDays(newValue - get(ChronoField.DAY_OF_WEEK));
            case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH));
            case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR));
            case DAY_OF_MONTH: return resolvePreviousValid(prolepticYear, month, nvalue);
            case DAY_OF_YEAR: return resolvePreviousValid(prolepticYear, ((nvalue - 1) / 30) + 1, ((nvalue - 1) % 30) + 1);
            case EPOCH_DAY: return ofEpochDay(nvalue);
            case ALIGNED_WEEK_OF_MONTH: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_MONTH)) * 7);
            case ALIGNED_WEEK_OF_YEAR: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_YEAR)) * 7);
            case MONTH_OF_YEAR: return resolvePreviousValid(prolepticYear, nvalue, day);
            case YEAR_OF_ERA: return resolvePreviousValid(prolepticYear >= 1 ? nvalue : 1 - nvalue, month, day);
            case YEAR: return resolvePreviousValid(nvalue, month, day);
            case ERA: return resolvePreviousValid(1 - prolepticYear, month, day);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.adjustInto(this, newValue);
}
 
Example #3
Source File: LocalDate.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private int get0(TemporalField field) {
    switch ((ChronoField) field) {
        case DAY_OF_WEEK: return getDayOfWeek().getValue();
        case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1;
        case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1;
        case DAY_OF_MONTH: return day;
        case DAY_OF_YEAR: return getDayOfYear();
        case EPOCH_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'EpochDay' for get() method, use getLong() instead");
        case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1;
        case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1;
        case MONTH_OF_YEAR: return month;
        case PROLEPTIC_MONTH: throw new UnsupportedTemporalTypeException("Invalid field 'ProlepticMonth' for get() method, use getLong() instead");
        case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year);
        case YEAR: return year;
        case ERA: return (year >= 1 ? 1 : 0);
    }
    throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
 
Example #4
Source File: TCKJapaneseChronology.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "resolve_eymd")
public void test_resolve_eymd(ResolverStyle style, JapaneseEra era, int yoe, int m, int d, JapaneseDate expected) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.ERA, (long) era.getValue());
    fieldValues.put(ChronoField.YEAR_OF_ERA, (long) yoe);
    fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m);
    fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d);
    if (expected != null) {
        JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, style);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            JapaneseChronology.INSTANCE.resolveDate(fieldValues, style);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example #5
Source File: TimeFormatter.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a time formatter that uses the default locale and timezone.
 */
public TimeFormatter() {
    dtf = new DateTimeFormatterBuilder()
            .appendValue(CLOCK_HOUR_OF_AMPM)
            .appendLiteral(':')
            .appendValue(MINUTE_OF_HOUR, 2)
            .optionalStart()
            .appendLiteral(':')
            .appendValue(SECOND_OF_MINUTE, 2)
            .appendLiteral(' ')
            .appendText(ChronoField.AMPM_OF_DAY)
            .optionalStart()
            .appendLiteral(' ')
            .appendOffset("+HH:MM", "+00:00")
            .toFormatter()
            .withLocale(Locale.getDefault())
            .withZone(ZoneId.systemDefault());
}
 
Example #6
Source File: TCKIsoChronology.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "resolve_ymaa")
public void test_resolve_ymaa_strict(int y, int m, int w, int d, LocalDate expected, boolean smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m);
    fieldValues.put(ChronoField.ALIGNED_WEEK_OF_MONTH, (long) w);
    fieldValues.put(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH, (long) d);
    if (strict) {
        LocalDate date = IsoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            IsoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example #7
Source File: TCKMinguoChronology.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "resolve_ymaa")
public void test_resolve_ymaa_strict(int y, int m, int w, int d, MinguoDate expected, boolean smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m);
    fieldValues.put(ChronoField.ALIGNED_WEEK_OF_MONTH, (long) w);
    fieldValues.put(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH, (long) d);
    if (strict) {
        MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example #8
Source File: TCKJulianFields.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@DataProvider(name="samples")
Object[][] data_samples() {
    return new Object[][] {
        {ChronoField.EPOCH_DAY, JAN01_1970, 0L},
        {JulianFields.JULIAN_DAY, JAN01_1970, 2400001L + 40587L},
        {JulianFields.MODIFIED_JULIAN_DAY, JAN01_1970, 40587L},
        {JulianFields.RATA_DIE, JAN01_1970, 710347L + (40587L - 31771L)},

        {ChronoField.EPOCH_DAY, DEC31_1969, -1L},
        {JulianFields.JULIAN_DAY, DEC31_1969, 2400001L + 40586L},
        {JulianFields.MODIFIED_JULIAN_DAY, DEC31_1969, 40586L},
        {JulianFields.RATA_DIE, DEC31_1969, 710347L + (40586L - 31771L)},

        {ChronoField.EPOCH_DAY, NOV12_1945, (-24 * 365 - 6) - 31 - 30 + 11},
        {JulianFields.JULIAN_DAY, NOV12_1945, 2431772L},
        {JulianFields.MODIFIED_JULIAN_DAY, NOV12_1945, 31771L},
        {JulianFields.RATA_DIE, NOV12_1945, 710347L},

        {ChronoField.EPOCH_DAY, JAN01_0001, (-24 * 365 - 6) - 31 - 30 + 11 - 710346L},
        {JulianFields.JULIAN_DAY, JAN01_0001, 2431772L - 710346L},
        {JulianFields.MODIFIED_JULIAN_DAY, JAN01_0001, 31771L - 710346L},
        {JulianFields.RATA_DIE, JAN01_0001, 1},
    };
}
 
Example #9
Source File: TCKThaiBuddhistChronology.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "resolve_yd")
public void test_resolve_yd_smart(int y, int d, ThaiBuddhistDate 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 (smart) {
        ThaiBuddhistDate date = ThaiBuddhistChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            ThaiBuddhistChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example #10
Source File: TCKJulianFields.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@DataProvider(name="samples")
Object[][] data_samples() {
    return new Object[][] {
        {ChronoField.EPOCH_DAY, JAN01_1970, 0L},
        {JulianFields.JULIAN_DAY, JAN01_1970, 2400001L + 40587L},
        {JulianFields.MODIFIED_JULIAN_DAY, JAN01_1970, 40587L},
        {JulianFields.RATA_DIE, JAN01_1970, 710347L + (40587L - 31771L)},

        {ChronoField.EPOCH_DAY, DEC31_1969, -1L},
        {JulianFields.JULIAN_DAY, DEC31_1969, 2400001L + 40586L},
        {JulianFields.MODIFIED_JULIAN_DAY, DEC31_1969, 40586L},
        {JulianFields.RATA_DIE, DEC31_1969, 710347L + (40586L - 31771L)},

        {ChronoField.EPOCH_DAY, NOV12_1945, (-24 * 365 - 6) - 31 - 30 + 11},
        {JulianFields.JULIAN_DAY, NOV12_1945, 2431772L},
        {JulianFields.MODIFIED_JULIAN_DAY, NOV12_1945, 31771L},
        {JulianFields.RATA_DIE, NOV12_1945, 710347L},

        {ChronoField.EPOCH_DAY, JAN01_0001, (-24 * 365 - 6) - 31 - 30 + 11 - 710346L},
        {JulianFields.JULIAN_DAY, JAN01_0001, 2431772L - 710346L},
        {JulianFields.MODIFIED_JULIAN_DAY, JAN01_0001, 31771L - 710346L},
        {JulianFields.RATA_DIE, JAN01_0001, 1},
    };
}
 
Example #11
Source File: LocalDate.java    From Java8CN with Apache License 2.0 6 votes vote down vote up
private int get0(TemporalField field) {
    switch ((ChronoField) field) {
        case DAY_OF_WEEK: return getDayOfWeek().getValue();
        case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1;
        case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1;
        case DAY_OF_MONTH: return day;
        case DAY_OF_YEAR: return getDayOfYear();
        case EPOCH_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'EpochDay' for get() method, use getLong() instead");
        case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1;
        case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1;
        case MONTH_OF_YEAR: return month;
        case PROLEPTIC_MONTH: throw new UnsupportedTemporalTypeException("Invalid field 'ProlepticMonth' for get() method, use getLong() instead");
        case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year);
        case YEAR: return year;
        case ERA: return (year >= 1 ? 1 : 0);
    }
    throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
 
Example #12
Source File: TCKOffsetDateTime.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_getLong_TemporalField() {
    OffsetDateTime test = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(12, 30, 40, 987654321), OFFSET_PONE);
    assertEquals(test.getLong(ChronoField.YEAR), 2008);
    assertEquals(test.getLong(ChronoField.MONTH_OF_YEAR), 6);
    assertEquals(test.getLong(ChronoField.DAY_OF_MONTH), 30);
    assertEquals(test.getLong(ChronoField.DAY_OF_WEEK), 1);
    assertEquals(test.getLong(ChronoField.DAY_OF_YEAR), 182);

    assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12);
    assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30);
    assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40);
    assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321);
    assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0);
    assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1);

    assertEquals(test.getLong(ChronoField.INSTANT_SECONDS), test.toEpochSecond());
    assertEquals(test.getLong(ChronoField.OFFSET_SECONDS), 3600);
}
 
Example #13
Source File: TCKThaiBuddhistChronology.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "resolve_ymd")
public void test_resolve_ymd_strict(int y, int m, int d, ThaiBuddhistDate expected, Object smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m);
    fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d);
    if (strict) {
        ThaiBuddhistDate date = ThaiBuddhistChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            ThaiBuddhistChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example #14
Source File: HijrahDate.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public HijrahDate with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        // not using checkValidIntValue so EPOCH_DAY and PROLEPTIC_MONTH work
        chrono.range(f).checkValidValue(newValue, f);    // TODO: validate value
        int nvalue = (int) newValue;
        switch (f) {
            case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek());
            case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH));
            case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR));
            case DAY_OF_MONTH: return resolvePreviousValid(prolepticYear, monthOfYear, nvalue);
            case DAY_OF_YEAR: return plusDays(Math.min(nvalue, lengthOfYear()) - getDayOfYear());
            case EPOCH_DAY: return new HijrahDate(chrono, newValue);
            case ALIGNED_WEEK_OF_MONTH: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_MONTH)) * 7);
            case ALIGNED_WEEK_OF_YEAR: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_YEAR)) * 7);
            case MONTH_OF_YEAR: return resolvePreviousValid(prolepticYear, nvalue, dayOfMonth);
            case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth());
            case YEAR_OF_ERA: return resolvePreviousValid(prolepticYear >= 1 ? nvalue : 1 - nvalue, monthOfYear, dayOfMonth);
            case YEAR: return resolvePreviousValid(nvalue, monthOfYear, dayOfMonth);
            case ERA: return resolvePreviousValid(1 - prolepticYear, monthOfYear, dayOfMonth);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return super.with(field, newValue);
}
 
Example #15
Source File: TCKThaiBuddhistChronology.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "resolve_ymd")
public void test_resolve_ymd_strict(int y, int m, int d, ThaiBuddhistDate expected, Object smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m);
    fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d);
    if (strict) {
        ThaiBuddhistDate date = ThaiBuddhistChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            ThaiBuddhistChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example #16
Source File: ScheduledTriggerTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
@AwaitsFix(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Sep-2018
// this does not appear to be a good way to test this
public void testTrigger() throws Exception {
  CoreContainer container = cluster.getJettySolrRunners().get(0).getCoreContainer();

  Map<String, Object> properties = createTriggerProperties(new Date().toInstant().toString(), TimeZone.getDefault().getID());

  scheduledTriggerTest(container, properties);

  TimeZone timeZone = TimeZone.getDefault();
  DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
      .append(DateTimeFormatter.ISO_LOCAL_DATE).appendPattern("['T'[HH[:mm[:ss]]]]") //brackets mean optional
      .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
      .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
      .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
      .toFormatter(Locale.ROOT).withZone(timeZone.toZoneId());
  properties = createTriggerProperties(dateTimeFormatter.format(Instant.now()), timeZone.getID());
  scheduledTriggerTest(container, properties);
}
 
Example #17
Source File: TCKMinguoChronology.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "resolve_yd")
public void test_resolve_yd_strict(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);
    if (strict) {
        MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example #18
Source File: PDTXMLConverterTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testLocalTime ()
{
  assertNull (PDTXMLConverter.getXMLCalendarTime ((LocalTime) null));
  final LocalTime aLT = PDTFactory.getCurrentLocalTime ();
  final XMLGregorianCalendar c1 = PDTXMLConverter.getXMLCalendarTime (aLT);
  assertNotNull (c1);
  assertEquals (DatatypeConstants.FIELD_UNDEFINED, c1.getYear ());
  assertEquals (DatatypeConstants.FIELD_UNDEFINED, c1.getMonth ());
  assertEquals (DatatypeConstants.FIELD_UNDEFINED, c1.getDay ());
  assertEquals (aLT.getHour (), c1.getHour ());
  assertEquals (aLT.getMinute (), c1.getMinute ());
  assertEquals (aLT.getSecond (), c1.getSecond ());
  assertEquals (aLT.get (ChronoField.MILLI_OF_SECOND), c1.getMillisecond ());
  assertEquals (DatatypeConstants.FIELD_UNDEFINED, c1.getTimezone ());
  final LocalTime aLT2 = PDTXMLConverter.getLocalTime (c1);
  assertNotNull (aLT2);
  // In Java9 aLDT2 only has millis
  assertEquals (aLT.withNano (c1.getMillisecond () * (int) CGlobal.NANOSECONDS_PER_MILLISECOND), aLT2);
  assertNull (PDTXMLConverter.getLocalTime (null));
}
 
Example #19
Source File: Parsed.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public long getLong(TemporalField field) {
    Objects.requireNonNull(field, "field");
    Long value = fieldValues.get(field);
    if (value != null) {
        return value;
    }
    if (date != null && date.isSupported(field)) {
        return date.getLong(field);
    }
    if (time != null && time.isSupported(field)) {
        return time.getLong(field);
    }
    if (field instanceof ChronoField) {
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.getFrom(this);
}
 
Example #20
Source File: TCKOffsetDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_getLong_TemporalField() {
    OffsetDateTime test = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(12, 30, 40, 987654321), OFFSET_PONE);
    assertEquals(test.getLong(ChronoField.YEAR), 2008);
    assertEquals(test.getLong(ChronoField.MONTH_OF_YEAR), 6);
    assertEquals(test.getLong(ChronoField.DAY_OF_MONTH), 30);
    assertEquals(test.getLong(ChronoField.DAY_OF_WEEK), 1);
    assertEquals(test.getLong(ChronoField.DAY_OF_YEAR), 182);

    assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12);
    assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30);
    assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40);
    assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321);
    assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0);
    assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1);

    assertEquals(test.getLong(ChronoField.INSTANT_SECONDS), test.toEpochSecond());
    assertEquals(test.getLong(ChronoField.OFFSET_SECONDS), 3600);
}
 
Example #21
Source File: ChronoLocalDateTimeImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int get(TemporalField field) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        return (f.isTimeBased() ? time.get(field) : date.get(field));
    }
    return range(field).checkValidIntValue(getLong(field), field);
}
 
Example #22
Source File: TestUmmAlQuraChronology.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_chronoFields() {
    ChronoLocalDate hdate = HijrahChronology.INSTANCE.date(1434, 6, 28);
    assertEquals(hdate.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH), 3);
    assertEquals(hdate.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR), 7);
    assertEquals(hdate.get(ChronoField.ALIGNED_WEEK_OF_MONTH), 4);
    assertEquals(hdate.get(ChronoField.ALIGNED_WEEK_OF_YEAR), 25);
    assertEquals(hdate.get(ChronoField.ERA), 1);
    assertEquals(hdate.get(ChronoField.YEAR_OF_ERA), 1434);
    assertEquals(hdate.get(ChronoField.MONTH_OF_YEAR), 6);
    assertEquals(hdate.get(ChronoField.DAY_OF_MONTH), 28);
    assertEquals(hdate.get(ChronoField.DAY_OF_WEEK), 3);
    assertEquals(hdate.get(ChronoField.DAY_OF_YEAR), 175);
}
 
Example #23
Source File: TCKInstant.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_get_TemporalField() {
    Instant test = TEST_12345_123456789;
    assertEquals(test.get(ChronoField.NANO_OF_SECOND), 123456789);
    assertEquals(test.get(ChronoField.MICRO_OF_SECOND), 123456);
    assertEquals(test.get(ChronoField.MILLI_OF_SECOND), 123);
}
 
Example #24
Source File: TCKJapaneseChronology.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolve_styles")
public void test_resolve_yearOfEra_yearOfEraOnly_valid(ResolverStyle style) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR_OF_ERA, 1L);
    JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, style);
    assertEquals(date, null);
    assertEquals(fieldValues.get(ChronoField.YEAR_OF_ERA), (Long) 1L);
    assertEquals(fieldValues.size(), 1);
}
 
Example #25
Source File: TestUmmAlQuraChronology.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test (dataProvider="monthDays")
public void test_valueRange_monthDays(int year, int month, int maxlength) {
    ChronoLocalDate date = HijrahChronology.INSTANCE.date(year, month, 1);
    ValueRange range = null;
    for (int i=1; i<=12; i++) {
        range = date.range(ChronoField.DAY_OF_MONTH);
        date = date.plus(1, ChronoUnit.MONTHS);
        assertEquals(range.getMaximum(), month, maxlength);
    }
}
 
Example #26
Source File: TCKMinguoChronology.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #27
Source File: TCKInstant.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected List<TemporalField> invalidFields() {
    List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values()));
    list.removeAll(validFields());
    list.add(JulianFields.JULIAN_DAY);
    list.add(JulianFields.MODIFIED_JULIAN_DAY);
    list.add(JulianFields.RATA_DIE);
    return list;
}
 
Example #28
Source File: ChronoZonedDateTime.java    From desugar_jdk_libs with GNU General Public License v2.0 5 votes vote down vote up
@Override
default ValueRange range(TemporalField field) {
    if (field instanceof ChronoField) {
        if (field == INSTANT_SECONDS || field == OFFSET_SECONDS) {
            return field.range();
        }
        return toLocalDateTime().range(field);
    }
    return field.rangeRefinedBy(this);
}
 
Example #29
Source File: TCKHijrahChronology.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolve_styleByEra")
public void test_resolve_yearOfEra_eraAndYearOfEraOnly_valid(ResolverStyle style, HijrahEra era) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.ERA, (long) era.getValue());
    fieldValues.put(ChronoField.YEAR_OF_ERA, 1343L);
    HijrahDate date = HijrahChronology.INSTANCE.resolveDate(fieldValues, style);
    assertEquals(date, null);
    assertEquals(fieldValues.get(ChronoField.ERA), null);
    assertEquals(fieldValues.get(ChronoField.YEAR_OF_ERA), null);
    assertEquals(fieldValues.get(ChronoField.YEAR), (Long) 1343L);
    assertEquals(fieldValues.size(), 1);
}
 
Example #30
Source File: TCKJapaneseChronology.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolve_yd")
public void test_resolve_yd_lenient(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);
    JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT);
    assertEquals(date, expected);
    assertEquals(fieldValues.size(), 0);
}