*
The moose likes Java in General and the fly likes how can i get previous date? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how can i get previous date?" Watch "how can i get previous date?" New topic
Author

how can i get previous date?

prasant sahoo
Greenhorn

Joined: Dec 14, 2007
Posts: 7
Hi,
I am entering a future date,lets say(20-08-2008) through my application.I need to perform some actions on the previous date(19-08-2008) of the entered date(20-08-2008).How can i get that previous date? please help..
Rameshwar Kharat
Greenhorn

Joined: Mar 17, 2008
Posts: 3
Try GregorianCalendar
http://www.developer.com/java/article.php/732511
when dealing with the Dates

eg.
code to get yesterday's date using gregorian Calendar

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar today = new GregorianCalendar();
GregorianCalendar yesterday = new GregorianCalendar();
yesterday.add(Calendar.DATE, -1);
String yesDate = new String(yesterday.toString());
System.out.println(yesDate);

Hope this will help you .
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

I'd use Calendar.getInstance() instead of GregorianCalendar. That way you always get the calendar system of the local system; for some countries it may be different.


You can create a Calendar object for a specific date quite easily too:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: how can i get previous date?
 
Similar Threads
Java Date formatting problem
convert date in appropriate format
Hibernate Date Issue
count by current timestamp
java.util.Date does it contain information about hours?