Search in sources :

Example 21 with ChronoZonedDateTime

use of java.time.chrono.ChronoZonedDateTime in project fastjson by alibaba.

the class Jdk8DateCodec method write.

private void write(SerializeWriter out, TemporalAccessor object, String format) {
    DateTimeFormatter formatter;
    if ("unixtime".equals(format)) {
        Instant instant = null;
        if (object instanceof ChronoZonedDateTime) {
            long seconds = ((ChronoZonedDateTime) object).toEpochSecond();
            out.writeInt((int) seconds);
            return;
        }
        if (object instanceof LocalDateTime) {
            long seconds = ((LocalDateTime) object).atZone(JSON.defaultTimeZone.toZoneId()).toEpochSecond();
            out.writeInt((int) seconds);
            return;
        }
    }
    if ("millis".equals(format)) {
        Instant instant = null;
        if (object instanceof ChronoZonedDateTime) {
            instant = ((ChronoZonedDateTime) object).toInstant();
        } else if (object instanceof LocalDateTime) {
            instant = ((LocalDateTime) object).atZone(JSON.defaultTimeZone.toZoneId()).toInstant();
        }
        if (instant != null) {
            long millis = instant.toEpochMilli();
            out.writeLong(millis);
            return;
        }
    }
    if (format == formatter_iso8601_pattern) {
        formatter = formatter_iso8601;
    } else {
        formatter = DateTimeFormatter.ofPattern(format);
    }
    String text = formatter.format((TemporalAccessor) object);
    out.writeString(text);
}
Also used : LocalDateTime(java.time.LocalDateTime) Instant(java.time.Instant) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 22 with ChronoZonedDateTime

use of java.time.chrono.ChronoZonedDateTime in project j2objc by google.

the class TestFormatter method test.

@Test(dataProvider = "calendarsByLocale")
public void test(String calendarLocale) {
    failure = 0;
    int N = 12;
    // locales = Locale.getAvailableLocales();
    Locale[] locales = new Locale[] { Locale.ENGLISH, Locale.FRENCH, Locale.JAPANESE, Locale.CHINESE };
    Random r = new Random();
    Locale calLocale = Locale.forLanguageTag(calendarLocale);
    Chronology chrono = Chronology.ofLocale(calLocale);
    ChronoLocalDate now = chrono.dateNow();
    ChronoLocalDateTime<?> ldt0 = now.atTime(LocalTime.now());
    // Android-changed: use hard-coded zone id instead of system default.
    ChronoZonedDateTime<?> zdt0 = ldt0.atZone(ZoneId.of("America/Los_Angeles"));
    ChronoZonedDateTime<?>[] zdts = new ChronoZonedDateTime<?>[] { zdt0, zdt0.withZoneSameLocal(ZoneId.of("UTC")), zdt0.withZoneSameLocal(ZoneId.of("GMT")), zdt0.withZoneSameLocal(ZoneId.of("UT")) };
    while (N-- > 0) {
        for (ChronoZonedDateTime<?> zdt : zdts) {
            zdt = zdt.with(ChronoField.DAY_OF_YEAR, (r.nextInt(365) + 1)).with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
            Instant instant = zdt.toInstant();
            // Android-changed: Calendar.getInstance() only returns GregorianCalendar.
            // Manually get a JapaneseImperialCalendar for the test.
            TimeZone tz = TimeZone.getTimeZone(zdt.getZone());
            Calendar cal;
            if (calLocale.getLanguage().equals("ja")) {
                cal = Calendar.getJapaneseImperialInstance(tz, calLocale);
            } else {
                cal = Calendar.getInstance(tz, calLocale);
            }
            cal.setTimeInMillis(instant.toEpochMilli());
            for (Locale locale : locales) {
                for (String fmtStr : fmtStrDate) {
                    testDate(fmtStr, locale, zdt, cal);
                }
                for (String fmtStr : fmtStrTime) {
                    testTime(fmtStr, locale, zdt, cal);
                }
                testZoneId(locale, zdt, cal);
                testInstant(locale, instant, zdt, cal);
            }
        }
    }
    if (verbose) {
        if (failure != 0) {
            System.out.println("Total " + failure + "/" + total + " tests failed");
        } else {
            System.out.println("All tests (" + total + ") PASSED");
        }
    }
    assertEquals(failure, 0);
}
Also used : ChronoLocalDate(java.time.chrono.ChronoLocalDate) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) Instant(java.time.Instant) Chronology(java.time.chrono.Chronology) Test(org.testng.annotations.Test)

Example 23 with ChronoZonedDateTime

use of java.time.chrono.ChronoZonedDateTime in project j2objc by google.

the class TestFormatter method testInstant.

private void testInstant(Locale locale, Instant instant, ChronoZonedDateTime<?> zdt, Calendar cal) {
    String fmtStr = "s:[%ts] s:[%1$Ts] Q:[%1$tQ] Q:[%1$TQ]";
    printFmtStr(locale, fmtStr);
    String expected = test(fmtStr, locale, null, cal);
    test(fmtStr, locale, expected, instant);
    test(fmtStr, locale, expected, zdt);
    if (zdt instanceof ZonedDateTime) {
        OffsetDateTime odt = ((ZonedDateTime) zdt).toOffsetDateTime();
        test(fmtStr, locale, expected, odt);
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) OffsetDateTime(java.time.OffsetDateTime)

Example 24 with ChronoZonedDateTime

use of java.time.chrono.ChronoZonedDateTime in project assertj-core by joel-costigliola.

the class ChronoZonedDateTimeByInstantComparatorTest method should_disregard_chronology_difference.

@Test
void should_disregard_chronology_difference() {
    ZonedDateTime now = ZonedDateTime.now();
    ZonedDateTime inTokyo = now.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
    ChronoZonedDateTime<JapaneseDate> inTokyoJapanese = JapaneseChronology.INSTANCE.zonedDateTime(now);
    assertThat(inTokyoJapanese.compareTo(inTokyo)).as("Built-in comparison should report that they differ").isNotZero();
    assertThat(comparator.compare(inTokyoJapanese, inTokyo)).isZero();
}
Also used : ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) ZonedDateTime(java.time.ZonedDateTime) JapaneseDate(java.time.chrono.JapaneseDate) Test(org.junit.jupiter.api.Test)

Aggregations

ChronoZonedDateTime (java.time.chrono.ChronoZonedDateTime)24 ZonedDateTime (java.time.ZonedDateTime)16 OffsetDateTime (java.time.OffsetDateTime)10 Test (org.testng.annotations.Test)7 Instant (java.time.Instant)5 LocalTime (java.time.LocalTime)5 ChronoLocalDate (java.time.chrono.ChronoLocalDate)5 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)4 Chronology (java.time.chrono.Chronology)4 Test (org.junit.Test)4 DateTimeException (java.time.DateTimeException)3 LocalDateTime (java.time.LocalDateTime)3 ChronoLocalDateTime (java.time.chrono.ChronoLocalDateTime)3 TemporalAccessor (java.time.temporal.TemporalAccessor)3 TemporalField (java.time.temporal.TemporalField)3 Entry (java.util.Map.Entry)3 LocalDate (java.time.LocalDate)2 OffsetTime (java.time.OffsetTime)2 YearMonth (java.time.YearMonth)2 IsoChronology (java.time.chrono.IsoChronology)2