Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP DateTime to Java Date

I have a PHP web service sending JSON responses back to my Java client. One of the fields is a DateTime value. However, I'm having problems translating the serialized PHP Date/Time to a Java Date.

For example, here is a date stored in my database:

2011-12-07 15:03:01

Here is how it's encoded in the JSON response:

1323288181

I suspected this would be the milliseconds since the Unix epoch, but when I construct a Java Date with that given value, the date turns out to be the following:

Fri Jan 16 01:34:48 CST 1970

Obviously it's not milliseconds since January 1, 1970 at midnight.

How do I go about doing this?

like image 903
Tyler Treat Avatar asked Apr 26 '26 19:04

Tyler Treat


1 Answers

Looks like that's seconds since the Unix epoch - so just multiply your value by 1000 when passing it to the Date constructor.

Note that Date.toString() will always use the system time zone, but a Date really represents an instant in time, so it doesn't have a time zone.

If you're doing anything significant with dates and times, I'd thoroughly recommend using Joda Time instead of the classes in java.util.

like image 188
Jon Skeet Avatar answered Apr 29 '26 09:04

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!