3:11 pm - Thursday November 25, 8855

Calendar in java example program add(int field, int amount)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044
/**
 *@author:candidjava
 *@description:Calendar in java example program add(int field, int amount)
 **/

import java.util.Calendar;

public class CalendarAdd {
	public static void main(String[] args) {
		// create a calendar
		Calendar cal = Calendar.getInstance();
		// print current date
		System.out.println("The current date is : " + cal.getTime());
		// add 20 days to the calendar
		cal.add(Calendar.DATE, 20);
		System.out.println("20 days later: " + cal.getTime());
		// subtract 2 months from the calendar
		cal.add(Calendar.MONTH, -2);
		System.out.println("2 months ago: " + cal.getTime());
		// subtract 5 year from the calendar
		cal.add(Calendar.YEAR, -5);
		System.out.println("5 years ago: " + cal.getTime());
		// add 5yrs to the calendar
		cal.add(Calendar.YEAR, 5);
		System.out.println("5 years later: " + cal.getTime());
	}
}

Output:

The current date is : Thu Oct 11 13:58:06 IST 2012
20 days later: Wed Oct 31 13:58:06 IST 2012
2 months ago: Fri Aug 31 13:58:06 IST 2012
5 years ago: Fri Aug 31 13:58:06 IST 2007
5 years later: Fri Aug 31 13:58:06 IST 2012

BACK

Filed in: Z-A All Java Codes

Comments are closed.