Specific date minus X days
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Morgan_Erickson
Posted On:   Tuesday, August 28, 2001 08:26 AM

I am trying to write something that will take a date in yyyymmdd format and roll it back X number of days. For example, given 20010403 and 2 you should get 20010401. So far I have: public static String DateRoller(String strDate, int intDaysToRoll) { java.util.Calendar rcal = null; rcal = GregorianCalendar.getInstance(); for(int i = 1;i { rcal.roll(rcal.DATE,false); //Roll back 1 day... } Date curDate = rcal.getTime(); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyymmdd"); return sdf1.format(curDate); } //End DateRoller I'm not getting any valid dates returned   More>>

I am trying to write something that will take a date in yyyymmdd format and roll it back X number of days.



For example, given 20010403 and 2 you should get 20010401.



So far I have:



			
public static String DateRoller(String strDate, int intDaysToRoll)
{
java.util.Calendar rcal = null;
rcal = GregorianCalendar.getInstance();
for(int i = 1;i
{
rcal.roll(rcal.DATE,false); //Roll back 1 day...
}
Date curDate = rcal.getTime();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyymmdd");
return sdf1.format(curDate);
} //End DateRoller




I'm not getting any valid dates returned...



Any ideas?



Thanks!    <<Less

Re: Specific date minus X days

Posted By:   AlessandroA_Garbagnati  
Posted On:   Tuesday, August 28, 2001 09:59 AM

Morgan,


You problem is that in the SimpleDateFormat, 'mm' means minutes, while you are looking for 'MM' (month).


You can use either roll or, eventually, the add() method (obviously, if you want to subtract days you need to use a negative amount):


...

rcal.add(Calendar.DATE, -2);

Date curDate = rcal.getTime();

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");

return sdf1.format(curDate);

About | Sitemap | Contact