Link to home
Start Free TrialLog in
Avatar of blarouche
blarouche

asked on

Find a future date

Hi Experts


I would like to find what will be the date for the current  year on March first - 5 days.

I would like to get this date as an int this format :  example 20070220.


How would I do that ?

Thank you


ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
DateFormat df = new SimpleDateFormat("yyyymmDD");
int date = Integer.parseInt(df.format(cal.getTime());
int date = Integer.parseInt(df.format(cal.getTime()));
Avatar of blarouche
blarouche

ASKER

Sorry CEHJ


I accepted the answer but I found out an error

My result give me 200611359 which is wrong
I think there is a mistake in the code.

I changed it for :

Calendar cal = Calendar.getInstance();
            cal.set(Calendar.MONTH, Calendar.FEBRUARY);
            cal.set(Calendar.DATE,cal.getActualMaximum(Calendar.MONTH));
            cal.add(Calendar.DATE, -4);
            
            DateFormat df = new SimpleDateFormat("yyyymmDD");
            int date = Integer.parseInt(df.format(cal.getTime()));
            System.out.println(date);

But it still doesn't work

I found something that works for me.


Calendar cal = Calendar.getInstance();
            cal.set(Calendar.MONTH, Calendar.MARCH);
            cal.set(Calendar.DATE, 01);
            cal.add(Calendar.DATE, -5);
            
            Date myDate = cal.getTime();
            DateFormat df = new SimpleDateFormat("yyyyMMdd");
            int marchdate = Integer.parseInt(df.format(myDate));
            System.out.println(marchdate);
Yes that's better actually