1

I'm trying to convert a string to date but every time i do it keeps throwing errors at me, Im not sure what i am missing

    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
    try {
        // m brings in all variables from a get/setter
        date = format.parse(m.getEventTime());
    } catch (ParseException e) {
        e.printStackTrace();
        Log.d("Response.Error.Date", m.getEventTime());
    }
    eventTime.setText(date.toString());

My variable m.getEventTime() is passing the following string 2018-04-28 14:00:00

I have tried passing the string as 2018-04-28T14:00:00Z to no avail.

No errors are coming from the stack trace from the try/catch block but the log is printing out D/Response.Error.Date: 2017-08-19 15:00:00 when i add e.toString() to the log it prints out D/Response.Error.Date: 2017-08-19 15:00:00 java.text.ParseException: Unparseable date: "2017-08-19 15:00:00"

On the actual application when run the time is shown as now Sat Apr 28 08:22:33 GMT+01:00 2018

Am i missing something?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
nats
  • 67
  • 1
  • 8
  • 1
    The simple date format should me "yyyy-MM-dd hh:mm:ss" to parse that string – Jyoti JK Apr 28 '18 at 07:47
  • @JyotiJK Thanks, i thought `new SimpleDateFormat("EEE, MMM d, yy hh:mm a"` was how to parse - changing to your way allowed me to actually parse the date - but now how would i change it to the `EEE, MMM d, yy hh:mm a` format? – nats Apr 28 '18 at 08:10
  • 1
    after getting date, again change the date to this format `new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);`. And the result will be in string not date. – Jyoti JK Apr 28 '18 at 08:11
  • A `Date` doesn’t have a format, so to obtain your desired format you need to format it into a new string using a new formatter. 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. Apr 28 '18 at 09:13
  • I would consider it quite a serious problem that you cannot see a stacktrace being printed. You should look into fixing your project setup so you can see what your program is trying to tell you about errors that have occurred. – Ole V.V. Apr 28 '18 at 09:16

3 Answers3

2

You can try this,

Date date = new Date();
SimpleDateFormat oldformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
try {
    // m brings in all variables from a get/setter
    date = oldformat.parse(m.getEventTime());
} catch (ParseException e) {
    e.printStackTrace();
    Log.d("Response.Error.Date", m.getEventTime());
}
eventTime.setText(format.format(date));
Jyoti JK
  • 2,141
  • 1
  • 17
  • 40
1

You can get time format like this:

    String dateTime = null;

    DateFormat df = new SimpleDateFormat("dd-MM-yyyy, hh:mm a");
                              //here you can use your required format 

    dateTime = df.format(new Date(stringTime));
Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
0

You are converting date into English which is causing the exception:

Please Change:

SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);

eventTime.setText(date.toString());

with

SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a");

eventTime.setText(format.format(date));
  • Incorrect answer, sorry. With your change the same exception is still thrown. – Ole V.V. Apr 28 '18 at 09:17
  • Sorry! Please change "eventTime.setText(date.toString());" to "eventTime.setText(format.format(date));" too. – Prakhar Gupta Apr 28 '18 at 10:13
  • Prakhar Gupta, if you think you have an answer to the reason for the `ParseException`, you may [edit your answer](https://stackoverflow.com/posts/50074700/edit). This is better than adding information in a comment. – Ole V.V. Apr 28 '18 at 14:43