我从JSON Feed获得了很多日期。
他们看起来像这样:\/Date(1307972400000+0200)\/
我需要使用Java将这些日期解析为小时和分钟。
修改
这是我走了多远:
String s = dateString.replaceAll("^/Date\\(" , "");
这给了我以下输出:1307972400000+0200)/
如何删除此字符串的其余部分,是否有任何建议?
我希望它像这样:1307972400000L
答案 0 :(得分: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();
并做你想做的事。
更新抱歉,上一个示例不适用于时区。使用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);
答案 1 :(得分:0)
为了增加@Programmer Bruce接受的答案,你可以简单地使用它:
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);
}
答案 2 :(得分:-1)
如何删除此字符串的其余部分,是否有任何建议?
我希望它像这样:1307972400000L
严格地说,你可以从开头到'+'字符获得子串。
String input = "/Date(1307972400000+0200)/";
String result = input.replaceAll("^/Date\\(" , "");
result = result.substring(0, result.indexOf('+'));
System.out.println(new Long(result));