[ Back | Previous | Next ]

How to use the Calander to add and subtract dates?

Package:
java.util.*
Product:
JDK
Release:
1.1.x
Related Links:
Calendar
EventObject
Hashtable
Locale
Properties
PropertyResourceBundle
TimeZone
Comment:
	GregorianCalendar cal = new GregorianCalendar();
	SimpleDateFormat  fmt = new SimpleDateFormat("yyyy-MM-dd" );

	int wkOfYear = cal.get(Calendar.WEEK_OF_YEAR);
	cal.clear();
	cal.set(Calendar.YEAR, 1999);
	cal.set(Calendar.WEEK_OF_YEAR, wkOfYear);
	Date tmpDate = cal.getTime();

	fmt.setCalendar(cal);
	System.out.println( fmt.format( cal.getTime() ) );
	System.out.println();

	cal.add(Calendar.DATE, 6);
	tmpDate = cal.getTime();
	fmt.setCalendar(cal);
	System.out.println( fmt.format( cal.getTime() ) );
	System.out.println();