将TZ格式的字符串转换为日期

use*_*713 16 java datetime date simpledateformat

可能的解决方案:将Java Date转换为另一个Time as Date格式

我经历过但没有得到答案.

我有一个字符串" 2013-07-17T03:58:00.000Z ",我想将它转换为我们在制作新Date()时获得的相同形式的日期.日期d =新Date();

时间应该在IST.Zone-Asia/Kolkata

因此,上面字符串的日期应该是

7月17日星期三12:05:16 IST 2013 //无论什么时间按照印度标准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);
Run Code Online (Sandbox Code Playgroud)

Mar*_*k M 22

使用日历表示时区.

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();
Run Code Online (Sandbox Code Playgroud)

为此,我建议Joda Time,因为它具有更好的功能.对于JodaTime,你可以这样做:

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();
Run Code Online (Sandbox Code Playgroud)