java - set the calendar month

I want to set the calendar date backed to 01-08-2006, but it keeps return the 01-09-2006

Please correct my mistakes

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

            FirstDateOfLastMonthStr="01-08-2006"

            String[] dtArry = new String[2];
            dtArry = FirstDateOfLastMonthStr.split("-");
            int startPeriodDate = Integer.parseInt(dtArry[0]);
            int startPeriodMonth = Integer.parseInt(dtArry[1]);
            int startPeriodYear = Integer.parseInt(dtArry[2]);
            cd.set(Calendar.MONTH, startPeriodMonth );
            cd.set(Calendar.DATE, startPeriodDate );
            cd.set(Calendar.YEAR, startPeriodYear );

            System.out.println("Now : Last month date:" + dateFormat.format(cd.getTime()));

*** It returns 01-09-2006 instead of 01-08-2006
LeanMoreTryMoreAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
month is zero based, so use:

            cd.set(Calendar.MONTH, startPeriodMonth-1 );
LeanMoreTryMoreAuthor Commented:
startPeriodMonth is the month need to be set to. 08 is last month.
Mick BarryJava DeveloperCommented:
what month do you ewant to set it to?  8 = September
gkishorejiCommented:
when u have a date string, u shud parse the date,

      SimpleDateFormat dateFormat =  new SimpleDateFormat("dd-MM-yyyy");
      String FirstDateOfLastMonthStr="01-08-2006";
      Date cd = dateFormat.parse(FirstDateOfLastMonthStr);
      System.out.println("Now : Last month date:" + dateFormat.format(cd));

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LeanMoreTryMoreAuthor Commented:
I work it out. it should use

Calendar cd= new GregorianCalendar();
cd.add(Calendar.MONTH,-1)

but NOT TO use cd.getTime() <=== this is reset the date

Thanks you for all to help me anyway
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.