0

I am trying to ensure that my nextday method can add ONE day to the date the user entered and ensure it adds 40 days correctly, taking into account the correct days per month and leap years

public nextday() {
    for (int count = 1; count < 40; count++)
        ;
}

public String toDayDateString() // toDayDateString method {
    int countDays = 0;

    for (int i = 1; i < getMonth(); i++) {
        if (i == 2 && checkLeapYr(getYear()))
            countDays += 29;
        else
            countDays += daysPerMonth[i];
    }
    countDays += date;
    String message = String.format("\n", countDays, getYear());
    return message;
}

private Boolean checkLeapYr(int inYear) { // check leap year method.
    if (getYear() % 400 == 0 || (getYear() % 4 == 0 && getYear() % 100 != 0))
        return true;
    else
        return false;
}

Below is a menu that is supposed to allow the user to choose to enter a date or quit, but the date is not being accepted correctly.

{
 public static void main ( String [] args)
// User selects how they will enter the dates
 {      
   +"(1) Enter the date as MM/DD/YYYY\n"
   +"(Any Key) Quit\n";
  int input=0;

  Date newDate;

  do{
   String userInput = JOptionPane.showInputDialog(null, menu);
   input = Integer.parseInt( userInput);
   String outputMessage="";

   if ( input = 1)
   {
    userInput =JOptionPane.showInputDialog(null, "Please enter a date as 02/28/2011");

    switch ( input )  // here is where the menu choice will be evaluated
    {
     case 1 :
      token = userInput.split("/");
      if (token.length == 3 )

      {
       newDate = new Date( Integer.parseInt( token[0]),
        Integer.parseInt( token[1]), Integer.parseInt( token[2]) );
       outputMessage = newDate.toString();
       JOptionPane.showMessageDialog(null, outputMessage);
      }

      break;

     case 2:
  } while ( input <>1); // this will quit the program when user selects any key other than 1 at the menu.
Meesh
  • 379
  • 1
  • 13
Bklyn_40
  • 21
  • 1
  • 1
  • 7
  • Btw, that `if (input = 1)` isn't going to do what you think it is, and "not equals" is `!=`. – Dave Newton Sep 24 '13 at 15:41
  • 1
    Take a look at the `Calendar` class. It has all you need. – jboi Sep 24 '13 at 15:45
  • 2
    Do not reinvent the wheel. You can use either java.util.Calendar API (it is a bit cumbersome, but it's OK for that usage), or Joda Time API. See answers to this question: http://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java?rq=1 – Cyrille Ka Sep 24 '13 at 15:46

3 Answers3

2
  • You should use java.text.DateFormat to parse date strings
  • Writing your own date arithmetic is very error prone (leap years are only one exception among many), better use a java.util.Calendar implementation instead.

As for the date arithmetic, here's how it's done with Calendar:

//TODO: parse input string with DateFormat
Date startDate = ... // your parsed date

Calendar cal = new GregorianCalendar();
cal.setTime(startDate);

for (int i = 0; i < 40; i++) {

    // add a day
    cal.add(Calendar.DATE, 1);

    // and get the new result as date
    Date date = cal.getTime();

    System.out.println(date);
}

If you need to count backwards, add negative time amounts (days, hours etc).

Peter Walser
  • 15,208
  • 4
  • 51
  • 78
0
    public static void main(String[] args) throws IOException, ParseException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter date(dd/mm/yyyy) : ");
    String input = br.readLine();
    DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
    Calendar cal = Calendar.getInstance();
    Date date = df.parse(input);
    cal.setTime(date);
    cal.add(Calendar.MONTH, 1); // to get actual month,as it parses 02 as Jan.
    for (int i = 1; i < 40; i++) {
        System.out.println(" day " + i + " : " + cal.getTime());
        cal.add(Calendar.DATE, 1);
    }
}
Dax Joshi
  • 143
  • 11
0
    public static void main(String[] args) throws IOException,ParseException{
                BufferedReader br = new BufferedReader(new   InputStreamReader(System.in));       
                System.out.print("Enter date(dd/mm/yyyy) : ");
                String input = br.readLine();
                DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                Calendar cal = Calendar.getInstance();
                Date date = df.parse(input);
                cal.setTime(date);
                for (int i = 1; i <=40; i++) {
                    cal.add(Calendar.DATE,1);
                    System.out.println(" day " + i + " : " + cal

.getTime());

            }
        }