Calendar Class (java.util.Calendar) Examples – Java 5.0 tutorial

In this post we will demonstrate java.util.Calendar Class features and its uses with examples. After working with examples in this post you will be comfortable to use Calender class in java projects. It is useful in frameworks like JSF, Srtuts2.

Java Calendar Class Examples

Let us start with some valuable examples to see how can we use Calendar Class in Java Programming.

1. Instantiating Calendar Class Example

java.util.Calendar is an abstract class. It can not be initialize by saying new Calendar().  We use getInstance() method to initialize it. By default getInstance() method will consider you current system date to create instance object of Calendar. Please find the example code below.

package com.sitenol.CalendarExamples;

import java.util.Calendar;

public class CreateCalendarObjectExample {

	public static void main(String[] args) {
		Calendar obj = Calendar.getInstance();
		System.out.println(obj);
	}
}

Output :

java.util.GregorianCalendar[time=1391259425733,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=1,WEEK_OF_YEAR=5,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=32,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=6,HOUR_OF_DAY=18,MINUTE=27,SECOND=5,MILLISECOND=733,ZONE_OFFSET=19800000,DST_OFFSET=0]

Note : Calendar gregorianCal = new GregorianCalendar() is one more and the most using way to initialize the Calendar object.  java.util.GregorianCalendar class extends java.util.Calendar abstract class. System.out.println(gregorianCal) will give the same output as given above.

2. Formatting Calendar Object using SimpleDateFormat Class

In this example we will demonstrate how can we formate date in different ways like 20-Feb-2014 using Calendar object. As a requirement our client can ask us to show date like 20-Feb-2014 (dd-MMM-yyyy) on user interface. Some clients can also ask to formate the same date object like 2014/Feb/20 (yyyy/MMM/dd) or 2014-02-20 (yyyy-mm-dd). Firstly we will create a Calendar object and then we will use java.text.SimpleDateFormat class to format date. Please find the code for this example below.

package com.sitenol.CalendarExamples;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class FormatingCalendarObjects {
	public static void main(String[] args) {
		String datePattern = "yyyy-MMM-dd";
		Calendar calObj = new GregorianCalendar();
		calObj.clear();
		SimpleDateFormat yyyymmdd = new SimpleDateFormat(datePattern);
		System.out.println(yyyymmdd.format(calObj.getTime()));
	}
}

Output :

1970-Jan-01

In the above code we have String datePattern = “yyyy-MMM-dd”; which is declaring the formate in which date needs to be shown. In the table below we are showing different date formates can be used and the output from it.

Some More Date Formates
Date Pattern Output
yyyy-MM-dd 1970-01-01
M/d/yyyy 1/1/1970
MM/dd/yy 01/01/70
MM/dd/yyyy 01/01/1970
MM-dd-yy 01-01-70

You can choose more formates but according to SimpleDateFormat definition. Please see the official documentation for advance features.

3. Comparing two Calendar Objects

In this Calendar example we have written code to show comparison of two java.util.Calender objects. For this purpose you need to create two Objects of Calendar. We have two of methods use .before(Calendar cal) and .after(Calendar cal) methods for comparison.

Please find the example code below.

package com.CalendarExamples;

import java.util.Calendar;

public class ComparingTwoDates {
	public static void main(String[] args) {
		Calendar cal1 = Calendar.getInstance();
		cal1.clear();
		cal1.set(2014, Calendar.FEBRUARY, 03);
		Calendar cal2 = Calendar.getInstance();
		cal2.clear();
		cal2.set(2011, Calendar.JANUARY, 05);
		/**
		 * checking that is cal1 is before date to cal2
		 */
		System.out.println("cal1 is before cal2 : "+ cal1.before(cal2));
		/**
		 * checking that is cal1 is after date to cal2
		 */
		System.out.println("cal1 is after cal2 : "+cal1.after(cal2));
	}
}

Output :

cal1 is before cal2 : false
cal1 is after cal2 : true

We will update some more examples in this post.

Share this post:

Recent Posts

6 Comments

  1. Avatar for Tarun Singh
    Villamayor says:

    Have you ever thought about creating an ebook or guest authoring on other websites? I have a blog centered on the same information you discuss and would love to have you share some stories/information. I know my visitors would appreciate your work. If you are even remotely interested, feel free to shoot me an e-mail.

    • Avatar for Tarun Singh
      Bhavesh Patel says:

      Calendar class is very important in developing web application in Java. I have got a lot of information in this post.
      Please tell how can we save a Calendar class in MySql Database and again retrieve it again and show in UI?

      • Avatar for Tarun Singh
        Tarun Singh says:

        Hi Bhavesh! Thanks for stopping here. I have plan to write a blogpost on Database related programming for Calendar class. I will post once I have done. Thanks.

    • Avatar for Tarun Singh
      Tarun Singh says:

      Hello Villamayor! Sorry for late reply. I will email you if I have any plans. Right now I am writing ebook for my blog (sitenol) subscribers. Thanks.

  2. Avatar for Tarun Singh
    Yusad0lat says:

    i need your help with java 2 enterprise edition j2ee

Leave a Comment