Calendar toString() Method in Java with Examples

The toString() method in Calendar class is used to get the string representation of the Calendar object. This method in Calendar Class is just for debug process and not to be used as an operation.

Syntax:

public String toString()

Parameters: The method does not take any parameters.

Return Value: The method returns the String representation of the Calendar object. It can return an empty object but not null.

Below programs illustrate the working of toString() Method of Calendar class:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code to illustrate toString() Method
  
import java.util.*;
  
public class CalendarClassDemo {
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar calndr1
            = Calendar.getInstance();
  
        // Returning the string representation
        System.out.println("The string form: "
                           + calndr1.getTime()
                                 .toString());
    }
}

chevron_right


Output:

The string form: Wed Feb 13 09:46:36 UTC 2019

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code to illustrate toString() Method
  
import java.util.*;
  
public class CalendarClassDemo {
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar calndr1
            = new GregorianCalendar(2018, 12, 2);
  
        // Returning the string representation
        System.out.println("The string form: "
                           + calndr1.getTime()
                                 .toString());
    }
}

chevron_right


Output:

The string form: Wed Jan 02 00:00:00 UTC 2019

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.