-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from kpartlow/master
Added Instant.parse before DateUtiltities.parse, made ZoneDateTime come back equivalent round trip.
- Loading branch information
Showing
9 changed files
with
140 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,15 @@ | |
import java.time.LocalTime; | ||
import java.time.OffsetDateTime; | ||
import java.time.OffsetTime; | ||
import java.time.ZoneOffset; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import com.cedarsoftware.util.CompactLinkedMap; | ||
|
||
/** | ||
* @author Kenny Partlow ([email protected]) | ||
* <br> | ||
|
@@ -34,11 +38,6 @@ | |
final class OffsetDateTimeConversions { | ||
private OffsetDateTimeConversions() {} | ||
|
||
static OffsetDateTime toDifferentZone(Object from, Converter converter) { | ||
OffsetDateTime offsetDateTime = (OffsetDateTime) from; | ||
return offsetDateTime.toInstant().atZone(converter.getOptions().getZoneId()).toOffsetDateTime(); | ||
} | ||
|
||
static Instant toInstant(Object from, Converter converter) { | ||
return ((OffsetDateTime)from).toInstant(); | ||
} | ||
|
@@ -48,15 +47,15 @@ static long toLong(Object from, Converter converter) { | |
} | ||
|
||
static LocalDateTime toLocalDateTime(Object from, Converter converter) { | ||
return toDifferentZone(from, converter).toLocalDateTime(); | ||
return ((OffsetDateTime)from).toLocalDateTime(); | ||
} | ||
|
||
static LocalDate toLocalDate(Object from, Converter converter) { | ||
return toDifferentZone(from, converter).toLocalDate(); | ||
return ((OffsetDateTime)from).toLocalDate(); | ||
} | ||
|
||
static LocalTime toLocalTime(Object from, Converter converter) { | ||
return toDifferentZone(from, converter).toLocalTime(); | ||
return ((OffsetDateTime)from).toLocalTime(); | ||
} | ||
|
||
static AtomicLong toAtomicLong(Object from, Converter converter) { | ||
|
@@ -98,4 +97,16 @@ static String toString(Object from, Converter converter) { | |
OffsetDateTime offsetDateTime = (OffsetDateTime) from; | ||
return offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME); | ||
} | ||
|
||
static Map<String, Object> toMap(Object from, Converter converter) { | ||
OffsetDateTime offsetDateTime = (OffsetDateTime) from; | ||
|
||
LocalDateTime localDateTime = offsetDateTime.toLocalDateTime(); | ||
ZoneOffset zoneOffset = offsetDateTime.getOffset(); | ||
|
||
Map<String, Object> target = new CompactLinkedMap<>(); | ||
target.put(MapConversions.DATE_TIME, converter.convert(localDateTime, String.class)); | ||
target.put(MapConversions.OFFSET, converter.convert(zoneOffset, String.class)); | ||
return target; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/test/java/com/cedarsoftware/util/convert/ZonedDateTimeConversionsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.cedarsoftware.util.convert; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import com.cedarsoftware.util.DeepEquals; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class ZonedDateTimeConversionsTests { | ||
|
||
private Converter converter; | ||
|
||
|
||
private static final ZoneId TOKYO = ZoneId.of("Asia/Tokyo"); | ||
private static final ZoneId CHICAGO = ZoneId.of("America/Chicago"); | ||
private static final ZoneId ALASKA = ZoneId.of("America/Anchorage"); | ||
|
||
private static final ZonedDateTime ZDT_1 = ZonedDateTime.of(LocalDateTime.of(2019, 12, 15, 9, 7, 16, 2000), CHICAGO); | ||
private static final ZonedDateTime ZDT_2 = ZonedDateTime.of(LocalDateTime.of(2027, 12, 23, 9, 7, 16, 2000), TOKYO); | ||
private static final ZonedDateTime ZDT_3 = ZonedDateTime.of(LocalDateTime.of(2027, 12, 23, 9, 7, 16, 2000), ALASKA); | ||
|
||
@BeforeEach | ||
public void before() { | ||
// create converter with default options | ||
this.converter = new Converter(new DefaultConverterOptions()); | ||
} | ||
|
||
private static Stream<Arguments> roundTripZDT() { | ||
return Stream.of( | ||
Arguments.of(ZDT_1), | ||
Arguments.of(ZDT_2), | ||
Arguments.of(ZDT_3) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("roundTripZDT") | ||
void testZonedDateTime(ZonedDateTime zdt) { | ||
|
||
String value = this.converter.convert(zdt, String.class); | ||
ZonedDateTime actual = this.converter.convert(value, ZonedDateTime.class); | ||
|
||
assertTrue(DeepEquals.deepEquals(actual, zdt)); | ||
|
||
value = DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zdt); | ||
actual = this.converter.convert(value, ZonedDateTime.class); | ||
|
||
assertTrue(DeepEquals.deepEquals(actual, zdt)); | ||
} | ||
} |