Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: getting the end of the month for a given date

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default getting the end of the month for a given date

    Calendar eCal = Calendar.getInstance();
             eCal.set(2011,11,3);
            //eCal.set(Integer.parseInt(year.getText()), Integer.parseInt(month.getSelectedItem().toString()), Integer.parseInt(day.getSelectedItem().toString()));
            int iEday = eCal.getActualMaximum(eCal.DAY_OF_MONTH);
            System.out.println("Month:"+ eCal.get(eCal.MONTH));
            System.out.println("Max date = "+iEday);
    as stated in the subject... my current codes seems to get the wrong date... when nov is being set to the Calendar, it retrun the Date as 31 while Oct will return the Date 30...


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: getting the end of the month for a given date

    Try

    eCal.getActualMaximum(Calendar.DAY_OF_MONTH);

    That should return 30 though as November, the 11th month, has a maximum of 30 days.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: getting the end of the month for a given date

    no diff... still the same...

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: getting the end of the month for a given date

    Java uses 0 for January and 11 for December. I haven't looked at your code but is this your problem? To avoid problems like this you should use constants in the Calendar class.
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: getting the end of the month for a given date

    Quote Originally Posted by Junky View Post
    Java uses 0 for January and 11 for December. I haven't looked at your code but is this your problem? To avoid problems like this you should use constants in the Calendar class.
    oh ic... but the month is entered by the user... would my codes work if i did (Calendar.DAY_OF_MONTH+1)?

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: getting the end of the month for a given date

    How is the user inputting the month? As an int (11 for November)? Then you would use the set method and subtract one from the input.
    cal.set(Calendar.MONTH, userInput - 1);
    Of course you would add some error checking on the user input.

    If user input is a String ("November" or some variant) then you would need some way to parse the String into its int equivalent.
    DateFormatSymbols dfs = new DateFormatSymbols();
    String[] months = dfs.getMonths();
    for(int index = 0; index < months; index++) {
        if(months[index].equals(userInput)) {
            return index;
        }
    }
    return -1; // indicates invalid month entered
    Improving the world one idiot at a time!

Similar Threads

  1. How much per a month?
    By benglish in forum Totally Off Topic
    Replies: 14
    Last Post: September 23rd, 2013, 04:52 PM
  2. Replies: 1
    Last Post: July 22nd, 2011, 07:08 AM
  3. same date should entered in another date field
    By shashib09 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 14th, 2011, 08:42 AM
  4. Tip of the Month
    By helloworld922 in forum The Cafe
    Replies: 6
    Last Post: July 5th, 2010, 02:42 AM