Skip to content
Related Articles

Related Articles

Improve Article

Calendar toString() Method in Java with Examples

  • Last Updated : 13 Feb, 2019

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()

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :