简体   繁体   English

在a年中解析MonthDate字符串

[英]parsing MonthDate string in a leap year

please consider the following snippet: 请考虑以下代码段:

    SimpleDateFormat parser = new SimpleDateFormat("MMdd");
    parser.setLenient(false);
    try {
        Date date = parser.parse("0229");
        System.out.println(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

I have a text field which should contain a date in the format MMdd (no year, as it should always default to the current year). 我有一个文本字段,其中应包含MMdd格式的日期(没有年份,因为它应始终默认为当前年份)。

This year, 2012, a leap year, I found myself in the strange situation of not being able to parse the valid date February 29th. 今年,2012年,是a年,我发现自己处于无法解析2月29日有效日期的奇怪状况。

The calendar used by the code above always defaults to 1970, which, though luck, was not a leap year. 上面的代码使用的日历始终默认为1970,尽管运气不好,但这并不是a年。 Hence trying to parse "0229" always throws a parse exception. 因此,尝试解析“ 0229”总是会引发解析异常。

Any idea on how to parse this? 关于如何解析这个想法?

附加当前年份并使用“ MMddyyyy”格式吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM