File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes get the Yesterday's date  ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "get the Yesterday Watch "get the Yesterday New topic
Author

get the Yesterday's date ??

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1368
How can i get the Yesterday's date(dd-mm-yyyy) thru calendar (or) GregorianCalendar (or) Date ?
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
you can use -

add(Calendar.DATE, -1) method on Calendar object to get previous date.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1368
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

Joined: Oct 10, 2000
Posts: 1364
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Yes you can use the getTime() method in the Date class and subtract 86400000 and again convert this to the date format you need.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get the Yesterday's date ??
 
Similar Threads
plz help me, with Calendar..
Getting yesterday's date ......
java.util.Date
Date in java
Previous Day - Using date function