Java.util.Calendar.getTimeInMillis() Method

Advertisements


Description

The java.util.Calendar.getTimeInMillis() method returns this Calendar's time in milliseconds.

Declaration

Following is the declaration for java.util.Calendar.getTimeInMillis() method

public long getTimeInMillis()

Parameters

  • NA

Return Value

The method returns the current time as UTC milliseconds.

Exception

  • NA

Example

The following example shows the usage of java.util.calendar.getTimeInMillis() method.

package com.tutorialspoint;

import java.util.*;

public class CalendarDemo {

   public static void main(String[] args) throws InterruptedException {

      // create a calendar
      Calendar cal = Calendar.getInstance();

      // print current time in milliseconds
      System.out.println("Current time is : " + cal.getTimeInMillis());

      // add a delay of 2 seconds
      Thread.sleep(2000);

      // create a new calendar
      Calendar cal2 = Calendar.getInstance();

      // print the next time in milliseconds
      long d = cal2.getTimeInMillis();
      System.out.println("Next time is : " + d);

   }
}

Let us compile and run the above program, this will produce the following result:

Current time is : 1336226501635
Next time is : 1336226503635



Advertisements
Advertisements