1

I am trying to convert this date object Fri Sep 21 08:00:00 SGT 2018 to this format yyyy-MM-dd hh:mm:ss date object.

        Date dt = sd1.parse(startTime);
        logger.info(dt);
        logger.info(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(dt.toString()));

The first log statement works, it returns me Fri Sep 21 08:00:00 SGT 2018 but the second log statement does not work.

It throws me an error

unparseable date Fri Sep 21 08:00:00 SGT 2018

What am i doing wrong here? My end goal is to get the date object in yyyy-MM-dd hh:mm:ss format.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
purplewind
  • 331
  • 10
  • 26
  • How are you expecting "Fri Sep 21 08:00:00 SGT 2018" to match "yyyy-MM-dd hh:mm:ss"? – jbx Sep 21 '18 at 09:04
  • You should use the `format` method of `SimpleDateFormat` and pass it the date object. – Markus Penguin Sep 21 '18 at 09:05
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 21 '18 at 09:09

1 Answers1

0

First you have to parse the date properly with :

    new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse("Fri Sep 21 08:00:00 SGT 2018");

So this should work:

    Date newDate = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse("Fri Sep 21 08:00:00 SGT 2018");
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS");
    String strDate = sdf.format(newDate.getTime());
    System.out.println(strDate);

But I suggest you to go with LocalDateTime

LocalDateTime ldt = LocalDateTime.parse("Fri Sep 21 08:00:00 SGT 2018", DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy"));      
System.out.println(ldt.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SS")));
user987339
  • 10,519
  • 8
  • 40
  • 45
  • 1
    Hi, I am able to get strDate but right now it is String format. I need it to be in Date format so that i can store it in my database. – purplewind Sep 21 '18 at 09:18
  • newDate is actually what you are looking for. – user987339 Sep 21 '18 at 09:22
  • erm, If you are using preparement statement. Can try something like this 'pstmt.setDate(1, new java.sql.Date(csvDateFormatter.parse(strDate).getTime()));' where csvDateFormatter is a new SimpleDateformat("YourDateFormat"). – 薛源少 Sep 21 '18 at 09:52
  • 1
    Whether you can store the `Date` into a database has nothing to do with its format. What is the datatype of the column you want to store it into? I’m sure we can help (though it seems to be a different question from the one you asked first). – Ole V.V. Sep 21 '18 at 10:00
  • @OleV.V. the datatype of the column is datetime. – purplewind Sep 21 '18 at 10:16
  • 1
    If you are using at least JDBC 4.2, I believe you may use `LocalDateTime` as in the last snippet and then `pstmt.setObject(1, ldt); `for saving the `LocalDateTime` to the database (where `pstmt` is your prepared statement and you need to substitute the correct parameter index instead of 1). Only beware of a potential time zone issue since the `LocalDateTime` doesn’t contain time zone information. – Ole V.V. Sep 21 '18 at 10:18
  • 1
    If using JDBC older than 4.2 and not willing to upgrade right now, use a `java.sql.Timestamp` for storing into your database through the prepared statement. There are many examples out there, just search. You may use `Timestamp.valueOf(ldt)` to obtain a `Timestamp` from the `LocalDateTime`. – Ole V.V. Sep 21 '18 at 10:23
  • The `LocalDateTime` class is the *wrong* class to be using for this purpose. Lacking any concept of time zone or offset-from-UTC, this class *cannot* represent a moment. For a moment, use `Instant` (UTC), `OffsetDateTime` (hours/minutes of an offset), or `ZonedDateTime` (a full time zone). – Basil Bourque Sep 23 '18 at 22:05
  • @purplewind There is no SQL-standard type of `datetime`. You should detail what database you are using and exactly what column data type you using as edits to your Question. – Basil Bourque Sep 23 '18 at 22:08