Skip to content

Commit 5a3ff6a

Browse files
committed
Added ZoneId preference so ZoneDateTime comes back equivalent
1 parent b8aec84 commit 5a3ff6a

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

src/main/java/com/cedarsoftware/util/DateUtilities.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,10 @@ public static ZonedDateTime parseDate(String dateStr, ZoneId defaultZoneId, bool
260260
tz = matcher.group(5).trim();
261261
}
262262
if (matcher.group(6) != null) {
263-
if (StringUtilities.isEmpty(tz)) { // Only use timezone name when offset is not used
264-
tz = stripBrackets(matcher.group(6).trim());
265-
}
263+
// to make round trip of ZonedDateTime equivalent we need to use the original Zone as ZoneId
264+
// ZoneId is a much broader definition handling multiple possible dates, and we want this to
265+
// be equivalent to the original zone that was used if one was present.
266+
tz = stripBrackets(matcher.group(6).trim());
266267
}
267268
}
268269

src/main/java/com/cedarsoftware/util/convert/MapConversions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ static ZoneId toZoneId(Object from, Converter converter) {
378378
ConverterOptions options = converter.getOptions();
379379
ZoneId zoneId = converter.convert(map.get(ZONE), ZoneId.class);
380380
return zoneId;
381+
} else if (map.containsKey(ID)) {
382+
ConverterOptions options = converter.getOptions();
383+
ZoneId zoneId = converter.convert(map.get(ID), ZoneId.class);
384+
return zoneId;
381385
} else {
382386
return fromSingleKey(from, converter, ZONE, ZoneId.class);
383387
}

src/test/java/com/cedarsoftware/util/TestDateUtilities.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package com.cedarsoftware.util;
22

3-
import org.junit.jupiter.api.Test;
4-
import org.junit.jupiter.params.ParameterizedTest;
5-
import org.junit.jupiter.params.provider.Arguments;
6-
import org.junit.jupiter.params.provider.MethodSource;
7-
import org.junit.jupiter.params.provider.ValueSource;
8-
93
import java.lang.reflect.Constructor;
104
import java.lang.reflect.Modifier;
115
import java.text.SimpleDateFormat;
@@ -17,6 +11,12 @@
1711
import java.util.TimeZone;
1812
import java.util.stream.Stream;
1913

14+
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.params.ParameterizedTest;
16+
import org.junit.jupiter.params.provider.Arguments;
17+
import org.junit.jupiter.params.provider.MethodSource;
18+
import org.junit.jupiter.params.provider.ValueSource;
19+
2020
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
2222
import static org.junit.jupiter.api.Assertions.assertNotEquals;

src/test/java/com/cedarsoftware/util/convert/ZonedDateTimeConversionsTests.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import java.time.LocalDateTime;
44
import java.time.ZoneId;
55
import java.time.ZonedDateTime;
6+
import java.time.format.DateTimeFormatter;
67
import java.util.stream.Stream;
78

89
import org.junit.jupiter.api.BeforeEach;
910
import org.junit.jupiter.params.ParameterizedTest;
1011
import org.junit.jupiter.params.provider.Arguments;
1112
import org.junit.jupiter.params.provider.MethodSource;
1213

13-
import static org.assertj.core.api.Assertions.assertThat;
14+
import com.cedarsoftware.util.DeepEquals;
15+
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
1417

1518
class ZonedDateTimeConversionsTests {
1619

@@ -44,11 +47,13 @@ private static Stream<Arguments> roundTripZDT() {
4447
void testZonedDateTime(ZonedDateTime zdt) {
4548

4649
String value = this.converter.convert(zdt, String.class);
47-
System.out.println(value);
4850
ZonedDateTime actual = this.converter.convert(value, ZonedDateTime.class);
49-
System.out.println(actual);
5051

51-
assertThat(actual).isEqualTo(zdt);
52-
}
52+
assertTrue(DeepEquals.deepEquals(actual, zdt));
5353

54+
value = DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zdt);
55+
actual = this.converter.convert(value, ZonedDateTime.class);
56+
57+
assertTrue(DeepEquals.deepEquals(actual, zdt));
58+
}
5459
}

0 commit comments

Comments
 (0)