I want to get the current time and date in milliseconds. How can I get this?
I tried this:
Date date=new Date() ;
System.out.println("Today is " +date.getTime());
It will return the milliseconds from the 1 Jan 1970.
But I want the current millisecods of the today’s date, like:
23:59:00 = 86340000 milliseconds
This is not the correct approach for Java 8 or newer. This answer is retained for posterity; for any reasonably modern Java use Basil Bourque’s approach instead.
The following seems to work.
The problem is that
date.getTime()returns the number of milliseconds from 1970-01-01T00:00:00Z, butnew Date()gives the current local time. Adding theZONE_OFFSETandDST_OFFSETfrom theCalendarclass gives you the time in the default/current time zone.