Java: Date parsing, why do I get an error

Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss.SSSZ”);//2018-02-05T18:00:51.001+0000 String text = dateFormat.format(date); try { Date test = dateFormat.parse(text); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } worked for me. With “SSSZ” instead of “SZ” at the end of the pattern.

java.text.ParseException: Unparseable date: java.text.DateFormat.parse(DateFormat.java:579)

Never use SimpleDateFormat or DateTimeFormatter without a Locale Since the given date-time is in English, you should use Locale.ENGLISH with your date-time parser; otherwise the parsing will fail in a system (computer, phone etc.) which is using a non-English type of locale. Also, note that the date-time API of java.util and their formatting API, SimpleDateFormat … Read more