Java Code Examples for java.time.ZoneId#equals()
The following examples show how to use
java.time.ZoneId#equals() .
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: PostgreSQLGuavaRangeType.java From hibernate-types with Apache License 2.0 | 6 votes |
/** * Creates the {@code ZonedDateTime} range from provided string: * <pre>{@code * Range<ZonedDateTime> closed = Range.zonedDateTimeRange("[2007-12-03T10:15:30+01:00\",\"2008-12-03T10:15:30+01:00]"); * Range<ZonedDateTime> quoted = Range.zonedDateTimeRange("[\"2007-12-03T10:15:30+01:00\",\"2008-12-03T10:15:30+01:00\"]"); * Range<ZonedDateTime> iso = Range.zonedDateTimeRange("[2011-12-03T10:15:30+01:00[Europe/Paris], 2012-12-03T10:15:30+01:00[Europe/Paris]]"); * }</pre> * <p> * The valid formats for bounds are: * <ul> * <li>yyyy-MM-dd HH:mm:ss[.SSSSSS]X</li> * <li>yyyy-MM-dd'T'HH:mm:ss[.SSSSSS]X</li> * </ul> * * @param rangeStr The range string, for example {@literal "[2011-12-03T10:15:30+01:00,2012-12-03T10:15:30+01:00]"}. * * @return The range of {@code ZonedDateTime}s. * * @throws DateTimeParseException when one of the bounds are invalid. * @throws IllegalArgumentException when bounds time zones are different. */ public static Range<ZonedDateTime> zonedDateTimeRange(String rangeStr) { Range<ZonedDateTime> range = ofString(rangeStr, parseZonedDateTime().compose(unquote()), ZonedDateTime.class); if (range.hasLowerBound() && range.hasUpperBound()) { ZoneId lowerZone = range.lowerEndpoint().getZone(); ZoneId upperZone = range.upperEndpoint().getZone(); if (!lowerZone.equals(upperZone)) { Duration lowerDst = ZoneId.systemDefault().getRules().getDaylightSavings(range.lowerEndpoint().toInstant()); Duration upperDst = ZoneId.systemDefault().getRules().getDaylightSavings(range.upperEndpoint().toInstant()); long dstSeconds = upperDst.minus(lowerDst).getSeconds(); if (dstSeconds < 0) { dstSeconds *= -1; } long zoneDriftSeconds = ((ZoneOffset) lowerZone).getTotalSeconds() - ((ZoneOffset) upperZone).getTotalSeconds(); if (zoneDriftSeconds < 0) { zoneDriftSeconds *= -1; } if (dstSeconds != zoneDriftSeconds) { throw new IllegalArgumentException("The upper and lower bounds must be in same time zone!"); } } } return range; }
Example 2
Source File: TwoPhaseCommitSinkFunctionTest.java From flink with Apache License 2.0 | 5 votes |
@Override public Clock withZone(ZoneId zone) { if (zone.equals(this.zoneId)) { return this; } return new SettableClock(zone, epochMilli); }
Example 3
Source File: TwoPhaseCommitSinkFunctionTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Override public Clock withZone(ZoneId zone) { if (zone.equals(this.zoneId)) { return this; } return new SettableClock(zone, epochMilli); }
Example 4
Source File: EpochSecondsDateFormatter.java From crate with Apache License 2.0 | 5 votes |
@Override public DateFormatter withZone(ZoneId zoneId) { if (zoneId.equals(ZoneOffset.UTC) == false) { throw new IllegalArgumentException(pattern() + " date formatter can only be in zone offset UTC"); } return this; }
Example 5
Source File: MockUtils.java From reactor-core with Apache License 2.0 | 5 votes |
@Override public Clock withZone(ZoneId zone) { if (zone.equals(this.zone)) { // intentional NPE return this; } return new VirtualClock(instant, zone); }
Example 6
Source File: TwoPhaseCommitSinkFunctionTest.java From flink with Apache License 2.0 | 5 votes |
@Override public Clock withZone(ZoneId zone) { if (zone.equals(this.zoneId)) { return this; } return new SettableClock(zone, epochMilli); }
Example 7
Source File: NanoClock.java From jenetics with Apache License 2.0 | 4 votes |
@Override public NanoClock withZone(final ZoneId zone) { return zone.equals(_zone) ? this : new NanoClock(zone); }
Example 8
Source File: JavatimeTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 9
Source File: JavatimeTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 10
Source File: StandardDateFormat.java From sis with Apache License 2.0 | 4 votes |
/** * Converts the given legacy {@code Date} object into a {@code java.time} implementation in given timezone. * The method performs the following choice: * * <ul> * <li>If the given date has zero values in hours, minutes, seconds and milliseconds fields in UTC timezone, * then the returned implementation will be a {@link LocalDate}, dropping the timezone information (i.e. * the date is considered an approximation). Note that this is consistent with ISO 19162 requirement that * dates are always in UTC, even if Apache SIS allows some flexibility.</li> * <li>Otherwise if the timezone is not {@code null} and not UTC, then this method returns an {@link OffsetDateTime}.</li> * <li>Otherwise this method returns a {@link LocalDateTime} in the given timezone.</li> * </ul> * * @param date the date to convert, or {@code null}. * @param zone the timezone of the temporal object to obtain, or {@code null} for UTC. * @return the temporal object for the given date, or {@code null} if the given argument was null. */ public static Temporal toHeuristicTemporal(final Date date, ZoneId zone) { if (date == null) { return null; } final long time = date.getTime(); if ((time % MILLISECONDS_PER_DAY) == 0) { return LocalDate.ofEpochDay(time / MILLISECONDS_PER_DAY); } final Instant instant = Instant.ofEpochMilli(time); if (zone == null) { zone = ZoneOffset.UTC; } else if (!zone.equals(ZoneOffset.UTC)) { return OffsetDateTime.ofInstant(instant, zone); } return LocalDateTime.ofInstant(instant, zone); }
Example 11
Source File: JavatimeTest.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 12
Source File: JavatimeTest.java From native-obfuscator with GNU General Public License v3.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 13
Source File: JavatimeTest.java From hottub with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 14
Source File: JavatimeTest.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 15
Source File: JavatimeTest.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 16
Source File: JavatimeTest.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 17
Source File: ColumnImpl.java From jackcess with Apache License 2.0 | 4 votes |
private static LocalDateTime toLocalDateTime( TemporalAccessor value, DateTimeContext dtc) { // handle some common Temporal types if(value instanceof LocalDateTime) { return (LocalDateTime)value; } if(value instanceof ZonedDateTime) { // if the temporal value has a timezone, convert it to this db's timezone return ((ZonedDateTime)value).withZoneSameInstant( dtc.getZoneId()).toLocalDateTime(); } if(value instanceof Instant) { return LocalDateTime.ofInstant((Instant)value, dtc.getZoneId()); } if(value instanceof LocalDate) { return ((LocalDate)value).atTime(BASE_LT); } if(value instanceof LocalTime) { return ((LocalTime)value).atDate(BASE_LD); } // generic handling for many other Temporal types try { LocalDate ld = value.query(TemporalQueries.localDate()); if(ld == null) { ld = BASE_LD; } LocalTime lt = value.query(TemporalQueries.localTime()); if(lt == null) { lt = BASE_LT; } ZoneId zone = value.query(TemporalQueries.zone()); if(zone != null) { // the Temporal has a zone, see if it is the right zone. if not, // adjust it ZoneId zoneId = dtc.getZoneId(); if(!zoneId.equals(zone)) { return ZonedDateTime.of(ld, lt, zone).withZoneSameInstant(zoneId) .toLocalDateTime(); } } return LocalDateTime.of(ld, lt); } catch(DateTimeException | ArithmeticException e) { throw new IllegalArgumentException( "Unsupported temporal type " + value.getClass(), e); } }
Example 18
Source File: JavatimeTest.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 19
Source File: JavatimeTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 20
Source File: JavatimeTest.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }