import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;

public class CheckIn{

	Date today;


	void getArrivalDate() {

		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

		try {

			System.out.print("Enter Date in dd/mm/yyyy format: ");
			String d = Keyboard.readString();
			today = df.parse(d);
			df.format(today);

		} catch (ParseException e) {

		e.printStackTrace();
		}
	}

	void getDepartureDate(){


		Calendar ca1 = Calendar.getInstance();
    	ca1.setTime(today);

    	System.out.print("How many nights do you want to stay? ");
		int nights = Keyboard.readInt();

    	ca1.add(Calendar.DATE, nights);

		System.out.println((ca1.get(Calendar.DATE))
                        + "/"
                        + ca1.get(Calendar.MONTH + 1)
                        + "/"
                        + ca1.get(Calendar.YEAR));


	}
}

When I'm testing the method getDepartureDate(), the month isn't working well.

Eg. If I input 12/12/2011(Arrival Date) I get 14/50/2011 as a Departure Date.

I really can't understand this(Beginner)

Edited 5 Years Ago by Matth963: n/a

ca1.get(Calendar.MONTH + 1)
Calendar.MONTH is an int constant signifying "month", so MONTH+1 is another int constant signifying (I don't know what), so it get's the wrong field. You need
(ca1.get(Calendar.MONTH) + 1)

ca1.get(Calendar.MONTH + 1)
Calendar.MONTH is an int constant signifying "month", so MONTH+1 is another int constant signifying (I don't know what), so it get's the wrong field. You need
(ca1.get(Calendar.MONTH) + 1)

Now I'm getting the Month always '01'....help please

Edited 5 Years Ago by Matth963: n/a

That's odd - please post the relevant code in its latest version

System.out.println((ca1.get(Calendar.DATE)
                        + "-"
                        + ca1.get(Calendar.MONTH) + 1)
                        + "-"
                        + ca1.get(Calendar.YEAR));

Look at the suggestion I coded and look at your code. Think about it. I'm here to help you learn to be a Java master, not to do it for you :-)

System.out.println(ca1.get(Calendar.DATE)
                        + "-"
                        + (ca1.get(Calendar.MONTH) + 1)
                        + "-"
                        + ca1.get(Calendar.YEAR));

Is that good? ... Stil 1

Edited 5 Years Ago by Matth963: n/a

It worked? Did you change something?

Edited 5 Years Ago by Matth963: n/a

Here's a simplified version of your code that shows adding days to today's date and printing them out...

public static void main(String[] args) {

      Calendar ca1 = Calendar.getInstance();

      int nights = 8;
       
      ca1.add(Calendar.DATE, nights);
       
      System.out.println((ca1.get(Calendar.DATE))
      + "/"
      + (ca1.get(Calendar.MONTH) + 1)
      + "/"
      + ca1.get(Calendar.YEAR));
       
   }

Run that, confirm it works, then put the rest of your code back into it until you find out where it breaks.
ps I have to go out in 5 minutes from now...

When I changed the line of the Month I had the date format dd/mm/yyyy....now ic hanged it to dd/MM/yyyy and it's working.

THANKS

This question has already been answered. Start a new discussion instead.