Date
Convert Date to millisec
In this example we shall show you how to convert a Date to millisec, that means get the long number of millisec that represent a Date object. A Date object represents a specific instant in time, with millisecond precision. To convert a Date to millisec one should perform the following steps:
- Create a new Date object, using the
Date()
constructor, that allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond. - Use
getTime()
API method of Date. The method returns the number of milliseconds sinceJanuary 1, 1970, 00:00:00 GMT
represented by this Date object,
as described in the code snippet below.
package com.javacodegeeks.snippets.core; import java.util.Date; public class DateToMilliSec{ public static void main(String args[]){ Date date = new Date(); // Print date System.out.println("Date = " + date); //getTime() returns the time that passed since 1970 00:00:00 GMT, in milliseconds System.out.println("Milliseconds = " + date.getTime()); } }
Output:
Date = Sun Jul 22 18:58:19 EEST 2012
Milliseconds = 1342972699977
This was an example of how to convert a Date to millisec in Java.