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