[英]Why java date is not parsable?
I am developing a mobile application using oracle MAF. 我正在使用oracle MAF开发移动应用程序。 Oracle MAF provides its date component and if I select a date then output is like :
2015-06-16T04:35:00.000Z
for selected date Jun 16, 2015 10:05 AM
. 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
。
I am trying to convert this format to "Indian Standard Time" with .ical (ICalendar Date format) which should be like 20150613T100500
for the selected date Jun 16, 2015 10:05 AM
. 我正在尝试使用.ical(ICalendar日期格式)将此格式转换为“印度标准时间”,对于所选日期
Jun 16, 2015 10:05 AM
应该类似于20150613T100500
。 I am using code below: 我正在使用以下代码:
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
isoFormat.setTimeZone(TimeZone.getTimeZone("IST"));
String start_date_time = isoFormat.parse("20150616T043500000Z").toString();
But it returns date time as : 但是它返回日期时间为:
Tue Jun 16 04:35:00 GMT+5:30 2015
And should be like: 并且应该像:
20150616T100500
You need to parse the value from 2015-06-16T04:35:00.000Z
UTC to a java.util.Date
您需要将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");
Which gives us a java.util.Date
of Tue Jun 16 14:35:00 EST 2015
(for me). 这给了我们一个
Tue Jun 16 14:35:00 EST 2015
的java.util.Date
(对我来说)。
Then, you need to format this in IST 然后,您需要在IST中对其进行格式化
SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
outFormat.setTimeZone(TimeZone.getTimeZone("IST"));
String formatted = outFormat.format(start_date_time);
System.out.println(formatted);
Which outputs 20150616T100500
哪个输出
20150616T100500
Just because it's good practice... 只是因为这是一个好习惯...
// 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: If I didn't parse the value to LocalDateTime
, then convert it to UTC
, I was out by an hour, but I'm open to knowing better ways nb:如果我没有将值解析为
LocalDateTime
,然后将其转换为UTC
,我已经有一个小时了,但是我愿意了解更好的方法
Supply format should be "yyyy-MM-dd'T'HH:mm:ss"
供应格式应为
"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();
}
}
Output 产量
Tue Jun 16 04:35:00 IST 2015
20150616T043500
A few additions to the format, and a correct TZ in the date string: 格式的一些补充,以及日期字符串中的正确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.