【问题标题】:java.time.DateTimeException: Invalid date 'February 29' as '2017' is not a leap year Javajava.time.DateTimeException:无效日期 'February 29' 因为 '2017' 不是闰年 Java
【发布时间】:2017-10-02 05:28:40
【问题描述】:

我正在尝试在 Java 中获取开始和结束日期,我有从一月到十二月的月份列表 JCombobox 和年份 JCombobox 到显示年份。

首先我已将String 转换为月数并使用此代码:

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

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

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

    }

然后我创建了一个字符串dateString 以使用LocalDate 获取所选月份的第一个和最后一个日期,一切正常,但是当我选择2017 年2 月和2017 年时,它给了我异常java.time.DateTimeException: Invalid date 'February 29' as '2017' is not a leap year

获取第一个和最后一个日期的代码是

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

我不知道我做错了什么,请有人帮助我

【问题讨论】:

  • 我知道,但是你能解释一下异常是如何引发的
  • 顺便说一下,将来请包含异常的完整堆栈跟踪 - 和/或在代码中添加注释,说明异常发生在哪一行。
  • 好的,我得到了答案

标签: java date jcombobox localdate


【解决方案1】:

这是因为您在这一行中使用了maxLength()

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

maxLength() 方法返回本月的最大天数,正如API documentation 所说。 2 月份确实最多有 29 天(只是在 2017 年没有,但这是一般的 2 月份,而不是在任何特定年份!)。

这应该可行,因为它考虑了特定年份的月份长度:

LocalDate endDate = date.withDayOfMonth(date.lengthOfMonth());

【讨论】:

【解决方案2】:

maxLength() 方法不适用于确定 2 月的上下文相关长度(28 天或 29 天)。该方法总是产生 29。

您应该考虑使用lengthOfMonth() 方法。

【讨论】:

【解决方案3】:
        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;
        }
    }

试试上面的代码。 希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 2010-10-08
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多