2015-06-16 56 views
1

我正在开发一个使用oracle MAF的移动应用程序。 Oracle MAF提供日期组件,如果我选择日期,则输出如下:2015-06-16T04:35:00.000Z对于选定日期Jun 16, 2015 10:05 AM为什么java日期不可解析?

我试图用.ical(ICalendar日期格式)将此格式转换为“Indian Standard Time”,对于所选日期Jun 16, 2015 10:05 AM应该是20150613T100500。我使用下面的代码:

SimpleDateFormat isoFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); 
isoFormat.setTimeZone(TimeZone.getTimeZone("IST")); 
String start_date_time = isoFormat.parse("20150616T043500000Z").toString(); 

但它返回日期时间为:

Tue Jun 16 04:35:00 GMT+5:30 2015 

而且应该是这样的:

20150616T100500 
+0

模式'yyyyMMdd'T'HHmmss'与'2015-06-16T04:35:00.000Z '?也许'yyyy-MM-dd'T'HH:mm:ss.SSSSZ'可能会有效 – MadProgrammer

+0

对于'yyyyMMdd'T'HHmmss'的格式,您需要使用'20150613T100500'作为解析的值。 – MadProgrammer

+1

感谢解决的异常,我使用20150616T043500000Z作为字符串,但它返回“Tue Jun 16 04:35:00 GMT + 5:30 2015”,转换为像20150613T100500这样的块的方式是什么,以及它为什么显示时间为04 :35,而我的时间是10:05。 – Himanshu

回答

1

你需要从2015-06-16T04:35:00.000Z UTC解析值到java.util.Date

SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 
from.setTimeZone(TimeZone.getTimeZone("UTC")); 
Date start_date_time = from.parse("2015-06-16T04:35:00.000Z"); 

这给我们java.util.DateTue Jun 16 14:35:00 EST 2015(对我来说)。

然后,你需要在IST

SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); 
outFormat.setTimeZone(TimeZone.getTimeZone("IST")); 
String formatted = outFormat.format(start_date_time); 
System.out.println(formatted); 

,输出20150616T100500

的Java 8时API

只是因为它是很好的做法,格式化这个...

// No Time Zone 
    String from = "2015-06-16T04:35:00.000Z"; 
    LocalDateTime ldt = LocalDateTime.parse(from, DateTimeFormatter.ISO_ZONED_DATE_TIME); 

    // Convert it to UTC 
    ZonedDateTime zdtUTC = ZonedDateTime.of(ldt, ZoneId.systemDefault()).withZoneSameInstant(ZoneId.of("UTC")); 

    // Convert it to IST 
    ZonedDateTime zdtITC = zdtUTC.withZoneSameInstant(ZoneId.of("Indian/Cocos")); 
    String timestamp = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss").format(zdtITC); 
    System.out.println(timestamp); 

NB:如果我没有解析到LocalDateTime的值,则转换这是UTC,我出了一个小时,但我愿意知道更好的方式

0

供应格式应为"yyyy-MM-dd'T'HH:mm:ss"

public static void main(String[] args) { 
    SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
    isoFormat.setTimeZone(TimeZone.getTimeZone("IST")); 
    try { 
     Date start_date_time = isoFormat.parse("2015-06-16T04:35:00.000Z"); 
     System.out.println(start_date_time); 
     SimpleDateFormat output = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); 
     String formattedTime = output.format(start_date_time); 
     System.out.println(formattedTime); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 
} 

输出

Tue Jun 16 04:35:00 IST 2015 
20150616T043500 
+0

我想先详细地阅读一下这个问题 – MadProgrammer

+0

@MadProgrammer corrected –

+0

不应该以'20150616T100500'出现吗? – MadProgrammer

0

一些补充的格式,并在日期字符串正确的TZ:

SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz"); 
isoFormat.setTimeZone(TimeZone.getTimeZone("IST")); 
String start_date_time = isoFormat.parse("2015-06-16T04:35:00.000CEST").toString();