How to get time in milliseconds in Java

In Java, you can use following methods to get time in milliseconds

  1. Date class – getTime() method
  2. Calendar class – getTimeInMillis() method
package com.mkyong.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeMilisecond {
  public static void main(String[] argv) throws ParseException {
	SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
	String dateInString = "22-01-2015 10:20:56";
	Date date = sdf.parse(dateInString);
	System.out.println("Date - Time in milliseconds : " + date.getTime());
	Calendar calendar = Calendar.getInstance();
	System.out.println("Calender - Time in milliseconds : " + calendar.getTimeInMillis());


22-01-2015 10:20:56
Date - Time in milliseconds : 1421893256000
Calender - Time in milliseconds : 1421893256000
Tags :

About the Author

Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.


  • Teo

    @software For cases such as the one described by you, why not store every timestamp in UTC? Then you can convert milliseconds from UTC to your local timezone or back, depending on the direction which you need. Of course the linked website does it via Javascript but you can do it programatically in most languages.

  • software

    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..: )

  • mygamegallery

    how to get the actual time like Aug 10 2012 10:32:45

  • Flix

    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();

    • mkyong

      hi your idea is great !!! thanks for sharing it

    • Daniel Kullmann

      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.

  • Flix

    There is a third option and you avoid the cost of create a new object. Using the static method of class System: