Java Calendar toString() Method



Description

The Java Calendar toString() method returns a string representation of the calendar.

Declaration

Following is the declaration for java.util.Calendar.toString() method

public String toString()

Parameters

NA

Return Value

This method does not return a value.

Exception

NA

Getting String Representation of Current Dated Calendar Instance Example

The following example shows the usage of Java Calendar toString() method. We're creating an instance of a Calendar of current date using getInstance() method and printing the calendar object using toString() method.

package com.tutorialspoint;

import java.util.Calendar;

public class CalendarDemo {
   public static void main(String[] args) {

      // create a calendar
      Calendar cal = Calendar.getInstance();

      // print calendar object
      System.out.println("Calendar: " + cal.toString());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Calendar: java.util.GregorianCalendar[time=1664370513374,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=8,WEEK_OF_YEAR=40,WEEK_OF_MONTH=5,DAY_OF_MONTH=28,DAY_OF_YEAR=271,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=6,HOUR_OF_DAY=18,MINUTE=38,SECOND=33,MILLISECOND=374,ZONE_OFFSET=19800000,DST_OFFSET=0]

Getting String Representation of Current Dated GregorianCalendar Instance Example

The following example shows the usage of Java Calendar setTime() method. We're creating an instance of a Calendar of current date using GregorianCalendar() method and printing the calendar object using toString() method.

package com.tutorialspoint;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarDemo {
   public static void main(String[] args) {

      // create a calendar
	   Calendar cal = new GregorianCalendar();

      // print current time
      System.out.println("Calendar: " + cal.toString());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Calendar: java.util.GregorianCalendar[time=1664370449332,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=8,WEEK_OF_YEAR=40,WEEK_OF_MONTH=5,DAY_OF_MONTH=28,DAY_OF_YEAR=271,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=6,HOUR_OF_DAY=18,MINUTE=37,SECOND=29,MILLISECOND=332,ZONE_OFFSET=19800000,DST_OFFSET=0]

Getting String Representation of Given Dated GregorianCalendar Instance Example

The following example shows the usage of Java Calendar toString() method. We're creating an instance of a Calendar of current date using getInstance() method and printing the calendar object using toString() method.

package com.tutorialspoint;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarDemo {
   public static void main(String[] args) {

      // create a calendar
      Calendar cal = new GregorianCalendar(2022,8,27);

      // print current time
      System.out.println("Calendar: " + cal.toString());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Calendar: java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=?,YEAR=2022,MONTH=8,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=27,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.htm
Advertisements