Java Code Examples for java.time.ZonedDateTime#withZoneSameLocal()

The following examples show how to use java.time.ZonedDateTime#withZoneSameLocal() . 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: BaseDateTime.java    From jphp with Apache License 2.0 5 votes vote down vote up
public static DateTimeParseResult parse(Environment env, TraceInfo traceInfo, Memory time, Memory timeZone,
                                        ZonedDateTime baseDateTime) throws DateTimeParserException {
    String timeStr = time.isNull() ? "now" : time.toString();
    baseDateTime = baseDateTime.withZoneSameLocal(toZoneId(env, traceInfo, timeZone));
    if ("now".equals(timeStr)) {
        return new DateTimeParseResult(baseDateTime, Collections.emptySet(), null, null);
    } else {
        return new DateTimeParser(timeStr, baseDateTime).parseResult();
    }
}
 
Example 2
Source File: TCKZonedDateTime.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZoneSameLocal() {
    LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0);
    ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100);
    ZonedDateTime test = base.withZoneSameLocal(ZONE_0200);
    assertEquals(test.toLocalDateTime(), base.toLocalDateTime());
}
 
Example 3
Source File: TestZoneTextPrinterParser.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void test_printText() {
    Random r = RandomFactory.getRandom();
    int N = 8;
    Locale[] locales = Locale.getAvailableLocales();
    Set<String> zids = ZoneRulesProvider.getAvailableZoneIds();
    ZonedDateTime zdt = ZonedDateTime.now();

    //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size());
    while (N-- > 0) {
        zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
                 .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
        for (String zid : zids) {
            if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) {
                continue;      // TBD: match jdk behavior?
            }
            zdt = zdt.withZoneSameLocal(ZoneId.of(zid));
            TimeZone tz = TimeZone.getTimeZone(zid);
            boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli()));
            for (Locale locale : locales) {
                String longDisplayName = tz.getDisplayName(isDST, TimeZone.LONG, locale);
                String shortDisplayName = tz.getDisplayName(isDST, TimeZone.SHORT, locale);
                if ((longDisplayName.startsWith("GMT+") && shortDisplayName.startsWith("GMT+"))
                        || (longDisplayName.startsWith("GMT-") && shortDisplayName.startsWith("GMT-"))) {
                    printText(locale, zdt, TextStyle.FULL, tz, tz.getID());
                    printText(locale, zdt, TextStyle.SHORT, tz, tz.getID());
                    continue;
                }
                printText(locale, zdt, TextStyle.FULL, tz,
                        tz.getDisplayName(isDST, TimeZone.LONG, locale));
                printText(locale, zdt, TextStyle.SHORT, tz,
                        tz.getDisplayName(isDST, TimeZone.SHORT, locale));
            }
        }
    }
}
 
Example 4
Source File: TestZoneTextPrinterParser.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void test_printText() {
    Random r = RandomFactory.getRandom();
    int N = 8;
    Locale[] locales = Locale.getAvailableLocales();
    Set<String> zids = ZoneRulesProvider.getAvailableZoneIds();
    ZonedDateTime zdt = ZonedDateTime.now();

    //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size());
    while (N-- > 0) {
        zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
                 .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
        for (String zid : zids) {
            if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) {
                continue;      // TBD: match jdk behavior?
            }
            zdt = zdt.withZoneSameLocal(ZoneId.of(zid));
            TimeZone tz = TimeZone.getTimeZone(zid);
            boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli()));
            for (Locale locale : locales) {
                String longDisplayName = tz.getDisplayName(isDST, TimeZone.LONG, locale);
                String shortDisplayName = tz.getDisplayName(isDST, TimeZone.SHORT, locale);
                if ((longDisplayName.startsWith("GMT+") && shortDisplayName.startsWith("GMT+"))
                        || (longDisplayName.startsWith("GMT-") && shortDisplayName.startsWith("GMT-"))) {
                    printText(locale, zdt, TextStyle.FULL, tz, tz.getID());
                    printText(locale, zdt, TextStyle.SHORT, tz, tz.getID());
                    continue;
                }
                printText(locale, zdt, TextStyle.FULL, tz,
                        tz.getDisplayName(isDST, TimeZone.LONG, locale));
                printText(locale, zdt, TextStyle.SHORT, tz,
                        tz.getDisplayName(isDST, TimeZone.SHORT, locale));
            }
        }
    }
}
 
Example 5
Source File: TestZoneTextPrinterParser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void test_printText() {
    Random r = RandomFactory.getRandom();
    int N = 8;
    Locale[] locales = Locale.getAvailableLocales();
    Set<String> zids = ZoneRulesProvider.getAvailableZoneIds();
    ZonedDateTime zdt = ZonedDateTime.now();

    //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size());
    while (N-- > 0) {
        zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
                 .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
        for (String zid : zids) {
            if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) {
                continue;      // TBD: match jdk behavior?
            }
            zdt = zdt.withZoneSameLocal(ZoneId.of(zid));
            TimeZone tz = TimeZone.getTimeZone(zid);
            boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli()));
            for (Locale locale : locales) {
                String longDisplayName = tz.getDisplayName(isDST, TimeZone.LONG, locale);
                String shortDisplayName = tz.getDisplayName(isDST, TimeZone.SHORT, locale);
                if ((longDisplayName.startsWith("GMT+") && shortDisplayName.startsWith("GMT+"))
                        || (longDisplayName.startsWith("GMT-") && shortDisplayName.startsWith("GMT-"))) {
                    printText(locale, zdt, TextStyle.FULL, tz, tz.getID());
                    printText(locale, zdt, TextStyle.SHORT, tz, tz.getID());
                    continue;
                }
                printText(locale, zdt, TextStyle.FULL, tz,
                        tz.getDisplayName(isDST, TimeZone.LONG, locale));
                printText(locale, zdt, TextStyle.SHORT, tz,
                        tz.getDisplayName(isDST, TimeZone.SHORT, locale));
            }
        }
    }
}
 
Example 6
Source File: TCKZonedDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZoneSameLocal() {
    LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0);
    ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100);
    ZonedDateTime test = base.withZoneSameLocal(ZONE_0200);
    assertEquals(test.toLocalDateTime(), base.toLocalDateTime());
}
 
Example 7
Source File: TCKZonedDateTime.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void test_withZoneSameLocal_retainOffset1() {
    LocalDateTime ldt = LocalDateTime.of(2008, 11, 2, 1, 30, 59, 0);  // overlap
    ZonedDateTime base = ZonedDateTime.of(ldt, ZoneId.of("UTC-04:00") );
    ZonedDateTime test = base.withZoneSameLocal(ZoneId.of("America/New_York"));
    assertEquals(base.getOffset(), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(), ZoneOffset.ofHours(-4));
}
 
Example 8
Source File: TCKZonedDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZoneSameLocal_retainOffset2() {
    LocalDateTime ldt = LocalDateTime.of(2008, 11, 2, 1, 30, 59, 0);  // overlap
    ZonedDateTime base = ZonedDateTime.of(ldt, ZoneId.of("UTC-05:00") );
    ZonedDateTime test = base.withZoneSameLocal(ZoneId.of("America/New_York"));
    assertEquals(base.getOffset(), ZoneOffset.ofHours(-5));
    assertEquals(test.getOffset(), ZoneOffset.ofHours(-5));
}
 
Example 9
Source File: TCKZonedDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZoneSameLocal_retainOffset1() {
    LocalDateTime ldt = LocalDateTime.of(2008, 11, 2, 1, 30, 59, 0);  // overlap
    ZonedDateTime base = ZonedDateTime.of(ldt, ZoneId.of("UTC-04:00") );
    ZonedDateTime test = base.withZoneSameLocal(ZoneId.of("America/New_York"));
    assertEquals(base.getOffset(), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(), ZoneOffset.ofHours(-4));
}
 
Example 10
Source File: TestZoneTextPrinterParser.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void test_printText() {
    Random r = new Random();
    int N = 50;
    Locale[] locales = Locale.getAvailableLocales();
    Set<String> zids = ZoneRulesProvider.getAvailableZoneIds();
    ZonedDateTime zdt = ZonedDateTime.now();

    //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size());
    while (N-- > 0) {
        zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
                 .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
        for (String zid : zids) {
            if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) {
                continue;      // TBD: match jdk behavior?
            }
            zdt = zdt.withZoneSameLocal(ZoneId.of(zid));
            TimeZone tz = TimeZone.getTimeZone(zid);
            boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli()));
            for (Locale locale : locales) {
                printText(locale, zdt, TextStyle.FULL, tz,
                        tz.getDisplayName(isDST, TimeZone.LONG, locale));
                printText(locale, zdt, TextStyle.SHORT, tz,
                        tz.getDisplayName(isDST, TimeZone.SHORT, locale));
            }
        }
    }
}
 
Example 11
Source File: TCKZonedDateTime.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZoneSameLocal() {
    LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0);
    ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100);
    ZonedDateTime test = base.withZoneSameLocal(ZONE_0200);
    assertEquals(test.toLocalDateTime(), base.toLocalDateTime());
}
 
