-2

I am developing a simple android app and I need convert a string which contains date and time to date in java, android. Here is an example format of my string:

Sun May 20 18:07:13 EEST 2018

And here is how I try to convert it to date:

SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date date = formatter.parse("Sun May 20 18:07:13 EEST 2018");

This is the error message I get. Where am I doing wrong??

W/System.err: java.text.ParseException: Unparseable date: "Sun May 20 18:07:13 EEST 2018" (at offset 0)
JollyRoger
  • 737
  • 1
  • 12
  • 38
  • Idk. Works just fine: https://www.ideone.com/ZFd7lG – denvercoder9 May 20 '18 at 15:17
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. May 20 '18 at 17:37
  • @OleV.V. I will give it a try, thanks body. – JollyRoger May 20 '18 at 17:43
  • 1
    As Ole V.V. commented, the troublesome date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the *java.time* classes. Much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque May 20 '18 at 20:06
  • Always search Stack Overflow before posting. You can assume all the basic date-time questions have already been asked and answered. – Basil Bourque May 20 '18 at 20:07

1 Answers1

0

Try this:

SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        Date date = null;
        try {
            date = formatter.parse("Sun May 20 18:07:13 EEST 2018");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(date);
Volodymyr T
  • 102
  • 9
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque May 20 '18 at 20:06