cc.recommenders.utils.gson.ISO8601DateParser.java Source code

Java tutorial

Introduction

Here is the source code for cc.recommenders.utils.gson.ISO8601DateParser.java

Source

/**
 * Copyright (c) 2010 Darmstadt University of Technology.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Marcel Bruch - initial API and implementation.
 */
package cc.recommenders.utils.gson;

import java.lang.reflect.Type;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.bind.DatatypeConverter;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class ISO8601DateParser implements JsonDeserializer<Date>, JsonSerializer<Date> {

    @Override
    public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
            throws JsonParseException {

        final String date = json.getAsJsonPrimitive().getAsString();
        final Calendar calendar = DatatypeConverter.parseDateTime(date);
        return calendar.getTime();
    }

    @Override
    public JsonElement serialize(final Date src, final Type typeOfSrc, final JsonSerializationContext context) {
        final Calendar calendar = GregorianCalendar.getInstance();
        calendar.setTime(src);
        return new JsonPrimitive(DatatypeConverter.printDateTime(calendar));
    }

}