java convert long timestamp to date code example

Example 1: convert long to date in java

//The Date constructor (click the link!) accepts the time as long in milliseconds, not seconds.
long currentDateTime = System.currentTimeMillis();
//creating Date from millisecond
Date currentDate = new Date(currentDateTime);

Example 2: date to timestamp java

All you need to do is change the string within the java.text.SimpleDateFormat constructor to: "MM-dd-yyyy HH:mm:ss".

Just use the appropriate letters to build the above string to match your input date.

Tags:

Java Example