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);
}
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);
}
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);
}
}
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();
}
Aggregations