How to parse DateTime property from AppEngine in J

2019-02-18 04:46发布

I need to parse DateTime string coming from AppEngine in Java (Android). The string looks like this: 2011-07-26 17:21:00+01:00. Is it some standard format? Is there simpler way than using custom SimpleDateFormat?

2条回答
时光不老,我们不散
2楼-- · 2019-02-18 05:17

What can be simpler than SimpleDateFormat in that case? You just have to get rid of that last nasty : in the timezone part.

Edit: The format is very close to a standard format.

This will get rid of the last nasty :, and parse the date:

String data = "2011-07-26 17:21:00+01:00";
data = data.replaceFirst("(.*):(..)", "$1$2");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ssZZZ");
Date parse = formatter.parse(data);
System.out.println("Got " + parse);
查看更多
闹够了就滚
3楼-- · 2019-02-18 05:19

SimpleDateFormat is pretty simple except that for the fact that your date string is a bit off because of the last : in there. Just replace the : and use the following pattern:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:ss:mmZ");
String dateStr = "2011-07-26 17:21:00+0100";
System.out.println(sdf.parse(dateStr));
查看更多
登录 后发表回答