Why java date is not parsable?

717 views Asked by At

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
3

There are 3 answers

0
MadProgrammer On BEST ANSWER

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

2
Ankur Singhal On

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
0
laune On

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