Search in sources :

Example 26 with Chronology

use of java.time.chrono.Chronology in project jdk8u_jdk by JetBrains.

the class TCKChronology method test_ThaiBuddhistChronology_dateNow.

@Test
public void test_ThaiBuddhistChronology_dateNow() {
    ZoneId zoneId_paris = ZoneId.of("Europe/Paris");
    Clock clock = Clock.system(zoneId_paris);
    Chronology chrono = Chronology.of("ThaiBuddhist");
    assertEquals(chrono.dateNow(), ThaiBuddhistChronology.INSTANCE.dateNow());
    assertEquals(chrono.dateNow(zoneId_paris), ThaiBuddhistChronology.INSTANCE.dateNow(zoneId_paris));
    assertEquals(chrono.dateNow(clock), ThaiBuddhistChronology.INSTANCE.dateNow(clock));
}
Also used : ZoneId(java.time.ZoneId) HijrahChronology(java.time.chrono.HijrahChronology) ThaiBuddhistChronology(java.time.chrono.ThaiBuddhistChronology) IsoChronology(java.time.chrono.IsoChronology) Chronology(java.time.chrono.Chronology) MinguoChronology(java.time.chrono.MinguoChronology) JapaneseChronology(java.time.chrono.JapaneseChronology) Clock(java.time.Clock) Test(org.testng.annotations.Test)

Example 27 with Chronology

use of java.time.chrono.Chronology in project jdk8u_jdk by JetBrains.

the class TCKChronology method test_JapaneseChronology_dateNow.

@Test
public void test_JapaneseChronology_dateNow() {
    ZoneId zoneId_paris = ZoneId.of("Europe/Paris");
    Clock clock = Clock.system(zoneId_paris);
    Chronology chrono = Chronology.of("Japanese");
    assertEquals(chrono.dateNow(), JapaneseChronology.INSTANCE.dateNow());
    assertEquals(chrono.dateNow(zoneId_paris), JapaneseChronology.INSTANCE.dateNow(zoneId_paris));
    assertEquals(chrono.dateNow(clock), JapaneseChronology.INSTANCE.dateNow(clock));
}
Also used : ZoneId(java.time.ZoneId) HijrahChronology(java.time.chrono.HijrahChronology) ThaiBuddhistChronology(java.time.chrono.ThaiBuddhistChronology) IsoChronology(java.time.chrono.IsoChronology) Chronology(java.time.chrono.Chronology) MinguoChronology(java.time.chrono.MinguoChronology) JapaneseChronology(java.time.chrono.JapaneseChronology) Clock(java.time.Clock) Test(org.testng.annotations.Test)

Example 28 with Chronology

use of java.time.chrono.Chronology in project jdk8u_jdk by JetBrains.

the class TestReducedParser method test_reducedWithChronoYearOfEra.

@Test(dataProvider = "ReducedWithChrono")
public void test_reducedWithChronoYearOfEra(ChronoLocalDate date) {
    Chronology chrono = date.getChronology();
    DateTimeFormatter df = new DateTimeFormatterBuilder().appendValueReduced(YEAR_OF_ERA, 2, 2, LocalDate.of(2000, 1, 1)).toFormatter().withChronology(chrono);
    int expected = date.get(YEAR_OF_ERA);
    String input = df.format(date);
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(input, pos);
    int actual = parsed.get(YEAR_OF_ERA);
    assertEquals(actual, expected, String.format("Wrong date parsed, chrono: %s, input: %s", chrono, input));
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) HijrahChronology(java.time.chrono.HijrahChronology) ThaiBuddhistChronology(java.time.chrono.ThaiBuddhistChronology) IsoChronology(java.time.chrono.IsoChronology) Chronology(java.time.chrono.Chronology) MinguoChronology(java.time.chrono.MinguoChronology) JapaneseChronology(java.time.chrono.JapaneseChronology) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 29 with Chronology

use of java.time.chrono.Chronology in project jdk8u_jdk by JetBrains.

the class DateTimePrintContext method adjust.

private static TemporalAccessor adjust(final TemporalAccessor temporal, DateTimeFormatter formatter) {
    // normal case first (early return is an optimization)
    Chronology overrideChrono = formatter.getChronology();
    ZoneId overrideZone = formatter.getZone();
    if (overrideChrono == null && overrideZone == null) {
        return temporal;
    }
    // ensure minimal change (early return is an optimization)
    Chronology temporalChrono = temporal.query(TemporalQueries.chronology());
    ZoneId temporalZone = temporal.query(TemporalQueries.zoneId());
    if (Objects.equals(overrideChrono, temporalChrono)) {
        overrideChrono = null;
    }
    if (Objects.equals(overrideZone, temporalZone)) {
        overrideZone = null;
    }
    if (overrideChrono == null && overrideZone == null) {
        return temporal;
    }
    // make adjustment
    final Chronology effectiveChrono = (overrideChrono != null ? overrideChrono : temporalChrono);
    if (overrideZone != null) {
        // if have zone and instant, calculation is simple, defaulting chrono if necessary
        if (temporal.isSupported(INSTANT_SECONDS)) {
            Chronology chrono = (effectiveChrono != null ? effectiveChrono : IsoChronology.INSTANCE);
            return chrono.zonedDateTime(Instant.from(temporal), overrideZone);
        }
        // block changing zone on OffsetTime, and similar problem cases
        if (overrideZone.normalized() instanceof ZoneOffset && temporal.isSupported(OFFSET_SECONDS) && temporal.get(OFFSET_SECONDS) != overrideZone.getRules().getOffset(Instant.EPOCH).getTotalSeconds()) {
            throw new DateTimeException("Unable to apply override zone '" + overrideZone + "' because the temporal object being formatted has a different offset but" + " does not represent an instant: " + temporal);
        }
    }
    final ZoneId effectiveZone = (overrideZone != null ? overrideZone : temporalZone);
    final ChronoLocalDate effectiveDate;
    if (overrideChrono != null) {
        if (temporal.isSupported(EPOCH_DAY)) {
            effectiveDate = effectiveChrono.date(temporal);
        } else {
            // check for date fields other than epoch-day, ignoring case of converting null to ISO
            if (!(overrideChrono == IsoChronology.INSTANCE && temporalChrono == null)) {
                for (ChronoField f : ChronoField.values()) {
                    if (f.isDateBased() && temporal.isSupported(f)) {
                        throw new DateTimeException("Unable to apply override chronology '" + overrideChrono + "' because the temporal object being formatted contains date fields but" + " does not represent a whole date: " + temporal);
                    }
                }
            }
            effectiveDate = null;
        }
    } else {
        effectiveDate = null;
    }
    // this better handles map-like underlying temporal instances
    return new TemporalAccessor() {

        @Override
        public boolean isSupported(TemporalField field) {
            if (effectiveDate != null && field.isDateBased()) {
                return effectiveDate.isSupported(field);
            }
            return temporal.isSupported(field);
        }

        @Override
        public ValueRange range(TemporalField field) {
            if (effectiveDate != null && field.isDateBased()) {
                return effectiveDate.range(field);
            }
            return temporal.range(field);
        }

        @Override
        public long getLong(TemporalField field) {
            if (effectiveDate != null && field.isDateBased()) {
                return effectiveDate.getLong(field);
            }
            return temporal.getLong(field);
        }

        @SuppressWarnings("unchecked")
        @Override
        public <R> R query(TemporalQuery<R> query) {
            if (query == TemporalQueries.chronology()) {
                return (R) effectiveChrono;
            }
            if (query == TemporalQueries.zoneId()) {
                return (R) effectiveZone;
            }
            if (query == TemporalQueries.precision()) {
                return temporal.query(query);
            }
            return query.queryFrom(this);
        }
    };
}
Also used : ChronoLocalDate(java.time.chrono.ChronoLocalDate) DateTimeException(java.time.DateTimeException) TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) ZoneId(java.time.ZoneId) ChronoField(java.time.temporal.ChronoField) TemporalQuery(java.time.temporal.TemporalQuery) Chronology(java.time.chrono.Chronology) IsoChronology(java.time.chrono.IsoChronology) ZoneOffset(java.time.ZoneOffset)

Example 30 with Chronology

use of java.time.chrono.Chronology in project jdk8u_jdk by JetBrains.

the class TCKThaiBuddhistChronology method test_chrono_byLocale_oldTH_variant.

@Test
public void test_chrono_byLocale_oldTH_variant() {
    Chronology test = Chronology.ofLocale(new Locale("th", "TH", "TH"));
    Assert.assertEquals(test.getId(), "ISO");
    Assert.assertEquals(test, IsoChronology.INSTANCE);
}
Also used : Locale(java.util.Locale) ThaiBuddhistChronology(java.time.chrono.ThaiBuddhistChronology) IsoChronology(java.time.chrono.IsoChronology) Chronology(java.time.chrono.Chronology) MinguoChronology(java.time.chrono.MinguoChronology) Test(org.testng.annotations.Test)

Aggregations

Chronology (java.time.chrono.Chronology)114 IsoChronology (java.time.chrono.IsoChronology)95 Test (org.testng.annotations.Test)66 ThaiBuddhistChronology (java.time.chrono.ThaiBuddhistChronology)58 MinguoChronology (java.time.chrono.MinguoChronology)54 HijrahChronology (java.time.chrono.HijrahChronology)47 JapaneseChronology (java.time.chrono.JapaneseChronology)46 ChronoLocalDate (java.time.chrono.ChronoLocalDate)42 LocalDate (java.time.LocalDate)39 Test (org.junit.Test)32 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)27 TemporalAmount (java.time.temporal.TemporalAmount)12 TemporalUnit (java.time.temporal.TemporalUnit)12 TemporalAccessor (java.time.temporal.TemporalAccessor)10 TemporalField (java.time.temporal.TemporalField)9 ZoneId (java.time.ZoneId)8 Locale (java.util.Locale)8 DateTimeException (java.time.DateTimeException)6 TemporalAdjuster (java.time.temporal.TemporalAdjuster)6 ArrayList (java.util.ArrayList)6