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)

Recommended Answers

All 14 Replies

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

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

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));

You put the brackets in the wrong place

You put the brackets in the wrong place

but where's wrong?

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

So, it's working now?

So, it's working now?

no

I just copied and ran your original code (with the latest change to the MONTH line) and it works here.

It worked? Did you change something?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.