How to get time in milliseconds in Java
In Java, there’s two methods to get time in milliseconds
1) Date class – getTime() method
2) Calendar class – getTimeInMillis() method
import java.util.Calendar; import java.util.Date; public class TimeMilisecond { public static void main(String[] argv) { long lDateTime = new Date().getTime(); System.out.println("Date() - Time in milliseconds: " + lDateTime); Calendar lCDateTime = Calendar.getInstance(); System.out.println("Calender - Time in milliseconds :" + lCDateTime.getTimeInMillis()); } }

Hi Mkyong,
i love your website, i was struck with below requirement please help me.
my requirement : i am getting date in string format here date is : “01/16/2013 6:00 PM”. i need to trigger one job at this time. here user is in US(timezone : America/Regina[GMT -06:00]) and server is in INDIa(Asia/Calcutta[GMT +5:30]).
user assuming that the job is trigger at 01/16/2013 6:00 PM of his local time, for that i have to convert the us time in to IST and stored in the server to trigger the job in server.
here user selected time is : “01/16/2013 6:00 PM”
difference bt timezones : 11:30 hr ( India is 11:00 hr ahead)
so user time in server(IST) is : “01/17/2013 5:30 AM”
how to achive this functionality with a simple method which takes date as string and two timezones one is server and another one is user timezone.
please help me.
Thanks in advance..: )
how to get the actual time like Aug 10 2012 10:32:45
There is a third option and you avoid the cost of create a new object. Using the static method of class System:
– System.currentTimeMillis();
Even if u want nanoseconds precision u can use:
– System.nanoTime();
hi your idea is great !!! thanks for sharing it
When using System.nanoTime(), one should be aware that this does not return any wallclock time, i.e. it is the amount of nanoseconds since an arbitrary point in time, like the start of the JVM. This means that this method should *only* be used to measure elasped time, but not be used as a timestamp that has any meaning.
There is a third option and you avoid the cost of create a new object. Using the static method of class System: