Java Utility Library

Java Calendar - toString() Method



The java.util.Calendar.toString() method returns a string representation of this calendar. This method is intended to be used only for debugging purposes, and the format of the returned string may vary between implementations. The returned string may be empty but may not be null.

Syntax

public String toString()

Parameters

No parameter is required.

Return Value

Returns a string representation of this calendar.

Exception

NA

Example:

In the example below, the java.util.Calendar.toString() method returns a string representation of the given calendar.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Calendar object with specified date
    Calendar Cal = new GregorianCalendar(2015, 10, 25);

    //printing the calendar
    System.out.println("The Calendar is: " + Cal.getTime());

    //getting the string representation of the calendar
    String Str = Cal.toString();    

    //Printing the String
    System.out.println("The String contains: " + Str);    
  }
}

The output of the above code will be:

The Calendar is: Wed Nov 25 00:00:00 UTC 2015
The String contains: java.util.GregorianCalendar[time=1448409600000,areFieldsSet=true,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Etc/UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=?,YEAR=2015,MONTH=10,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=25,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?]

❮ Java.util - Calendar