2

Appreciate there are lots of similar posts on this but I couldn't find a specific one to help.

I'm trying to convert this string to a Date in Java

2017-05-16 06:24:36-0700

But it fails each time with this code

Date Login = new SimpleDateFormat("dd/MM/yy HH:mm:ss").parse("2017-05-16 06:24:36-0700");

Now I'm presuming its due to the timezone info at the end - I just can't figure out how to set the format. I tried this but no luck

SimpleDateFormat("dd/MM/yy HH:mm:ssZ")

Any ideas?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
reviloRetals
  • 485
  • 1
  • 7
  • 14
  • Well, I suggest going here: http://stackoverflow.com/questions/18122608/simpledateformat-parse-loses-timezone?rq=1 – ItamarG3 May 16 '17 at 13:36
  • 5
    Your date has year-month-day with hyphens between and you try to parse it with a format that has the opposite order, day-month-year, and slashes instead of hyphens? – Ole V.V. May 16 '17 at 13:38
  • As far as I recall, the one-argument `SimpleDateFormat.parse()` would ignore any extra characters at the end of the string, like your time zone. So I doubt that its presence is your issue. – Ole V.V. May 16 '17 at 13:40
  • Similar Question: [Error java.time.format.DateTimeParseException: could not be parsed, unparsed text found at index 10](http://stackoverflow.com/q/39033525/642706) – Basil Bourque May 16 '17 at 23:34

2 Answers2

7

The date format passed to your SimpleDateFormat is "dd/MM/yy", while the date you are trying to parse is of the format "yyyy-MM-dd". Try this instead:

Date login = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ").parse("2017-05-16 06:24:36-0700");

As a side note, depending on which version of Java you are using, I would recommend using the new java.time package (JDK 1.8+) or the back port of that package (JDK 1.6+) instead of the outdated (no pun intended) Date and/or Calendar classes.

Instant login = Instant.from(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssZ").parse("2017-05-16 06:24:36-0700"));
Bryan
  • 14,756
  • 10
  • 70
  • 125
  • Yes you are correct, I had passed the date in the wrong format. Rookie error...thanks for the pointer – reviloRetals May 16 '17 at 14:38
  • In java.time, simpler to replace the SPACE in the middle with a `T` to comply with ISO 8601. Then parse directly: `OffsetDateTime.parse( "2017-05-16 06:24:36-0700".replace( " " , "T" ) )` – Basil Bourque May 16 '17 at 16:07
  • 1
    @BasilBourque, I could not get that to work in Java 8 (Text '2017-05-16T06:24:36-0700' could not be parsed at index 19). It needs a colon in the offset, `-07:00`. I think you’ve told us once that it works in Java 9, though. – Ole V.V. May 16 '17 at 20:40
  • 1
    @OleV.V. Yes indeed, as I recall now, failing to parse the offset lacking a colon is a bug in Java 8, fixed in Java 9. See [code run live at IdeOne.com](http://ideone.com/WOyoN8). – Basil Bourque May 16 '17 at 23:17
3

I have already upvoted Bryan’s answer exactly because it includes and recommends the java.time solution. I need to add a few thoughts, though.

Your code, reviloSlater, throws away the time zone information (more precsely, zone offset information), I’m not sure I would dare do that from the outset. With java.time classes it’s more natural to include it, and it’s easy to discard at a later point when we are sure we don’t need it.

To parse with offset:

    OffsetDateTime loginOdt = OffsetDateTime.parse("2017-05-16 06:24:36-0700",
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssZ"));

To drop the time zone offset information

    LocalDateTime loginLdt = loginOdt.toLocalDateTime();

A LocalDateTime is a date and a time without any time zone or offset information. In this case of course we get

2017-05-16T06:24:36

Bryan’s java.time code too uses the time zone offset information from the string. Edit: after Bryan’s edit that code now works and gives us:

2017-05-16T13:24:36Z

This is the same point in time (Instant.toString() prints the time in UTC). Another way is, with the OffsetDateTime from before we can just do

    Instant login = loginOdt.toInstant();

java.time is loaded with possibilities.

Community
  • 1
  • 1
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • FYI, related Question: [*Cannot parse String in ISO 8601 format, lacking colon in offset, to Java 8 Date*](https://stackoverflow.com/q/43360852/642706) – Basil Bourque Feb 03 '18 at 05:20