Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!