java SimpleDateFormat [英] java SimpleDateFormat

查看:129
本文介绍了java SimpleDateFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,如何解析包含不代表模式的字母的日期字符串?

in Java, how to parse a date string that contains a letter that does not represent a pattern?


"2007-11-02T14:46:03+01:00"




String date ="2007-11-02T14:46:03+01:00";
String format = "yyyy-MM-ddTHH:mm:ssz";
new SimpleDateFormat(format).parse(date);

Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'T'
    at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:769)
    at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:576)
    at java.text.SimpleDateFormat.(SimpleDateFormat.java:501)
    at java.text.SimpleDateFormat.(SimpleDateFormat.java:476)


推荐答案

您尝试解析的时间似乎在 ISO 8601格式 SimpleDateFormat 不幸的是不支持与ISO 8601所有相同的时区说明符。如果要能够正确处理ISO中指定的所有表单,最好的做法使用 Joda时间

The time you're trying to parse appears to be in ISO 8601 format. SimpleDateFormat unfortunately doesn't support all the same timezone specifiers as ISO 8601. If you want to be able to properly handle all the forms specified in the ISO, the best thing to do is use Joda time.

此示例直接出现在用户指南中:

DateTime dt = new DateTime("2004-12-13T21:39:45.618-08:00");

这篇关于java SimpleDateFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