Home >Java >javaTutorial >How Do I Convert Unix Timestamps to Java Date Objects?
Converting Unix Timestamps to Date Objects in Java
In Java, converting Unix timestamps to Date objects requires consideration of the timestamp's format. Unix timestamps typically represent seconds, while Java expects milliseconds since the epoch (January 1, 1970, 00:00:00 GMT).
When converting a Unix timestamp, multiply it by 1000 to convert it to milliseconds, as seen in the code below:
java.util.Date time = new java.util.Date((long) timeStamp * 1000);
If the timestamp is already in milliseconds, use the following code to create the Date object:
java.util.Date time = new java.util.Date((long) timeStamp);
According to the Java documentation, the Date constructor initializes the object with the specified number of milliseconds since the epoch.
The above is the detailed content of How Do I Convert Unix Timestamps to Java Date Objects?. For more information, please follow other related articles on the PHP Chinese website!