Search in sources :

Example 86 with ZoneId

use of java.time.ZoneId in project tutorials by eugenp.

the class DaylightSavingTimeJavaTimeExamplesTest method givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone.

@Test
public void givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone() throws ParseException {
    ZoneId italianZoneId = ZoneId.of("Europe/Rome");
    LocalDateTime localDateTimeBeforeDST = LocalDateTime.of(2018, 3, 25, 1, 55);
    System.out.println(localDateTimeBeforeDST);
    assertThat(localDateTimeBeforeDST.toString()).isEqualTo("2018-03-25T01:55");
    ZonedDateTime zonedDateTimeBeforeDST = localDateTimeBeforeDST.atZone(italianZoneId);
    prettyPrint(zonedDateTimeBeforeDST);
    assertThat(zonedDateTimeBeforeDST.toString()).isEqualTo("2018-03-25T01:55+01:00[Europe/Rome]");
    ZonedDateTime zonedDateTimeAfterDST = zonedDateTimeBeforeDST.plus(10, ChronoUnit.MINUTES);
    prettyPrint(zonedDateTimeAfterDST);
    assertThat(zonedDateTimeAfterDST.toString()).isEqualTo("2018-03-25T03:05+02:00[Europe/Rome]");
    Long deltaBetweenDatesInMinutes = ChronoUnit.MINUTES.between(zonedDateTimeBeforeDST, zonedDateTimeAfterDST);
    assertThat(deltaBetweenDatesInMinutes).isEqualTo(10);
}
Also used : LocalDateTime(java.time.LocalDateTime) ZoneId(java.time.ZoneId) ZonedDateTime(java.time.ZonedDateTime) Test(org.junit.Test)

Example 87 with ZoneId

use of java.time.ZoneId in project tutorials by eugenp.

the class ConversionExample method main.

public static void main(String[] args) {
    Instant instantFromCalendar = GregorianCalendar.getInstance().toInstant();
    ZonedDateTime zonedDateTimeFromCalendar = new GregorianCalendar().toZonedDateTime();
    Date dateFromInstant = Date.from(Instant.now());
    GregorianCalendar calendarFromZonedDateTime = GregorianCalendar.from(ZonedDateTime.now());
    Instant instantFromDate = new Date().toInstant();
    ZoneId zoneIdFromTimeZone = TimeZone.getTimeZone("PST").toZoneId();
}
Also used : ZoneId(java.time.ZoneId) ZonedDateTime(java.time.ZonedDateTime) Instant(java.time.Instant) GregorianCalendar(java.util.GregorianCalendar) Date(java.util.Date)

Example 88 with ZoneId

use of java.time.ZoneId in project tutorials by eugenp.

the class UseZonedDateTimeUnitTest method givenZoneId_thenZonedDateTime.

@Test
public void givenZoneId_thenZonedDateTime() {
    ZoneId zoneId = ZoneId.of("Europe/Paris");
    ZonedDateTime zonedDatetime = zonedDateTime.getZonedDateTime(LocalDateTime.parse("2016-05-20T06:30"), zoneId);
    Assert.assertEquals(zoneId, ZoneId.from(zonedDatetime));
}
Also used : ZoneId(java.time.ZoneId) ZonedDateTime(java.time.ZonedDateTime) Test(org.junit.Test)

Example 89 with ZoneId

use of java.time.ZoneId in project tutorials by eugenp.

the class TimezoneDisplay method getTimeZoneList.

public List<String> getTimeZoneList(OffsetBase base) {
    Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
    LocalDateTime now = LocalDateTime.now();
    return availableZoneIds.stream().map(ZoneId::of).sorted(new ZoneComparator()).map(id -> String.format("(%s%s) %s", base, getOffset(now, id), id.getId())).collect(Collectors.toList());
}
Also used : LocalDateTime(java.time.LocalDateTime) List(java.util.List) LocalDateTime(java.time.LocalDateTime) Set(java.util.Set) ZoneOffset(java.time.ZoneOffset) Comparator(java.util.Comparator) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId)

Example 90 with ZoneId

use of java.time.ZoneId in project Java8 by huhuhuHR.

the class TimezonesTest method main.

public static void main(String[] args) {
    Set<String> set = ZoneId.getAvailableZoneIds();
    set.forEach(System.out::println);
    ZoneId zone1 = ZoneId.of("Europe/Berlin");
    ZoneId zone2 = ZoneId.of("Brazil/East");
    System.out.println("------------------------------");
    System.out.println(zone1.getRules());
    System.out.println(zone2.getRules());
}
Also used : ZoneId(java.time.ZoneId)

Aggregations

ZoneId (java.time.ZoneId)420 Test (org.junit.Test)137 ZonedDateTime (java.time.ZonedDateTime)108 LocalDateTime (java.time.LocalDateTime)69 LocalDate (java.time.LocalDate)55 Instant (java.time.Instant)49 Test (org.testng.annotations.Test)41 ZoneOffset (java.time.ZoneOffset)37 DateTimeFormatter (java.time.format.DateTimeFormatter)32 Date (java.util.Date)29 ArrayList (java.util.ArrayList)23 LocalTime (java.time.LocalTime)21 TemporalAccessor (java.time.temporal.TemporalAccessor)15 List (java.util.List)15 HashMap (java.util.HashMap)14 Clock (java.time.Clock)13 Collectors (java.util.stream.Collectors)13 DateTimeException (java.time.DateTimeException)12 Duration (java.time.Duration)12 OffsetDateTime (java.time.OffsetDateTime)11