This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to get past date ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get past date ?" Watch "How to get past date ?" New topic
Author

How to get past date ?

lily ling
Greenhorn

Joined: Dec 03, 2001
Posts: 25
Hi, there
I want to get the date which is 80 days before certain date ( let's say today ), may I ask how to compute that date ?
Thanks in advance,
Lily
john guthrie
Ranch Hand

Joined: Aug 05, 2002
Posts: 124
I'd use simple math:
long now = System.currentTimeMillis();
long eightyDays = (long)80 * 24 * 60 * 60 * 1000;
Date then = new Date(now-eightyDays);
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
Or you can use the Calendar class:

Cheers, Neil
lily ling
Greenhorn

Joined: Dec 03, 2001
Posts: 25
Thanks a lot.
Yes, by using Calendar ( the second way ), I could print out the time I want ( 80 days ago, let's say ). If I want to know if a Date obj passed in is before this Calendar date, how to convert that Date obj to a Calendar obj so that I could use CalendarObj.before( passed_in_Date_to_CalendarObj ) to do the comparison ?
Tons of thanks,
Lily
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
The Calendar instance method getTime() returns a java.util.Date so you readily compare the 2, for example:

Returns:

21-05-2002 before 09-08-2002 = true
21-05-2002 before 09-08-2002 = true

Cheers Neil
lily ling
Greenhorn

Joined: Dec 03, 2001
Posts: 25
Thanks a lot, Neil and John. I got all I wanted.
Lily
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to get past date ?