1

I have a given String 2019-04-17 10:00:43+02:00 and want to convert it to something like Mon Apr 15 12:05:47 CEST 2019

I tried:


String date = "2019-04-17T11:02:46+02:00"
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ssX ");
java.util.Date result = formatter.parse(date);

but it gives an Exception like below.

Exception in thread "main" java.text.ParseException: Unparseable date: "2019-04-17 11:02:46+02:00" at java.text.DateFormat.parse(Unknown Source)

vimuth
  • 5,064
  • 33
  • 79
  • 116
quinzo
  • 580
  • 2
  • 6
  • 21

2 Answers2

0

This is a default ISO format which can be parsed as is with OffsetDateTime:

String date = "2019-04-17T11:02:46+02:00";
OffsetDateTime odt = OffsetDateTime.parse(date);

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

Date legacyDate = Date.from(odt.toInstant());
assylias
  • 321,522
  • 82
  • 660
  • 783
0

the problem with your code is you have put a extra space in the formatter.

String date = "2019-04-17T11:02:46+02:00";
DateFormat format = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ssX");
Date rst = format.parse(date);
System.out.println(rst);

output :

Thu Jan 17 14:32:46 IST 2019
Jamith NImantha
  • 1,999
  • 2
  • 20
  • 27