Skip to content

Commit

Permalink
Merge pull request eugenp#5293 from RanjeetKaur17/develop.0.3.0
Browse files Browse the repository at this point in the history
Changes to directly create ZoneOffset using offset value rather using…
  • Loading branch information
CalamarBicefalo authored Sep 19, 2018
2 parents 6f93afc + 4be74d0 commit 8cf4000
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
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);
public OffsetDateTime getCurrentTimeByZoneOffset(String offset) {
ZoneOffset zoneOffSet= ZoneOffset.of(offset);
OffsetDateTime date = OffsetDateTime.now(zoneOffSet);
return date;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
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);
public OffsetTime getCurrentTimeByZoneOffset(String offset) {
ZoneOffset zoneOffSet = ZoneOffset.of(offset);
OffsetTime time = OffsetTime.now(zoneOffSet);
return time;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import static org.junit.Assert.assertTrue;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;

import org.junit.Test;

Expand All @@ -14,12 +13,10 @@ public class OffsetDateTimeExampleUnitTest {

@Test
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
String zone = "Europe/Berlin";
OffsetDateTime time = offsetDateTimeExample.getCurrentTimeByZoneOffset(zone);
String offset = "+02:00";
OffsetDateTime time = offsetDateTimeExample.getCurrentTimeByZoneOffset(offset);

assertTrue(time.getOffset()
.equals(ZoneId.of(zone)
.getRules()
.getOffset(LocalDateTime.now())));
.equals(ZoneOffset.of(offset)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import static org.junit.Assert.assertTrue;

import java.time.LocalDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;

import org.junit.Test;

Expand All @@ -14,12 +13,10 @@ public class OffsetTimeExampleUnitTest {

@Test
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
String zone = "Europe/Berlin";
OffsetTime time = offsetTimeExample.getCurrentTimeByZoneOffset(zone);
String offset = "+02:00";
OffsetTime time = offsetTimeExample.getCurrentTimeByZoneOffset(offset);

assertTrue(time.getOffset()
.equals(ZoneId.of(zone)
.getRules()
.getOffset(LocalDateTime.now())));
.equals(ZoneOffset.of(offset)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ public class ZoneDateTimeExampleUnitTest {
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);

assertTrue(sourceDate.toInstant()
.compareTo(destDate.toInstant()) == 0);
}
}

0 comments on commit 8cf4000

Please sign in to comment.