将ISO8601 T-Z格式的字符串转换为日期 [英] Converting String in ISO8601 T-Z format to Date

查看:416
本文介绍了将ISO8601 T-Z格式的字符串转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的解决方案:将Java日期转换为另一个时间作为日期格式

我经历了但没有得到答案。

I went through it but does not get my answer.

我有一个字符串 2013-07-17T03:58:00.000Z ,我想将其转换为我们制作新Date()时获得的相同格式的日期。Dated = new Date();

I have a string "2013-07-17T03:58:00.000Z" and I want to convert it into date of the same form which we get while making a new Date().Date d=new Date();

时间应该在IST区域-亚洲/加尔各答

The time should be in IST Zone - Asia/Kolkata

因此上述字符串的日期应为

Thus the date for the string above should be

星期三Jul 17 12:05:16 IST 2013 //根据印度标准GMT + 0530,无论什么时间

Wed Jul 17 12:05:16 IST 2013 //Whatever Time as per Indian Standard GMT+0530

String s="2013-07-17T03:58:00.000Z";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 
TimeZone tx=TimeZone.getTimeZone("Asia/Kolkata");
formatter.setTimeZone(tx);
d= (Date)formatter.parse(s);


推荐答案

使用日历作为时区。

TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
Calendar cal = Calendar.getInstance(tz);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
sdf.setCalendar(cal);
cal.setTime(sdf.parse("2013-07-17T03:58:00.000Z"));
Date date = cal.getTime();

为此,我建议 Joda Time ,因为它在这种情况下具有更好的功能。对于JodaTime,您可以执行以下操作:

For this however I'd recommend Joda Time as it has better functions for this situation. For JodaTime you can do something like this:

DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateTime dt = dtf.parseDateTime("2013-07-17T03:58:00.000Z");
Date date = dt.toDate();

这篇关于将ISO8601 T-Z格式的字符串转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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