Skip to content

Commit 6584041

Browse files
author
eugenp
committed
date work for gson
1 parent 782216f commit 6584041

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

gson/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@
1111

1212
<!-- utils -->
1313

14+
<dependency>
15+
<groupId>joda-time</groupId>
16+
<artifactId>joda-time</artifactId>
17+
<version>2.4</version>
18+
</dependency>
19+
1420
<dependency>
1521
<groupId>com.google.guava</groupId>
1622
<artifactId>guava</artifactId>
17-
<version>17.0</version>
23+
<version>18.0</version>
1824
</dependency>
1925
<dependency>
2026
<groupId>commons-io</groupId>

gson/src/test/java/org/baeldung/gson/serialization/test/GsonSerializationTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import static org.junit.Assert.assertTrue;
55

66
import java.lang.reflect.Type;
7+
import java.util.Calendar;
78
import java.util.Collection;
89
import java.util.Date;
10+
import java.util.GregorianCalendar;
911

1012
import org.baeldung.gson.serialization.DifferentNameSerializer;
1113
import org.baeldung.gson.serialization.IgnoringFieldsNotMatchingCriteriaSerializer;
1214
import org.baeldung.gson.serialization.IgnoringFieldsSerializer;
1315
import org.baeldung.gson.serialization.SourceClass;
16+
import org.joda.time.DateTime;
1417
import org.junit.Test;
1518

1619
import com.google.common.collect.Lists;
@@ -77,15 +80,16 @@ public void givenUsingCustomDeserializer_whenFieldNotMatchesCriteria_thenIgnored
7780

7881
@Test
7982
public void givenDate_whenSerializing_thenCorrect() {
80-
final Date sourceDate = new Date(1000000L);
83+
Date sourceDate = new DateTime().withYear(2000).withMonthOfYear(1).withDayOfMonth(1).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).toDate();
84+
8185
final Gson gson = new Gson();
82-
final Type sourceDateType = new TypeToken<Date>() {
86+
Type sourceDateType = new TypeToken<Date>() {
8387
}.getType();
84-
final String jsonDate = gson.toJson(sourceDate, sourceDateType);
88+
String jsonDate = gson.toJson(sourceDate, sourceDateType);
8589

8690
System.out.println("jsonDate:\n" + jsonDate);
87-
final String expectedResult = "\"Jan 1, 1970 3:16:40 AM\"";
88-
assertTrue(jsonDate.equals(expectedResult));
91+
String expectedResult = "\"Jan 1, 2000 12:00:00 AM\"";
92+
assertEquals(expectedResult, jsonDate);
8993
}
9094

9195
}

0 commit comments

Comments
 (0)