Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get the Yesterday's date ??

 
kri shan
Ranch Hand
Posts: 1461
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i get the Yesterday's date(dd-mm-yyyy) thru calendar (or) GregorianCalendar (or) Date ?
 
Damanjit Kaur
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • 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: 1461
  • Mark post as helpful
  • send pies
  • 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)
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • 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 ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • 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...
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic