This article was last updated on : November 4th, 2016 at 3:45 am

3 useful Java util Calendar Class examples (java.util.Calendar)

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 Calendar class in java projects. It is useful in frameworks like JSF, Struts2.

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

Output :


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();
SimpleDateFormat yyyymmdd = new SimpleDateFormat(datePattern);

Output :


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.sitenol.CalendarExamples;
import java.util.Calendar;
public class ComparingTwoDates {
public static void main(String[] args) {
Calendar cal1 = Calendar.getInstance();
cal1.set(2014, Calendar.FEBRUARY, 03);
Calendar cal2 = Calendar.getInstance();
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


  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