Question

I'm using Java with hibernate and am in the early stages of learning how everything works. I'm getting there slowly but I have had some trouble with dates in Java.

My current problem is that when I get a timestamp from a MySQL database it seems to have a load of extra zeros on the end. When I pass this through to PHP (yes its a Java/PHP application we're writing) then formatting the date ends with an incorrect result.

Here's what I see:

2000-08-10 14:09:21 // Time in database
965912961000 // Timestamp from Java of this time
965912961 // The PHP equivalent of the database timestamp - using strtotime()

In the Java model I have the variable setup like this:

@Temporal (TemporalType.TIMESTAMP)
private Date logTime

So my question is how do I get rid of the three trailing zeros? Obviously I could remove these with PHP but I'd rather it came from the back-end Java if possible.

Was it helpful?

Solution

It's not clear why you're formatting the Date into an integer, but it's a simple fact of life that a Java Date is measured in milliseconds - whereas presumably a PHP one is measured in seconds (since the Unix epoch).

That's an internal representation though, which most of the time you shouldn't care about. If you're transmitting that integer, feel free to just divide by a thousand to convert it from a milliseconds-based representation to a seconds-based representation.

If you can be clearer about how you're seeing the integer to start with, we may be able to help more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top