简体   繁体   中英

Why java date is not parsable?

I am developing a mobile application using 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 .

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 . 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

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).

Then, you need to format this in 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

Java 8 Time API

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

Supply format should be "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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM