by Lars Vogel

Follow me on twitter

Lars Vogel on Google+

Java Date, Calendar and Time API - Tutorial

Lars Vogel

Version 0.3

26.03.2010

Revision History
Revision 0.1 01.05.2009 Lars
Vogel
First Version
Revision 0.2 05.02.2010 Lars
Vogel
Typo fixed
Revision 0.3 26.03.2010 Lars
Vogel
how to format the output format of date

Java Date and Time API

This article explains the API for using Calendar, Date and Time in Java and how to format the output of a date.


Table of Contents

1. Overview
2. Format date
3. Working with Dates and Calendars
3.1. Calendar
3.2. Date and Date Conversion
4. Thank you
5. Questions and Discussion
6. Links and Literature

1. Overview

The Java language provides direct support for time based objects. This article gives a few examples how this API can be used. .

The class java.util.Date was the first implementation which was used for manipulating dates. The class java.util.Calendar was added in Java 1.1 and provides simplified access to storing and manipulating dates.

It is recommended to use Calendar if possible. In reality you have to convert frequently from Date to Calendar and vice versa, e.g. during database access you often get a java.sql.Date object. The following explains how to use Calendar and how to convert Dates into Calendar.

2. Format date

To format a date you can use the class SimpleDateFormat. For example to get today's date in the 'dd/MM/yy' format you can use:

			
DateFormat df = new SimpleDateFormat("dd/MM/yy");
String formattedDate = df.format(new Date());
		

To format the date in in the format 'yyyy/MM/dd' you can use.

			
DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
String formattedDate = df.format(theDate);

		

3. Working with Dates and Calendars

3.1. Calendar

The class java.util.Calendar is an abstract encapsulation of the object Date. The concrete implementation is java.util.GregorianCalendar.

Calendar provides getter and setter for the date fields.

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

Table 1. Calendar field access

Field Explanation
Calendar.YEAR Identifies the year
Calendar.MONTH Identifies the month
Calendar.DAY_OF_MONTH Identifies the day
Calendar.HOUR Identifies the hour
Calendar.MINUTE Identifies the minute
Calendar.SECOND Identifies the second


Tip

The Calendar.MONTH starts with 0. So December is 11.

Create a new Java Project called "JavaIntroCalendar". Create the following class for testing.

				
package test;

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

public class CalendarTest {
	public static void main(String[] args) {
		// Constructor allows to set year, month and date
		Calendar cal1 = new GregorianCalendar(2008, 01, 01);
		// Constructor could also be empty
		// Calendar cal2 = new GregorianCalendar();
		// Change the month
		cal1.set(Calendar.MONTH, Calendar.MAY);

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

		// Format the output with leading zeros for days and month
		SimpleDateFormat date_format = new SimpleDateFormat("yyyyMMdd");
		System.out.println(date_format.format(cal1.getTime()));

	}
}

			

3.2. Date and Date Conversion

Use the following commands to convert to a Date from various formats.

				
package conversion;

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

public class ConversionExamplesDate {

	// Convert from String to date
	private void stringToDate() {
		
		try {
			Date date1;
			date1 = new SimpleDateFormat("MM/dd/yy").parse("05/18/05");
			System.out.println(date1);
			Date date2 = new SimpleDateFormat("MM/dd/yyyy").parse("05/18/2007");
			System.out.println(date2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	// Convert from millisecs to a String with a defined format
	private void calcDate(long millisecs) {
		SimpleDateFormat date_format = new SimpleDateFormat("MMM dd,yyyy HH:mm");
		Date resultdate = new Date(millisecs);
		System.out.println(date_format.format(resultdate));
	}
	
	private void writeActualDate(){
		Calendar cal = new GregorianCalendar();
		Date creationDate = cal.getTime();
		SimpleDateFormat date_format = new SimpleDateFormat("MMM dd,yyyy HH:mm");
		System.out.println(date_format.format(creationDate));
	}
	

	public static void main(String[] args) {
		ConversionExamplesDate convert = new ConversionExamplesDate();
		convert.stringToDate();
		convert.calcDate(System.currentTimeMillis());
		convert.writeActualDate();
	}
}

			

4. Thank you

Please help me to support this article:

Flattr this

5. Questions and Discussion

Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.de Google Group. I have created a short list how to create good questions which might also help you.

6. Links and Literature

Not yet listed