Java time since the epoch

user1585643 picture user1585643 · Aug 8, 2012 · Viewed 50.8k times · Source

In Java, how can I print out the time since the epoch given in seconds and nanoseconds in the following format :

java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

My input is:

long mnSeconds;
long mnNanoseconds;

Where the total of the two is the elapsed time since the epoch 1970-01-01 00:00:00.0.

Answer

ProfessionalAmateur picture ProfessionalAmateur · Aug 8, 2012

Use this and divide by 1000

long epoch = System.currentTimeMillis();

System.out.println("Epoch : " + (epoch / 1000));