Tuesday, February 17, 2026
HomeLanguagesJavaCalendar computeTime() method in Java with Examples

Calendar computeTime() method in Java with Examples

The computeTime() method in Calendar class is used for the conversion operation of the current calendar field values in fields[] to the millisecond time value time.

Syntax:

protected abstract void computeTime()

Parameters: The method does not take any parameters.

Return Value: The method does not return any value.

Below programs illustrate the working of computeTime() Method of Calendar class:
Example 1:




// Java Code to illustrate
// computeTime() Method
  
import java.util.*;
  
public class CalendarClassDemo
    extends GregorianCalendar {
    public static void main(String args[])
    {
        // Creating calendar object
        CalendarClassDemo calndr = new CalendarClassDemo();
  
        // Displaying the current date
        System.out.println("The Current "
                           + "date is: " + calndr.getTime());
  
        // Clearing the calendar
        calndr.clear();
  
        // Set a new year and call
        // computeTime()
        calndr.set(GregorianCalendar.YEAR, 2016);
        calndr.computeTime();
  
        // Displaying the current date
        System.out.println("The recent"
                           + " date is: " + calndr.getTime());
    }
}


Output:

The Current date is: Wed Feb 13 16:27:12 UTC 2019
The recent date is: Fri Jan 01 00:00:00 UTC 2016

Example 2:




// Java Code to illustrate
// computeTime() Method
  
import java.util.*;
  
public class CalendarClassDemo
    extends GregorianCalendar {
    public static void main(String args[])
    {
        // Creating calendar object
        CalendarClassDemo calndr = new CalendarClassDemo();
  
        // Displaying the current date
        System.out.println("The Current "
                           + "date is: " + calndr.getTime());
  
        // Clearing the calendar
        calndr.clear();
  
        // Set a new year and call
        // computeTime()
        calndr.set(GregorianCalendar.YEAR, 2000);
        calndr.computeTime();
  
        // Displaying the current date
        System.out.println("The recent"
                           + " date is: " + calndr.getTime());
    }
}


Output:

The Current date is: Wed Feb 13 16:27:15 UTC 2019
The recent date is: Sat Jan 01 00:00:00 UTC 2000

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#computeTime()

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS