-3

How do I parse this String in Java as date "2017-06-12T14:45:00+05:30" I've tried using SimpleDateFormat but it is throwing exception. Thanks

3 Answers3

2

It seems to be a valid ISO offset date time format, so this should work without requiring a formatter:

OffsetDateTime date = OffsetDateTime.parse(input);

If you really need a java.util.Date, you can then use:

Date legacyDate = Date.from(date.toInstant());
assylias
  • 321,522
  • 82
  • 660
  • 783
1
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.ENGLISH);
Date date = format.parse(string);

date is your solution

user1516873
  • 5,060
  • 2
  • 37
  • 56
  • No it says Unparseable date: "2017-06-12T14:45:00+05:30" – dhananjay sengraphwar Jun 05 '17 at 09:46
  • I recommend you stay away from the long outdated classes like `DateFormat`, `SimpleDateFormat` and `Date`. The modern classes lke `OffsetDateTime` are so much more programmer friendly. An added bonus is the mentioned class parses the ISO 8601 string from the question out of the box with no explicit format. – Ole V.V. Jun 05 '17 at 13:23
0

Just provide correct pattern in SimpleDateForamt

    String startDateString = "09/23/2009";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
Date startDate;
try {
    startDate = df.parse(startDateString);
    String newDateString = df.format(startDate);
    System.out.println(newDateString);
} catch (ParseException e) {
    e.printStackTrace();
}
Pradeep Singh
  • 1,094
  • 1
  • 9
  • 24