Java从字符串中解析带时区的日期 [英] Java parsing date with timezone from a string

查看:76
本文介绍了Java从字符串中解析带时区的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从格式为"31-12-2014 18:09 +05:30" 的字符串中解析带时区的日期.我尝试使用"d-MM-yyyy HH:mm ZZ" "d-MM-yyyy HH:mm Z" 使用简单日期格式进行解析.但这给了我一个无法比喻的日期例外.这该怎么做?请帮助我.

I want to parse a date with timezone from a string in the format "31-12-2014 18:09 +05:30". I tried to parse using simple-Date-Format using "d-MM-yyyy HH:mm ZZ" and "d-MM-yyyy HH:mm Z". But it is giving me a un-parables date exception. How to do this? Please help me.

推荐答案

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm XXX");
Date d = sdf.parse("31-12-2014 18:09 +05:30");
System.out.println(d);

请注意,您不能在X rel ="noreferrer"> JDK7的SimpleDateFormat ,因为它是ISO 8601时区格式.

Note that you can't use X before SimpleDateFormat of JDK7, because it's the ISO 8601 time zone format.

在Java 6中,您只能使用 ZZZ ,但它不能与+05:30匹配,因为 Z 与RFC 822时区格式匹配

With Java 6 you can only use ZZZ but it won't match +05:30 because Z match RFC 822 time zone format

如果您使用的是Java 6,请参考以下答案:

If you're using Java 6, please refer to this answer : Converting ISO 8601-compliant String to java.util.Date

这篇关于Java从字符串中解析带时区的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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