2014-12-05 01:16:24 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.google.gson.typeadapters;
|
|
|
|
|
2014-12-05 09:28:25 +01:00
|
|
|
import java.text.SimpleDateFormat;
|
2014-12-05 01:16:24 +01:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
2014-12-05 09:28:25 +01:00
|
|
|
import java.util.Locale;
|
2014-12-05 01:16:24 +01:00
|
|
|
import java.util.TimeZone;
|
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
|
|
|
|
public final class UtcDateTypeAdapterTest extends TestCase {
|
|
|
|
private final Gson gson = new GsonBuilder()
|
|
|
|
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter())
|
|
|
|
.create();
|
|
|
|
|
|
|
|
public void testLocalTimeZone() {
|
|
|
|
Date expected = new Date();
|
|
|
|
String json = gson.toJson(expected);
|
|
|
|
Date actual = gson.fromJson(json, Date.class);
|
|
|
|
assertEquals(expected.getTime(), actual.getTime());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testDifferentTimeZones() {
|
|
|
|
for (String timeZone : TimeZone.getAvailableIDs()) {
|
|
|
|
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
|
|
|
|
Date expected = cal.getTime();
|
|
|
|
String json = gson.toJson(expected);
|
|
|
|
// System.out.println(json + ": " + timeZone);
|
|
|
|
Date actual = gson.fromJson(json, Date.class);
|
|
|
|
assertEquals(expected.getTime(), actual.getTime());
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 08:41:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* JDK 1.7 introduced support for XXX format to indicate UTC date. But Android is older JDK.
|
|
|
|
* We want to make sure that this date is parseable in Android.
|
|
|
|
*/
|
|
|
|
public void testUtcDatesOnJdkBefore1_7() {
|
|
|
|
Gson gson = new GsonBuilder()
|
2014-12-05 09:28:25 +01:00
|
|
|
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter())
|
2014-12-05 08:41:08 +01:00
|
|
|
.create();
|
|
|
|
gson.fromJson("'2014-12-05T04:00:00.000Z'", Date.class);
|
|
|
|
}
|
2014-12-05 09:28:25 +01:00
|
|
|
|
|
|
|
public void testUtcWithJdk7Default() {
|
|
|
|
Date expected = new Date();
|
|
|
|
SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US);
|
|
|
|
iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
|
String expectedJson = "\"" + iso8601Format.format(expected) + "\"";
|
|
|
|
String actualJson = gson.toJson(expected);
|
|
|
|
assertEquals(expectedJson, actualJson);
|
|
|
|
Date actual = gson.fromJson(expectedJson, Date.class);
|
|
|
|
assertEquals(expected.getTime(), actual.getTime());
|
|
|
|
}
|
2014-12-05 01:16:24 +01:00
|
|
|
}
|