Getting time in Milliseconds

In the previous examples of Date class we have created
simple Date and now in this section we are going to get the current
date and time in the milliseconds. To do this we have used getTime()
method of Date class.
getTime() returns the total time in milliseconds
since 1 January 1970 00:00:00 GMT. getTime()
returns total time in long datatype.
Here is the full example code of GetTimeMilliseconds.java
as follows:
GetTimeMilliseconds.java
import java.util.Date;
public class GetTimeMilliseconds{
public static void main(String[] args) {
// Create a new instance of Date
Date newDate = new Date();
System.out.println("New Date:="+newDate);
// getTime() method returns number of milliseconds since Jan 1970
System.out.println("Number of milliseconds ==>"+newDate.getTime());
}
}
|
Compile this example with the javac command and
execute it.
Output:
C:\DateExample>javac GetTimeMilliseconds.java
C:\DateExample>java GetTimeMilliseconds
New Date:=Fri Oct 10 13:26:49 GMT+05:30 2008
Number of milliseconds ==>1223625409171 |
Download Source
Code

|