Adding Examples for ZonedDateTime, OffsetDateTime and OffsetTime

This commit is contained in:
RanjeetKaur17 2018-09-12 20:09:27 +04:00
parent 9dc52f5630
commit 33e7ce89ce
7 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1 @@
Main-Class: com.baeldung.manifest.AppExample

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