Java.util.Calendar.getTime() Method


Description

The java.util.Calendar.getTime() method returns a Date object that represents this Calendar's time value

Declaration

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

public final Date getTime()

Parameters

NA

Return Value

The method returns a Date representing the time value.

Exception

NA

Example

The following example shows the usage of java.util.calendar.getTime() 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
      System.out.println(" Current time is : " + cal.getTime());

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

      // create a new calendar
      Calendar cal2 = Calendar.getInstance();
        
      // print the next time
      Date d = cal2.getTime();
      System.out.println(" Next time is : " + d);
   }
}

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

Current time is : Wed May 02 14:14:06 EEST 2012
Next time is : Wed May 02 14:14:08 EEST 2012
java_util_calendar.htm
Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements