• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting a Date that is 30 days ago from today

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All, I want to display a Date that is 30 days ago from today, and I used Date object here is my code,




However, the display result is incorrect. i.e. The month value is incorrect, today is Jun 4, so 30 days ago, the month should be May (which is 4 numerically). can somebody tell me what i did wrong, or rather, let me know if I should use something different to get the date that is 30 days ago

thanks
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should have a look at the Calendar object and its roll(int field, int value) method, which you should find a little easier to use.
 
kay lin
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Sturrock:
I think you should have a look at the Calendar object and its roll(int field, int value) method, which you should find a little easier to use.



But the roll methods only takes care of the field you use, not the larger fields though. For example, if today is June 4, and I want it to be 30 days ago, then only the Day dispaly will be correct not the month, any other suggestions?
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The simple answer is found if you do

System.out.println(1000 * 60 * 60 * 24 * 30);

Result -1702967296

You are overflowing an int, that why Date uses a long.

Try
Date from = new Date( to.getTime() - 1000L*60L*60L*24L*30L);

Force these to a long!

Of course the Calendar is a great suggestion
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use Calendar's add() method rather than roll(), to make things simpler. And note that Date's getXXX() methods are deprecated. Better to use a SimpleDateFormat, or a Calendar, to extract this info from a Date. (Or use the Date's toString() method, which may be exactly what you want.)
 
Get me the mayor's office! I need to tell her about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic