2

I am try to get starting and ending date in Java, i Have List of Month from January to December in JCombobox and Year JCombobox to Dispay Years.

First i have converted String month to month number and using this Code:

if(month_sands.getSelectedIndex() != -1){
        int monthnumber = month_sands.getSelectedIndex() + 1;

         if(monthnumber>=9)
         {
             month=String.valueOf(monthnumber);

         }else
         {
             month="0"+String.valueOf(monthnumber);
         }

    }

Then Later i have Created a String dateString to get the First and Last date of the Selected month using LocalDate, every thing is working fine but when i select February month and Year 2017 it give me Exception java.time.DateTimeException: Invalid date 'February 29' as '2017' is not a leap year

The code to Get the first and last date is

try {
        String dateString = year_sands.getSelectedItem().toString()+"-"+month+"-"+"01";   
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.US); 
        LocalDate date = LocalDate.parse(dateString, dateFormat);
        LocalDate startDate=date.withDayOfMonth(1);
        LocalDate endDate = date.withDayOfMonth(date.getMonth().maxLength());

        start_date=startDate.toString();
        end_date=endDate.toString();

        System.out.println(start_date);
        System.out.println(end_date);
    } catch (Exception e) {
        e.printStackTrace();
    }

I dont know what am doing wrong please some one help me

Intact Abode
  • 382
  • 5
  • 20

3 Answers3

15

It happens because you are using maxLength() in this line:

LocalDate endDate = date.withDayOfMonth(date.getMonth().maxLength());

The method maxLength() returns the maximum length of this month in days, as the API documentation says. The month February indeed has a maximum of 29 days (just not in 2017, but this is about the month February in general, not in any particular year!).

This should work, because it takes the length of the month in the specific year into account:

LocalDate endDate = date.withDayOfMonth(date.lengthOfMonth());
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Please answer: https://stackoverflow.com/questions/52129200/resolverstyle-strict-is-not-working-in-datetimeformatiso-datetimeformat-iso – ankit Sep 05 '18 at 07:01
3

The method maxLength() is not suitable to determine the context-dependent length of February (either 28 or 29 days). That method always yields 29.

You should consider the method lengthOfMonth() instead.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
1
        String dateString = year_sands.getSelectedItem().toString()+"-"+month+"-"+"01"; 

        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.US);
        LocalDate date = LocalDate.parse(dateString, dateFormat);
        LocalDate startDate = date.withDayOfMonth(1);
        LocalDate endDate = null;
        if (Integer.parseInt(month) == 02) {
            if (isLeapYear(Integer.parseInt(year_sands.getSelectedItem().toString()))) {
                endDate = LocalDate.parse(year_sands.getSelectedItem().toString() + "-02" + "-29", dateFormat);
            }
            else
            {
                endDate = LocalDate.parse(year_sands.getSelectedItem().toString() + "-02" + "-28", dateFormat);
            }
        } else {
             endDate= date.withDayOfMonth(date.getMonth().maxLength());

        }


public static boolean isLeapYear(int year) {
        if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
            return true;
        } else {
            return false;
        }
    }

Try above code. Hope this will helps you.

Sagar Gangwal
  • 7,544
  • 3
  • 24
  • 38