2011-06-13 66 views
8

我從JSON訂閱源獲取大量日期。使用Android上的GSON將JSON日期格式解析爲字符串

他們看起來像這樣:\/Date(1307972400000+0200)\/

我需要解析這些日期,使用Java小時和分鐘。

編輯:

這是從來就多遠:

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

這給了我下面的輸出:1307972400000+0200)/

我怎樣可以去除這個字符串的剩餘部分,有什麼建議麼?

我希望它是這樣的:1307972400000L

回答

-1

我怎樣可以去除這個字符串的休息,有什麼建議?

我希望它是這樣的:1307972400000L

嚴格地說,你可以只從開始到「+」字符得到子。

String input = "/Date(1307972400000+0200)/"; 
String result = input.replaceAll("^/Date\\(" , ""); 
result = result.substring(0, result.indexOf('+')); 
System.out.println(new Long(result)); 
16

距離的.Net服務JSON飼料。我使用此代碼:

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() + ")/"); 
     } 
    } 
} 

它註冊自定義的串行器和解串爲Date類型。使用很簡單:Gson gson = GsonHelper.createWcfGson();並做你想做的。

Upd:對不起,前面的例子不適用於時區。使用Calendar來考慮時區偏移更容易。代碼如下所示:

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() + ")/"); 
     } 
    } 
} 

那麼你可以使用恢復Calendar對象,以獲取小時和分鐘(如果需要調整時區)。

calendar.get(Calendar.HOUR_OF_DAY); 
calendar.get(Calendar.MINUTE); 
+0

他應該調整時區(+0200) – Selvin 2011-06-13 13:40:36

+0

我不知道,如果I'm只是愚蠢,但我didn't得到這個工作 – Magnus 2011-06-13 14:26:04

+0

對不起,在你的情況下,它不會工作。此代碼不適用於時區。稍後我會嘗試改變它。 – 2011-06-13 14:30:06

0

要通過@Programmer布魯斯增強接受的答案,你可以簡單地使用:

public static Long getTime(String date) { 
    if (Utils.isEmpty(date)) 
     return -1l; 
    String t = date.substring(date.indexOf('(') + 1); 
    if (date.contains("+")) 
     t = t.substring(0, t.indexOf('+')); 
    else if (date.contains("-")) 
     t = t.substring(0, t.indexOf('-')); 
    else 
     t = t.substring(0, t.indexOf(')')); 
    return new Long(t); 
} 
相關問題