Mr.Noob Mr.Noob - 18 days ago 5
Java Question

How to set time to epoch time java?

I'm trying to set the time to epoch date time in java. how can I do this? so that I could get year months days etc out of the epoch date time.

Answer

use new Date(0L);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(new Date(0L)));

Take care of your timezone cause it will change depends on what you have by default.

UPDATE In java 8 you can use the new java.time library

You have this constant Instant.EPOCH

Comments