-1

I have an application that asks a user to input a start date. Then the user will go through a series of 5 pages and input data to be exported to an excel sheet with page 1 being on the first block with the date day 2 being on the second block with a incremented date etc etc. Is there a any type of function available to accomplish this or am I doomed to the use of if statements to increment the date correctly. I tried to increment the date by simply returning the date in a series of variables

    int day = getIntent().getExtras().getInt("day");
    int year = getIntent().getExtras().getInt("year");
    String month = getIntent().getStringExtra("month");
    String dayofweek = getIntent().getStringExtra("dayofweek");

And the I would output those variables in the appropriate cells for the excel sheet. Monday would be just the variables since thats the start date. Tuesday would be the day + 1. Wednesday would be the day + 1 to continuously increment it to day 5. The problem I ran into is it the start day is selected as the last day of the month it would continue to increment the day ie Jan 31 Jan 32 Jan 33 Jan 34 etc etc. So I have to take the end of the month, end of the year, and leap year in 2016 and 2020 into account. On top of weekends. For example if Tuesday is selected I want the start date to show up on tuesday the fourth date would show up on friday and then the next correct date on monday again.

So I guess the short of it all would be Is there a better way to implement this?

  • possible duplicate of [How can I increment a date by one day in Java?](http://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java) – Basil Bourque Nov 25 '14 at 06:52

1 Answers1

0

You have to make a date or calendar object to increment....

1 for month is February. The 30th of February is changed to 1st of March. You should set 0 for month. The best is to use the constant defined in Calendar:

c1.set(2000, Calendar.JANUARY, 30);

Then you can increment it

c1.add(Calendar.DATE, 1);
Date newDate = c1.getTime();  

You will get new date object of the next day :)

Salmaan
  • 3,543
  • 8
  • 33
  • 59