java.util.Calendar getTimeInMillis()

Description :

This java tutorial shows how to use the getTimeInMillis()method of Calendar class of java.util package. This method returns this Calendar’s time value in milliseconds.

Method Syntax :

public long getTimeInMillis()

Parameter Input :

 

DataType Parameter Description
N/A N/A N/A

 

Method Returns :

This method returns the current time as UTC milliseconds from the epoch.

Compatibility Version :

Requires Java 1.1 and up

Exception :

N/A

Discussion :

The Calender getTimeInMillis() method is used to get the current time on our system in milliseconds. Personally i prefer to store in database or file a date in milliseconds rather than the full date. It is easier and does not require complicated handling of dates.

Java Code Example :

This java example source code demonstrates the use of getTimeInMillis() method of Calendar class.

package com.javatutorialhq.java.examples;

import java.util.Calendar;

/* * This example source code demonstrates the use of * getTimeInMillis() method of Calendar class */

public class CalendarGetTimeInMillisExample {

public static void main(String

[] args) throws InterruptedException {

Calendar cal = Calendar.getInstance(); // Printing the current time in milliseconds System.out.println("Time 1 in millis :"+cal.getTimeInMillis());

// sleeping for one sec Thread.sleep(2000);

// reinstantiate the calendar object cal = Calendar.getInstance();

// Printing the new time in milliseconds System.out.println("Time 2 in millis: "+cal.getTimeInMillis());

}

}

Sample Output :

Running the getTimeInMillis() method example source code of Calendar class will give you the following output:

 

Calendar getTimeInMillis() method example

Calendar getTimeInMillis() method example

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :