Date

How can I get todays date and minus 1 day from it i.e gets yestredays datethanks
[108 byte] By [Rori] at [2007-9-26 3:26:48]
# 1
Use java.util.Calendar.roll(Calendar.DATE, false)
neville_sequeira at 2007-6-29 11:48:52 > top of Java-index,Archived Forums,Java Programming...
# 2
I can see why you'd use the roll() method for months, but is there any advantage to using it for the day? It seems more intuitive to use:java.util.Calendar.add(Calendar.DATE,-1);Rich
rmiller1985 at 2007-6-29 11:48:52 > top of Java-index,Archived Forums,Java Programming...
# 3

Actually, roll() won't give you "yesterday's" or "tomorrow's" date when working with a date where the value of its subject field is at the beginning or end of the month of its range.

For example, roll(Calendar.DATE, false) will change a Calendar object from August 1 to August 31, not to July 31.

Calendar.add() is the method of choice.

DragonMan at 2007-6-29 11:48:52 > top of Java-index,Archived Forums,Java Programming...
# 4
Yes, java.util.Calendar.add() is certainly the method of choice.Thanks for pointing out the subtleties associate with the behaviour of roll()
neville_sequeira at 2007-6-29 11:48:52 > top of Java-index,Archived Forums,Java Programming...
# 5
I am getting a date in a string format like this "2001-08-08" how can I get the yesterday's date from it...Thanks again
Rori at 2007-6-29 11:48:52 > top of Java-index,Archived Forums,Java Programming...
# 6
1. Set up a DateFormatter object with the appropriate format.2. Parse the incoming string into a Date object.3. Construct a GregorianCalendar object using the Date object.4. Use the add() method on the GregorianCalendar object.Rich
rmiller1985 at 2007-6-29 11:48:52 > top of Java-index,Archived Forums,Java Programming...
# 7
Can you show me how can I construct a GregorianCalendar object using the Date object.Thanks alot
Rori at 2007-6-29 11:48:52 > top of Java-index,Archived Forums,Java Programming...
# 8

Hello,

I am doing it like this

SimpleDateFormat formatExp = new SimpleDateFormat ("MM-dd-yyyy");

Date expirationdate = new Date();

try {

expirationdate = formatExp.parse(s3ppEffect);

}

catch(ParseException e){

System.out.println(" DATE NOT FOUND " );

}

java.util.Calendar dbEffDate = Calendar.getInstance();

dbEffDate.setTime(expirationdate);

java.util.Calendar.add(dbEffDate.setTime(expirationdate), -1);

add is expectin (int, int) not (date, int)

any ideas thanks

Rori at 2007-6-29 11:48:52 > top of Java-index,Archived Forums,Java Programming...
# 9
You want to change this:java.util.Calendar.add(dbEffDate.setTime(expirationdate), -1); to this:dbEffDate.add(Calendar.DATE,-1)If you import java.util.*, you don't have to worry about fully qualifying each class.Rich
rmiller1985 at 2007-6-29 11:48:52 > top of Java-index,Archived Forums,Java Programming...
# 10
thanks for the help it worked fine.
Rori at 2007-6-29 11:48:52 > top of Java-index,Archived Forums,Java Programming...