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
Asked
Active
Viewed 246 times
-3
-
What java version you're using? – user1516873 Jun 05 '17 at 09:48
-
1Possible duplicate of [Java string to date conversion](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – jvwilge Jun 05 '17 at 12:02
-
Possible duplicate of [Parsing ISO-8601 DateTime in java](https://stackoverflow.com/questions/16336643/parsing-iso-8601-datetime-in-java) – Ole V.V. Jun 05 '17 at 13:30
3 Answers
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

Gianfrancesco Aurecchia
- 750
- 5
- 23
-
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
-
-
1try pattern like new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.ENGLISH); – Pradeep Singh Jun 05 '17 at 09:49