It's not a secret anymore!*
The moose likes Java in General and the fly likes Date for last month Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date for last month" Watch "Date for last month" New topic
Author

Date for last month

sam davis
Greenhorn

Joined: Jun 16, 2003
Posts: 18
Hello,
Could someone help me out with the code to get the date for one month ago(last month)...and how would i account for the date rollover(when the year changes) i would really appreciate it..thanks..
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Use java.util.Calendar to accomplish this.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
sam davis
Greenhorn

Joined: Jun 16, 2003
Posts: 18
Thanks Joel for your help. However, i found roll() method to be better. Appreciate your help ..
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

...But roll won't change the year!
From the
documentation

Example: Consider a GregorianCalendar originally set to August 31, 1999. Calling roll(Calendar.MONTH, 8) sets the calendar to April 30, 1999. Using a GregorianCalendar, the DAY_OF_MONTH field cannot be 31 in the month April. DAY_OF_MONTH is set to the closest possible value, 30. The YEAR field maintains the value of 1999 because it is a larger field than MONTH.

[ July 09, 2003: Message edited by: Joel McNary ]
sam davis
Greenhorn

Joined: Jun 16, 2003
Posts: 18
Hey Joel,
You are right..I'm sorry but i don't think i was clear about my response..i was talking about changing month...thats what i used roll() for..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date for last month
 
Similar Threads
Convert String to Date
first date and last date of month
problem: How to get last month date
Comparing 2 dates
getting first and last date of past 6 months?