コード例 #1
0
  @SuppressWarnings("deprecation")
  @Test
  public void testSerializeDate() throws Exception {

    int year = 1990;
    int month = 11;
    int day = 15;
    int hour = 10;
    int minute = 50;
    int seconds = 30;

    Date date = new Date(Date.UTC(year - 1900, month - 1, day, hour, minute, seconds));

    DateSerializer dateSerializer = new DateSerializer();

    String strDate = dateSerializer.serialize(date, Date.class, null).toString();

    // yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z
    String expectedDate =
        String.format("\"%s-%d-%sT%s:%s:%s.000Z\"", year, month, day, hour, minute, seconds);

    assertEquals(expectedDate, strDate);
  }
コード例 #2
0
  @SuppressWarnings("deprecation")
  @Test
  public void testDeserializeDate() throws Exception {

    String strDate = "1990-11-15T10:50:30.123Z";

    DateSerializer dateSerializer = new DateSerializer();

    Date date = dateSerializer.deserialize(new JsonPrimitive(strDate), Date.class, null);

    int year = 1990;
    int month = 11;
    int day = 15;
    int hour = 10;
    int minute = 50;
    int seconds = 30;

    assertEquals(year - 1900, date.getYear());
    assertEquals(month - 1, date.getMonth());
    assertEquals(day, date.getDate());
    assertEquals(hour - date.getTimezoneOffset() / 60, date.getHours());
    assertEquals(minute, date.getMinutes());
    assertEquals(seconds, date.getSeconds());
  }