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.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books