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
TimeMilisecond.java

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(dateInString);
	System.out.println("Date - Time in milliseconds : " + date.getTime());

	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);
	System.out.println("Calender - Time in milliseconds : " + calendar.getTimeInMillis());

  }
}

Output


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

About the Author

author image
mkyong
Founder of Mkyong.com, 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.

Comments

Leave a Reply

avatar
newest oldest most voted
mkyong
Guest
mkyong

Thanks for your inputs.

Teo
Guest
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
Guest
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… Read more »
mkyong
Guest
mkyong

You can use Joda Time to convert date and time to different time zones, for example :

DateTime dt = new DateTime(date);
DateTimeZone dtZone = DateTimeZone.forID("America/New_York");
Date dateInUS = dt.withZone(dtZone).toLocalDateTime().toDate();

More examples here – http://www.mkyong.com/java/java-convert-date-and-time-between-timezone/

mygamegallery
Guest
mygamegallery

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

mkyong
Guest
mkyong
Flix
Guest
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();

Daniel Kullmann
Guest
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
Guest
Flix

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