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

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

问题描述

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

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

I went through it but doesnot get my answer.

我有一个字符串 2013-07-17T03:58:00.000Z ,我想将它转换为与新的Date()一样的形式的日期date d = 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.Zone-Asia / Kolkata

The time should be in IST.Zone-Asia/Kolkata

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

Thus the date for the string above should be

Wed 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();

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

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