Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON date format to String using GSON on Android

I´m getting a lot of dates from a JSON feed.

They look like this: \/Date(1307972400000+0200)\/

I need to parse these dates into hours and minutes using Java.

EDIT:

This is how far I´ve come:

String s = dateString.replaceAll("^/Date\\(" , "");

This gives me the following output: 1307972400000+0200)/

How can I strip the rest of this string, any suggestions?

I want it to like this: 1307972400000L

like image 811
Magnus Avatar asked Jun 13 '11 13:06

Magnus


1 Answers

It is JSON feed from .Net service. I'm using this code:

public class GsonHelper {
    public static Gson createWcfGson() {
        GsonBuilder gsonb = new GsonBuilder();
        gsonb.registerTypeAdapter(Date.class, new WcfDateDeserializer());
        Gson gson = gsonb.create();
        return gson;
    }

    private static class WcfDateDeserializer implements JsonDeserializer<Date>, JsonSerializer<Date> {

        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            String JSONDateToMilliseconds = "\\/(Date\\((.*?)(\\+.*)?\\))\\/";
            Pattern pattern = Pattern.compile(JSONDateToMilliseconds);
            Matcher matcher = pattern.matcher(json.getAsJsonPrimitive().getAsString());
            String result = matcher.replaceAll("$2");
            return new Date(new Long(result));
    }

        @Override
        public JsonElement serialize(Date date, Type arg1, JsonSerializationContext arg2) {
            return new JsonPrimitive("/Date(" + date.getTime() + ")/");
        }
    }
}

It registers custom serializer and deserializer for Date type. Using is simple: Gson gson = GsonHelper.createWcfGson(); and do what you want.

Upd: Sorry, previous example doesn't work with timezones. It's easier to use Calendar to take into account timezone offset. Code will look like this:

public class GsonHelper {
    public static Gson createWcfGson() {
        GsonBuilder gsonb = new GsonBuilder();
        gsonb.registerTypeAdapter(Date.class, new WcfCalendarDeserializer ());
        Gson gson = gsonb.create();
        return gson;
    }

    public static class WcfCalendarDeserializer implements JsonDeserializer<Calendar>, JsonSerializer<Calendar> {

        public Calendar deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

            String JSONDateToMilliseconds = "\\/(Date\\((.*?)(\\+.*)?\\))\\/";
            Pattern pattern = Pattern.compile(JSONDateToMilliseconds);
            Matcher matcher = pattern.matcher(json.getAsJsonPrimitive().getAsString());
            matcher.matches();
            String tzone = matcher.group(3);
            String result = matcher.replaceAll("$2");

            Calendar calendar = new GregorianCalendar();
            calendar.setTimeZone(TimeZone.getTimeZone("GMT" + tzone));
            calendar.setTimeInMillis(new Long(result));

            return calendar;
        }

        @Override
        public JsonElement serialize(Calendar calendar, Type arg1, JsonSerializationContext arg2) {
            return new JsonPrimitive("/Date(" + calendar.getTimeInMillis() + ")/");
        }
    }
}

Then you can use returned Calendar object to get hours and minutes (and adjust timezone if needed).

calendar.get(Calendar.HOUR_OF_DAY);
calendar.get(Calendar.MINUTE);
like image 110
Sergey Glotov Avatar answered Sep 30 '22 01:09

Sergey Glotov