Skip to content
Related Articles

Related Articles

Calendar toString() Method in Java with Examples

View Discussion
Improve Article
Save Article
  • Last Updated : 13 Feb, 2019
View Discussion
Improve Article
Save Article

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:




// 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());
    }
}

Output:

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

Example 2:




// 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());
    }
}

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
Recommended Articles
Page :

Start Your Coding Journey Now!