Sotirios-Efstathios Maneas

About Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a postgraduate student at the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. His main interests include distributed systems, web crawling, model checking, operating systems, programming languages and web applications.

Java Calendar example

In this tutorial we will discuss about Java’s java.util.Calendar and how to use it. The Calendar is an abstract class that provides methods for converting between time and calendar fields. Also, the class provides fields and methods for implementing a concrete calendar system.

The Calendar field values can be set by calling the following methods: set, add and roll. In addition, the Calendar has two modes for interpreting its fields, lenient and non-lenient. The lenient mode accepts a wider range of field values than it produces. The non-lenient mode is more strict and throws an exception in incorrect cases.

 

Create an Instance

The Calendar provides a class method, called getInstance(), which returns a general object of this type, whose calendar fields have been initialized with the current date and time. For example:

Calendar calendar = Calendar.getInstance();

The Calendar provides getter and setter methods for its fields:

  • public final int get (int field)
  • public final void set (int field, int value)

A subset of the Calendar fields is shown below:

  • Calendar.YEAR
  • Calendar.MONTH
  • Calendar.DAY_OF_MONTH
  • Calendar.HOUR
  • Calendar.MINUTE
  • Calendar.SECOND

A sample example that uses an instance of the Calendar class is shown below:

CalendarExample.java:

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

public class CalendarExample {

	 public static void main(String[] args) {
		// Get an instance of a Calendar, using the current time.
		 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 Calendar calendar = Calendar.getInstance();
		 System.out.println(dateFormat.format(calendar.getTime()));
		
		// Printing some information...
		System.out.println("ERA: " + calendar.get(Calendar.ERA));
		System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
		System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
		System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
		System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
		System.out.println("DATE: " + calendar.get(Calendar.DATE));
		System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
		System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
		System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
		System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
		System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
		System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
		System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
		System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
		System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
		System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
	 }
}

A sample execution is shown below:

2014-02-06 17:33:40
ERA: 1
YEAR: 2014
MONTH: 1
WEEK_OF_YEAR: 6
WEEK_OF_MONTH: 2
DATE: 6
DAY_OF_MONTH: 6
DAY_OF_YEAR: 37
DAY_OF_WEEK: 5
DAY_OF_WEEK_IN_MONTH: 1
AM_PM: 1
HOUR: 5
HOUR_OF_DAY: 17
MINUTE: 33
SECOND: 40
MILLISECOND: 692

Notice that the output will be different in each execution, because the Calendar is initialized using the current time.

Calendar and Date

The Calendar class can be used with the java.util.Date class, in order to create dates of a specific format. A sample example is given below:

CalendarDateExample.java:

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

public class CalendarDateExample {

	public static void main(String[] args) {
		// Create an instance of a GregorianCalendar
		Calendar calendar = new GregorianCalendar(2014, 1, 06);

		System.out.println("Year: " + calendar.get(Calendar.YEAR));
		System.out.println("Month: " + (calendar.get(Calendar.MONTH) + 1));
		System.out.println("Day: " + calendar.get(Calendar.DAY_OF_MONTH));

		// Format the output.
		SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println(date_format.format(calendar.getTime()));
	}
}

A sample execution is shown below:

Year: 2014
Month: 2
Day: 6
2014-02-06

GregorianCalendar

The only implementation of a Calendar that is provided by Java is the GregorianCalendar class. This class provides a standard calendar system that supports both the Julian and Gregorian calendar systems.

A sample example that uses a GregorianCalendar is shown below:

GregorianCalendarExample.java:

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

public class GregorianCalendarExample {

	public static void main(String[] args) {
		// Create an instance of a GregorianCalendar, using the current time.
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = new GregorianCalendar();
		System.out.println(dateFormat.format(calendar.getTime()));

		// Printing some information...
		System.out.println("ERA: " + calendar.get(Calendar.ERA));
		System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
		System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
		System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
		System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
		System.out.println("DATE: " + calendar.get(Calendar.DATE));
		System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
		System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
		System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
		System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
		System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
		System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
		System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
		System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
		System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
		System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
	}
}

A sample execution is shown below:

2014-02-06 17:24:50
ERA: 1
YEAR: 2014
MONTH: 1
WEEK_OF_YEAR: 6
WEEK_OF_MONTH: 2
DATE: 6
DAY_OF_MONTH: 6
DAY_OF_YEAR: 37
DAY_OF_WEEK: 5
DAY_OF_WEEK_IN_MONTH: 1
AM_PM: 1
HOUR: 5
HOUR_OF_DAY: 17
MINUTE: 24
SECOND: 50
MILLISECOND: 934

Notice that the output will be different in each execution, because the GregorianCalendar is initialized using the current time.

 
This was an example about calendars in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close