Merge pull request #5234 from RanjeetKaur17/develop.0.3.0

Adding Examples for ZonedDateTime, OffsetDateTime and OffsetTime
This commit is contained in:
José Carlos Valero Sánchez 2018-09-16 23:37:07 +02:00 committed by GitHub
commit 096f17f30e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,17 @@
package com.baeldung.zoneddatetime;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class OffsetDateTimeExample {
public OffsetDateTime getCurrentTimeByZoneOffset(String region) {
LocalDateTime now = LocalDateTime.now();
ZoneId zone = ZoneId.of(region);
ZoneOffset zoneOffSet= zone.getRules().getOffset(now);
OffsetDateTime date = OffsetDateTime.now(zoneOffSet);
return date;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.zoneddatetime;
import java.time.LocalDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class OffsetTimeExample {
public OffsetTime getCurrentTimeByZoneOffset(String region) {
LocalDateTime now = LocalDateTime.now();
ZoneId zone = ZoneId.of(region);
ZoneOffset zoneOffSet = zone.getRules()
.getOffset(now);
OffsetTime time = OffsetTime.now(zoneOffSet);
return time;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.zoneddatetime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZoneDateTimeExample {
public ZonedDateTime getCurrentTimeByZoneId(String region) {
ZoneId zone = ZoneId.of(region);
ZonedDateTime date = ZonedDateTime.now(zone);
return date;
}
public ZonedDateTime convertZonedDateTime(ZonedDateTime sourceDate, String destZone) {
ZoneId destZoneId = ZoneId.of(destZone);
ZonedDateTime destDate = sourceDate.withZoneSameInstant(destZoneId);
return destDate;
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import org.junit.Test;
public class OffsetDateTimeExampleUnitTest {
OffsetDateTimeExample offsetDateTimeExample = new OffsetDateTimeExample();
@Test
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
String zone = "Europe/Berlin";
OffsetDateTime time = offsetDateTimeExample.getCurrentTimeByZoneOffset(zone);
assertTrue(time.getOffset()
.equals(ZoneId.of(zone)
.getRules()
.getOffset(LocalDateTime.now())));
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.LocalDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import org.junit.Test;
public class OffsetTimeExampleUnitTest {
OffsetTimeExample offsetTimeExample = new OffsetTimeExample();
@Test
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
String zone = "Europe/Berlin";
OffsetTime time = offsetTimeExample.getCurrentTimeByZoneOffset(zone);
assertTrue(time.getOffset()
.equals(ZoneId.of(zone)
.getRules()
.getOffset(LocalDateTime.now())));
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.junit.Test;
public class ZoneDateTimeExampleUnitTest {
ZoneDateTimeExample zoneDateTimeExample = new ZoneDateTimeExample();
@Test
public void givenZone_whenGetCurrentTime_thenResultHasZone() {
String zone = "Europe/Berlin";
ZonedDateTime time = zoneDateTimeExample.getCurrentTimeByZoneId(zone);
assertTrue(time.getZone()
.equals(ZoneId.of(zone)));
}
@Test
public void givenZones_whenConvertDateByZone_thenGetConstantDiff() {
String sourceZone = "Europe/Berlin";
String destZone = "Asia/Tokyo";
ZonedDateTime sourceDate = zoneDateTimeExample.getCurrentTimeByZoneId(sourceZone);
ZonedDateTime destDate = zoneDateTimeExample.convertZonedDateTime(sourceDate, destZone);
assertTrue(sourceDate.toInstant().compareTo(destDate.toInstant()) == 0);
}
}