Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subtracting months using Calendar

Status
Not open for further replies.

timmbo

Programmer
Feb 22, 2001
167
0
0
US
Hi All,

I am completely confused here. I need to get the current month and subtract 2 months. I need it formatted to show month spelled out (i.e. June). Any help would be appreciated.


Calendar cal = Calendar.getInstance();
cal.setTime(new java.util.Date());
System.out.println("cal... " + cal);


Thanks,
Tim
 
This should subtract 2 months of the current date:
Code:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-2);
int month = calendar.get(Calendar.MONTH));

You cannot get the month name out in the way you want so, just the month number - you could use an array of month names as a lookup.

Richard
 
Thanks for your help Richard.
 
>> you could use an array of month names as a lookup.

Yes, or if your application requires multiple language support those strings would go into resource bundles.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top