Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

get the Yesterday's date ??

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can i get the Yesterday's date(dd-mm-yyyy) thru calendar (or) GregorianCalendar (or) Date ?
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can use -

add(Calendar.DATE, -1) method on Calendar object to get previous date.
 
kri shan
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
String date = dateFormat.format(new Date());

It returns the curret date like 3/29/05.
But i need Previous date, How can i get?(like 3/28/05)
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Damanjit said, use the Calendar's add() method.

I'm pasting clips from an example where I was adding a month

Calendar cal = Calendar.getInstance();
cal.add( Calendar.MONTH, 1 );
Date date = cal.getTime();


From Calendar API they show subtraction of 5 days:



add
public abstract void add(int field,
int amount)Date Arithmetic function. Adds the specified (signed) amount of time to the given time field, based on the calendar's rules. For example, to subtract 5 days from the current time of the calendar, you can achieve it by calling:
add(Calendar.DATE, -5).


Parameters:
field - the time field.
amount - the amount of date or time to be added to the field.



If you need to start with a particular Date object, Calendar has a method, setTime(Date date) .
[ March 29, 2005: Message edited by: Carol Enderlin ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kri shan:
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
String date = dateFormat.format(new Date());

It returns the curret date like 3/29/05.
But i need Previous date, How can i get?(like 3/28/05)



As noted above, you should use the Calendar class instead of the Date class. Calendar has an add() method that allows you to change the date value. If you then want to use DateFormat to display the date, you can use the getTime() method to get a Date instance for formatting.

Layne
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kri shan:
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
String date = dateFormat.format(new Date());

It returns the curret date like 3/29/05.
But i need Previous date, How can i get?(like 3/28/05)



I do believe that the following can give you the exact yesterday date...


It retrieves the milliseconds of today's date and substract the one day period(86400000) from today's date so that yesterday's date will be returned...

Hope this helps...
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can use the getTime() method in the Date class and subtract 86400000 and again convert this to the date format you need.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depending on how precise you want to be, the "subtracting 86400 seconds" trick may not work. Some years have "leap seconds" added to December 31st, to allow for drift in the Earth's rotational period. Therefore not every day is exactly 86400 seconds!

The Calendar.add() method, which several smart people described in detail above, is the way to go.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
Depending on how precise you want to be, the "subtracting 86400 seconds" trick may not work. Some years have "leap seconds" added to December 31st, to allow for drift in the Earth's rotational period. Therefore not every day is exactly 86400 seconds!


Dr.Friedman-Hill is right... If we got leap seconds, then "subtracting 86400 seconds" method might result an inexact date... So we should use Calendar.add() method is the best way to get the exact result...

The Calendar.add() method, which several smart people described in detail above, is the way to go.


I'm included in non-smart people above there...
reply
    Bookmark Topic Watch Topic
  • New Topic