Thursday, 13th October 2011
Follow WikiJava on twitter now. @Wikijava

Using the Calendar class tutorial

From WikiJava

Jump to: navigation, search
The author suggests:

buy this book

The most recent versions of jdk deprecated a massive part of the methods from the famous class java.util.Date.

This example shows how to use the new classes to replace the missing methods from Date.

Contents

the article

Image:250px-Subversion.png
You can download the complete code of this article from the Subversion repository at this link

Using the username:readonly and password: readonly

See the using the SVN repository instructions page for more help about this.

Calendar Objects

Essentially the new class in charge of replacing the functionalities of Date is the abstract class java.util.Calendar.

Calendar provides the tools for describing time in general, without being bound to specific ways of representing it.

The api extends Calendar with the java.util.GregorianCalendar that implements the time in the most widely used calendar in the world today.

The Calendar and GregorianCalendar classes are to be used quite differently from the old Date class.

If you want to represent the current date and time you can construct the GregorianCalendar using the default constructor, this will initialize the object with the current time of the computer.

if you want to represent specific dates or times you can construct the GregorianCalendar using one of the other constructors.

Something that may be deceiving in the way to instanciate Calendar objects is that you may want to instanciate the class as follows:

	Calendar calendar = new GregorianCalendar();
	calendar.set(year, month, day, hourOfDay, minute, second);

and then use calendar.getTimeInMillis() to obtain the time in milliseconds since 1970 for the date you set.

This won't work, because the getTimeInMillis will return the current milliseconds from the system clock (at the moment of the getTimeInMillis request), not the milliseconds at the time set in Calendar.

The workaround calendar.getTime().getTime() won't work either.

Really the best way to do this is by using the proper constructor of Calendar and setting in there the year, month, date ... that you want, for example:

	Calendar calendar = new GregorianCalendar(year, month, day, hourOfDay, minute, second);


follows the complete code for the TimeMilliseconds which gets from the command line the 6 parameters for a Date and time, and gives the same date in milliseconds, without using any deprecated methods from the class Date.


representing the data

Very often while doing things quickly the programmers used the method toString(), from the Date class, to obtain the date in a String in a reasonably legible format.

This is not possible anymore by using the Calendar class, its toString() method in fact returns something that is far away from being legible.

We are then forced to use the SimpleDateFormat class to explicitly specify the format for the output.

This will give us a bit more flexibility, at the cost of a greater complexity.

Here's how to use the SimpleDateFormat.

	SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
		"d MMM yyyy hh:mm aaa");
	String stringCalendar = simpleDateFormat.format(calendar.getTime());

I'm sure the documentation of SimpleDateFormat will be checked very often by everyone for knowing how to fill up the String parameter of SimpleDateFormat(String)

putting all together

package org.wikijava.time;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
 
/**
 * @author giulio
 * 
 */
public class TimeMilliseconds {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
 
	if (args.length < 6) {
	    System.err.println("usage: java -jar TimeMilliseconds "
		    + "[year] [month] [day] [hour] [minute] [second]");
	    return;
	}
 
	int year = Integer.parseInt(args[0]);
	int month = Integer.parseInt(args[1]);
	int day = Integer.parseInt(args[2]);
	int hourOfDay = Integer.parseInt(args[3]);
	int minute = Integer.parseInt(args[4]);
	int second = Integer.parseInt(args[5]);
 
	Calendar calendar = new GregorianCalendar(year, month, day, hourOfDay,
		minute, second);
 
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
		"d MMM yyyy hh:mm aaa");
	String stringCalendar = simpleDateFormat.format(calendar.getTime());
	System.out.println(stringCalendar);
 
	System.out.println("in milliseconds: " + calendar.getTimeInMillis());
    }
 
}

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Comments on wikijava are disabled now, cause excessive spam.