[英]Why java date is not parsable?
我正在使用oracle MAF开发移动应用程序。 Oracle MAF提供了日期组件,如果我选择一个日期,则输出为: 2015-06-16T04:35:00.000Z
用于选定的日期Jun 16, 2015 10:05 AM
2015-06-16T04:35:00.000Z
Jun 16, 2015 10:05 AM
。
我正在尝试使用.ical(ICalendar日期格式)将此格式转换为“印度标准时间”,对于所选日期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
您需要将UTC 2015-06-16T04:35:00.000Z
的值解析为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");
这给了我们一个Tue Jun 16 14:35:00 EST 2015
的java.util.Date
(对我来说)。
然后,您需要在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
只是因为这是一个好习惯...
// 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
,我已经有一个小时了,但是我愿意了解更好的方法
供应格式应为"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
格式的一些补充,以及日期字符串中的正确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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.