Example 12
Source File: TCKZonedDateTime.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZoneSameLocal_retainOffset1() {
    LocalDateTime ldt = LocalDateTime.of(2008, 11, 2, 1, 30, 59, 0);  // overlap
    ZonedDateTime base = ZonedDateTime.of(ldt, ZoneId.of("UTC-04:00") );
    ZonedDateTime test = base.withZoneSameLocal(ZoneId.of("America/New_York"));
    assertEquals(base.getOffset(), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(), ZoneOffset.ofHours(-4));
}
 
Example 13
Source File: TCKZonedDateTime.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZoneSameLocal_noChange() {
    LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0);
    ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100);
    ZonedDateTime test = base.withZoneSameLocal(ZONE_0100);
    assertEquals(test, base);
}
 
Example 14
Source File: TestZoneTextPrinterParser.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void test_printText() {
    Random r = RandomFactory.getRandom();
    int N = 8;
    Locale[] locales = Locale.getAvailableLocales();
    Set<String> zids = ZoneRulesProvider.getAvailableZoneIds();
    ZonedDateTime zdt = ZonedDateTime.now();

    //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size());
    while (N-- > 0) {
        zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
                 .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
        for (String zid : zids) {
            if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) {
                continue;      // TBD: match jdk behavior?
            }
            zdt = zdt.withZoneSameLocal(ZoneId.of(zid));
            TimeZone tz = TimeZone.getTimeZone(zid);
            boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli()));
            for (Locale locale : locales) {
                String longDisplayName = tz.getDisplayName(isDST, TimeZone.LONG, locale);
                String shortDisplayName = tz.getDisplayName(isDST, TimeZone.SHORT, locale);
                if ((longDisplayName.startsWith("GMT+") && shortDisplayName.startsWith("GMT+"))
                        || (longDisplayName.startsWith("GMT-") && shortDisplayName.startsWith("GMT-"))) {
                    printText(locale, zdt, TextStyle.FULL, tz, tz.getID());
                    printText(locale, zdt, TextStyle.SHORT, tz, tz.getID());
                    continue;
                }
                printText(locale, zdt, TextStyle.FULL, tz,
                        tz.getDisplayName(isDST, TimeZone.LONG, locale));
                printText(locale, zdt, TextStyle.SHORT, tz,
                        tz.getDisplayName(isDST, TimeZone.SHORT, locale));
            }
        }
    }
}
 
Example 15
Source File: TCKZonedDateTime.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void test_withZoneSameLocal_retainOffset2() {
    LocalDateTime ldt = LocalDateTime.of(2008, 11, 2, 1, 30, 59, 0);  // overlap
    ZonedDateTime base = ZonedDateTime.of(ldt, ZoneId.of("UTC-05:00") );
    ZonedDateTime test = base.withZoneSameLocal(ZoneId.of("America/New_York"));
    assertEquals(base.getOffset(), ZoneOffset.ofHours(-5));
    assertEquals(test.getOffset(), ZoneOffset.ofHours(-5));
}
 
Example 16
Source File: TCKZonedDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZoneSameLocal_retainOffset1() {
    LocalDateTime ldt = LocalDateTime.of(2008, 11, 2, 1, 30, 59, 0);  // overlap
    ZonedDateTime base = ZonedDateTime.of(ldt, ZoneId.of("UTC-04:00") );
    ZonedDateTime test = base.withZoneSameLocal(ZoneId.of("America/New_York"));
    assertEquals(base.getOffset(), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(), ZoneOffset.ofHours(-4));
}
 
Example 17
Source File: TCKZonedDateTime.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZoneSameLocal_retainOffset1() {
    LocalDateTime ldt = LocalDateTime.of(2008, 11, 2, 1, 30, 59, 0);  // overlap
    ZonedDateTime base = ZonedDateTime.of(ldt, ZoneId.of("UTC-04:00") );
    ZonedDateTime test = base.withZoneSameLocal(ZoneId.of("America/New_York"));
    assertEquals(base.getOffset(), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(), ZoneOffset.ofHours(-4));
}
 
Example 18
Source File: TCKZonedDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_withZoneSameLocal_null() {
    LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0);
    ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100);
    base.withZoneSameLocal(null);
}
 
Example 19
Source File: TCKZonedDateTime.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_withZoneSameLocal_null() {
    LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0);
    ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100);
    base.withZoneSameLocal(null);
}
 
Example 20
Source File: TimeUtils.java    From brein-time-utilities with Apache License 2.0 4 votes vote down vote up
public static ZonedDateTime fromZoneToZone(final ZonedDateTime ts,
                                           final ZoneId zone,
                                           final ZoneId toZone) {
    return ts.withZoneSameLocal(toZone);
}