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
Tags :
Founder of Mkyong.com and HostingCompass.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.

Related Posts

Popular Posts