• 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

how can i get yesterdays date?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.util.Date todaysDate = new java.util.Date();

thats todays date but how wud i go about geting yesters date?
is there any way i can say 'take one day off todaysDate' to get yesterdays date?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Robert,
Yes! You need to use a calendar instead of a date at first:


[edited to add -1 instead of 1]
[ March 27, 2005: Message edited by: Jeanne Boyarsky ]
 
Robert Johnson
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Calendar cal = new GregorianCalendar(); // creates with today's date
cal.add(1, Calendar.DATE);
Date yesterday = cal.getTime();



sure u dont "add 1" ??
should that be a -1 ??

i just after found these on the web. I'll try the three. Thanks for replying.

Calendar theDate = Calendar.getInstance(); // returns a new locale-specific
calendar set to the current time.
theDate.add(Calendar.DATE, -1);


This converts todays date in milliseconds. Then subtracts the number
of milliseconds in 24 hrs (24*60*60*1000) from it.
Thenit makes a date object by using above calculated milliseconds into constructor.

Date mydate=new Date();
long mymillidate=mydate.getTime();
mymillidate=mymillidate - 24*60*60*1000;
Date previousdate= new Date(mymillidate);
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Robert,
Yes, it should be a -1. I edited my post. Thanks for pointing this out.

I prefer using the calendar method than dealing with milliseconds. It makes the intent of what you are trying to do clearer.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The calendar also takes care of things like daylight savings for you. Otherwise you might go from 12 noon to 1 PM or 11 AM when you cross a transition.

In general I despise the Calendar API as being unnecessarily convoluted and confusing. And so I avoid it whenever possible. But this problem is precisely the sort of task that GregorianCalendar is cut out for.
[ March 27, 2005: Message edited by: Jim Yingst ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic