3

Why am I getting an error below? It happens only with some dates.

 Helper.getDate("2014-01-09T17:10:14Z", "dd-MM-yyyy");

public static Date getDate(String date, String format) {
    DateFormat df = new SimpleDateFormat(format);

    try {
        return df.parse(date);
    } catch (ParseException e) {
        log.error("getDate", e);
    }

    return null;
}
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341

3 Answers3

6

This date:

"2014-01-09T17:10:14Z"

has a format of "yyyy-MM-dd'T'HH:mm:ss'Z'"

(or in Java 7 "yyyy-MM-dd'T'HH:mm:ssX")

So of course it will be impossible to parse it with a format dd-MM-yyyy.

The formatting string needs to be compatible with the input!

(Note I gave a literal Z, this is back compatible. Java 7 actually understands ISO8601 and you could use an X there to have it actually be parsed.)

Affe
  • 47,174
  • 11
  • 83
  • 83
0

That string 2014-01-09T17:10:14Z is standard ISO 8601 format for date-times.

The Joda-Time 2.3 library takes such standard strings directly in its DateTime constructor. No need for formatters/parsers.

Easy one liner…
(Feed standard string to constructor of DateTime, then convert the DateTime instance to a java.util.Date instance)

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;

java.util.Date date = new DateTime( "2014-01-09T17:10:14Z" ).toDate();

Dump to console, and show DateTime values for comparison…

System.out.println( "date: " + date );
System.out.println( "dateTime: " + new DateTime( "2014-01-09T17:10:14Z" ) );
System.out.println( "dateTime in UTC: " + new DateTime( "2014-01-09T17:10:14Z" ).toDateTime( DateTimeZone.UTC ) );

When run…

date: Thu Jan 09 09:10:14 PST 2014
dateTime: 2014-01-09T09:10:14.000-08:00
dateTime in UTC: 2014-01-09T17:10:14.000Z
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Already has lot of answer but just wanted to update with java 8.

Generally we face two problems with dates

  1. Parsing String to Date
  2. Display Date in desired string format

DateTimeFormatter class in Java 8 can be used for both of these purpose. Below methods try to provide solution to these issues.

Method 1: Convert your UTC string to Instant. Using Instant you can create Date for any time-zone by providing time-zone string and use DateTimeFormatter to format date for display as you wish.

String dateString = "2016-07-13T18:08:50.118Z";
String tz = "America/Mexico_City";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a");
ZoneId zoneId = ZoneId.of(tz);

Instant instant = Instant.parse(dateString);

ZonedDateTime dateTimeInTz =ZonedDateTime.ofInstant(instant, zoneId);
System.out.println(dateTimeInTz.format(dtf));

Method 2:

Use DateTimeFormatter built in constants e.g ISO_INSTANT to parse string to LocalDate. ISO_INSTANT can parse dates of pattern

yyyy-MM-dd'T'HH:mm:ssX e.g '2011-12-03T10:15:30Z'

LocalDate parsedDate
  = LocalDate.parse(dateString, DateTimeFormatter.ISO_INSTANT);

DateTimeFormatter displayFormatter = DateTimeFormatter.ofPattern("yyyy MM dd");
System.out.println(parsedDate.format(displayFormatter));

Method 3:

If your date string has much precision of time e.g it captures fraction of seconds as well as in this case 2016-07-13T18:08:50.118Z then method 1 will work but method 2 will not work. If you try to parse it will throw DateTimeException Since ISO_INSTANT formatter will not be able to parse fraction of seconds as you can see from its pattern. In this case you will have to create a custom DateTimeFormatter by providing date pattern as below.

LocalDate localDate 
= LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX"));
WitVault
  • 23,445
  • 19
  • 103
  • 133