@Test public void testSerializeCalendar() throws Exception { int year = 1990; int month = 11; int day = 15; int hour = 10; int minute = 50; int seconds = 30; int milliseconds = 123; Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); calendar.set(year, month - 1, day, hour, minute, seconds); calendar.set(Calendar.MILLISECOND, milliseconds); CalendarSerializer calendarSerializer = new CalendarSerializer(); String strDate = calendarSerializer.serialize(calendar, Calendar.class, null).toString(); // yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z String expectedDate = String.format( "\"%s-%d-%sT%s:%s:%s.%sZ\"", year, month, day, hour, minute, seconds, milliseconds); assertEquals(expectedDate, strDate); }
@Test public void testDeserializeCalendar() throws Exception { String strDate = "1990-11-15T10:50:30.123Z"; CalendarSerializer calendarSerializer = new CalendarSerializer(); Calendar calendar = calendarSerializer.deserialize(new JsonPrimitive(strDate), Calendar.class, null); int year = 1990; int month = 11; int day = 15; int hour = 10; int minute = 50; int seconds = 30; int milliseconds = 123; calendar.setTimeZone(TimeZone.getTimeZone("UTC")); assertEquals(year, calendar.get(Calendar.YEAR)); assertEquals(month - 1, calendar.get(Calendar.MONTH)); assertEquals(day, calendar.get(Calendar.DAY_OF_MONTH)); assertEquals(hour, calendar.get(Calendar.HOUR_OF_DAY)); assertEquals(minute, calendar.get(Calendar.MINUTE)); assertEquals(seconds, calendar.get(Calendar.SECOND)); assertEquals(milliseconds, calendar.get(Calendar.MILLISECOND)); }