954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java Calendar & Date

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)

Matth963
Newbie Poster
16 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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)

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
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

Matth963
Newbie Poster
16 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
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));
Matth963
Newbie Poster
16 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

You put the brackets in the wrong place

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
You put the brackets in the wrong place


but where's wrong?

Matth963
Newbie Poster
16 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
System.out.println(ca1.get(Calendar.DATE)
                        + "-"
                        + (ca1.get(Calendar.MONTH) + 1)
                        + "-"
                        + ca1.get(Calendar.YEAR));


Is that good? ... Stil 1

Matth963
Newbie Poster
16 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

So, it's working now?

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
So, it's working now?


no

Matth963
Newbie Poster
16 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

It worked? Did you change something?

Matth963
Newbie Poster
16 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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...

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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

Matth963
Newbie Poster
16 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: