验证时间戳格式yyyy-MM-dd'T'HH:mm:ssZ在java?
问题描述:
我正在尝试使用 joda时间1.6.2 进行时间戳记验证。请指出我的错误,并帮助我。
代码
I'm trying to do a timestamp validation using joda time-1.6.2. Please point my error and help me out. Code
String timestamp = "2014-09-23T23:03:11Z";
String datePattern = "yyyy-MM-dd'T'HH:mm:ssZ";
try {
DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(datePattern);
dateFormatter.parseDateTime(timestamp);
} catch (Exception e) {
LOG.info("Timestamp is invalid format" + e);
}
异常
INFO: Timestamp is invalid formatjava.lang.IllegalArgumentException: Invalid format: "2014-09-23T23:03:11Z" is malformed at "Z"
答
String timestamp = "2014-09-23T23:03:11Z";
DateTime dt = new DateTime(timestamp, DateTimeZone.UTC);
原始答案
/ p>
Original Answer
String timestamp = "2014-09-23T23:03:11Z";
String datePattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
请注意 Z
上的单引号如果您的时间戳记以 Z
(您可以将其退出)结束,则必需。
Note the single quotes on Z
which are necessary if your timestamp string ends with a Z
(othewise you can leave them out).