Learning Java
Custom Search

Previous Next

Calendars and Dates

The Calendar class in package java.util can be used for dealing with dates and time. The class contains various values and methods to allow you to get, set, or manipulate the date and time.

If you try to instantiate the Calendar class, such as,

    Calendar cal1 = new Calendar();
the compiler would tell you that the class was abstract and cannot be instantiated. You would have to use a subclass of it such as GregorianCalendar. For example,

    Calendar cal1 = new GregorianCalendar();

Another option would be to use the getInstance() method of the Calendar class.

    Calendar cal1 = Calendar.getInstance();

In this first example below, Date01.java, we will use the following Calendar methods. The application will set a date (and sometimes time). Then it will display parts of that date and time. The first printout picks up the current date and time. Notice when it displays the month, the value is relative to zero. So, January is month 0 and December is month 11.

    getTime()
    get(Calendar.MONTH)
    get(Calendar.DAY_OF_MONTH)
    get(Calendar.YEAR)
    get(Calendar.HOUR_OF_DAY)
    get(Calendar.MINUTE)
    set(int year, int month, int date)
    set(int year, int month, int date, int hour, int minute, int second)
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Date01 {
	
    public static void main(String args[]) {
	Calendar cal = new GregorianCalendar();

	System.out.println("\ncal.getTime() : " + cal.getTime());
	System.out.println("\nMONTH: " + cal.get(Calendar.MONTH) 
		+ "\nDAY_OF_MONTH: "   + cal.get(Calendar.DAY_OF_MONTH) 
		+ "\nYEAR: "           + cal.get(Calendar.YEAR)
		+ "\nHOUR_OF_DAY: "    + cal.get(Calendar.HOUR_OF_DAY)
		+ "\nMINUTE: "         + cal.get(Calendar.MINUTE));

	cal.set(2007, 11, 25);
	System.out.println("\ncal.getTime() : " + cal.getTime());
	System.out.println("\nMONTH: " + cal.get(Calendar.MONTH) 
		+ "\nDAY_OF_MONTH: "   + cal.get(Calendar.DAY_OF_MONTH) 
		+ "\nYEAR: "           + cal.get(Calendar.YEAR)
		+ "\nHOUR_OF_DAY: "    + cal.get(Calendar.HOUR_OF_DAY)
		+ "\nMINUTE: "         + cal.get(Calendar.MINUTE));

	cal.set(1995, 4, 13);
	System.out.println("\ncal.getTime() : " + cal.getTime());
	System.out.println("\nMONTH: " + cal.get(Calendar.MONTH) 
		+ "\nDAY_OF_MONTH: "   + cal.get(Calendar.DAY_OF_MONTH) 
		+ "\nYEAR: "           + cal.get(Calendar.YEAR)
		+ "\nHOUR_OF_DAY: "    + cal.get(Calendar.HOUR_OF_DAY)
		+ "\nMINUTE: "         + cal.get(Calendar.MINUTE));

	cal.set(1984, 8, 30, 21, 35, 51);
	System.out.println("\ncal.getTime() : " + cal.getTime());
	System.out.println("\nMONTH: " + cal.get(Calendar.MONTH) 
		+ "\nDAY_OF_MONTH: "   + cal.get(Calendar.DAY_OF_MONTH) 
		+ "\nYEAR: "           + cal.get(Calendar.YEAR)
		+ "\nHOUR_OF_DAY: "    + cal.get(Calendar.HOUR_OF_DAY)
		+ "\nMINUTE: "         + cal.get(Calendar.MINUTE));
    }
} 

In this next example below, Date02, we will show some simple addition and subtraction in dates.

import java.util.Calendar;

public class Date02 {

    public static void main(String args[]) {
	Calendar cal1 = Calendar.getInstance();
	Calendar cal2 = Calendar.getInstance();

	System.out.println("Today is : "+ cal1.getTime());
	cal1.add(Calendar.DATE, 45);
	System.out.println("\nAdd 45 days to Date1............"
		+ cal1.getTime());
	cal1.add(Calendar.DATE, -30);
	System.out.println("\nSubtract 30 days from Date1....."
		+ cal1.getTime());
	cal2.add(Calendar.MONTH, 4);
	System.out.println("\nAdd 4 months to Date2..........."
		+ cal2.getTime());
	cal2.add(Calendar.HOUR_OF_DAY, -6);
	System.out.println("\nSubtract 6 hours from Date2....."
		+ cal2.getTime());

	System.out.println("\nFrom " + cal1.getTime() + " to "
		+ cal2.getTime() + " is ");
	System.out.println((cal2.getTime().getTime() 
		- cal1.getTime().getTime())	
		/ (24 * 3600 * 1000) + " days.");
	}
}  

 

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved