Main Tutorials

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 Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
11 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Ravi Ahuja
4 years ago

I want to convert more precise timestamps, like 2019-02-11T00:07:31.951199999999998-05:00. Can you suggest on this.

mkyong
9 years ago

Thanks for your inputs.

Teo
10 years ago

@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
11 years ago

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

mkyong
9 years ago
Reply to  software

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 – https://mkyong.com/java/java-convert-date-and-time-between-timezone/

mygamegallery
11 years ago

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

mkyong
9 years ago
Reply to  mygamegallery
Flix
15 years ago

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
10 years ago
Reply to  Flix

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
15 years ago

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