How to print date using GregorianCalendar class in Java?

JavaServer Side ProgrammingProgramming

The GregorianCalendar class supports standard calendars it supports Julian and Gregorian calendars you can create an object of GregorianCalendar using one of its constructors. Following are various examples demonstrating how to print date using this class −

Example

Following example creates GregorianCalander by passing year, month and date values as parameters to its constructor and prints the date −

Live Demo

import java.util.Calendar;
import java.util.GregorianCalendar;
public class Test {
   public static void main(String args[]){
      //Instantiating the GregorianCalendar
      GregorianCalendar cal = new GregorianCalendar(2018, 6, 27);
      System.out.println(cal);
      System.out.println("Date: "+cal.get(Calendar.DATE));
      System.out.println("Month: "+cal.get(Calendar.MONTH));
      System.out.println("Year: "+cal.get(Calendar.YEAR));
   }
}

Output

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=2018,MONTH=6,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=?]
Date: 27
Month: 6
Year: 2018

Example

Following example creates GregorianCalander by passing the Locale object as a parameter to its constructor and prints the date −

Live Demo

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
public class Test {
   public static void main(String args[]){    
Locale locale = new Locale("en", "IN");
      //Instantiating the GregorianCalendar  
GregorianCalendar cal = new GregorianCalendar(locale);
      System.out.println("Date: "+cal.get(Calendar.DATE));
      System.out.println("Month: "+cal.get(Calendar.MONTH));
      System.out.println("Year: "+cal.get(Calendar.YEAR));
   }
}

Output

Date: 7
Month: 10
Year: 2020

Example

Following example creates GregorianCalander by passing the TimeZone object as a parameter to its constructor and prints the date −

Live Demo

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class Test {
   public static void main(String args[]){    
      TimeZone timeZone = TimeZone.getTimeZone("GMT+5:30");
      //Instantiating the GregorianCalendar  
      GregorianCalendar cal = new GregorianCalendar(timeZone);
      System.out.println("Date: "+cal.get(Calendar.DATE));
      System.out.println("Month: "+cal.get(Calendar.MONTH));
      System.out.println("Year: "+cal.get(Calendar.YEAR));
   }
}

Output

Date: 7
Month: 10
Year: 2020

Example

Following example creates GregorianCalander using the getInstance() method and prints the date −

Live Demo

import java.util.Calendar;
import java.util.GregorianCalendar;
public class Test {
   public static void main(String args[]){    
      //Instantiating the GregorianCalendar  
      GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
      System.out.println("Date: "+cal.get(Calendar.DATE));
      System.out.println("Month: "+cal.get(Calendar.MONTH));
      System.out.println("Year: "+cal.get(Calendar.YEAR));
   }
}

Output

Date: 7
Month: 10
Year: 2020
raja
Published on 06-Feb-2021 03:59:46
Advertisements