This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How to get past date ?  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd use simple math:
long now = System.currentTimeMillis();
long eightyDays = (long)80 * 24 * 60 * 60 * 1000;
Date then = new Date(now-eightyDays);
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can use the Calendar class:

Cheers, Neil
 
lily ling
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, Neil and John. I got all I wanted.
Lily
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!