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?

4

2 Answers 2

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"));
Sign up to request clarification or add additional context in comments.

4 Comments

Yes you are correct, I had passed the date in the wrong format. Rookie error...thanks for the pointer
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" ) )
@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.
@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.
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.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.