简体   繁体   English

如何检查Date对象或Calendar对象是否是有效日期(真实日期)?

[英]How to check if a Date object or Calendar object is a valid date (real date)?

SimpleDateFormat class's method: SimpleDateFormat类的方法:

public void setLenient(boolean lenient)

Specify whether or not date/time parsing is to be lenient. 指定日期/时间解析是否宽松。 With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match this object's format. 通过宽大的解析,解析器可以使用启发式方法来解释与该对象的格式不完全匹配的输入。 With strict parsing, inputs must match this object's format. 在严格分析的情况下,输入必须与该对象的格式匹配。

or the Calendar class's method: Calendar类的方法:

public void setLenient(boolean lenient)

Specifies whether or not date/time interpretation is to be lenient. 指定日期/时间解释是否宽松。 With lenient interpretation, a date such as "February 942, 1996" will be treated as being equivalent to the 941st day after February 1, 1996. With strict (non-lenient) interpretation, such dates will cause an exception to be thrown. 如果采用宽大的解释,则将诸如“ 1996年2月942年”之类的日期视为等同于1996年2月1日之后的第941天。使用严格的(非宽容)解释,这样的日期将引发异常。 The default is lenient. 默认值为宽松。

checks for the conformity of the format or rolls the date. 检查格式是否一致或滚动日期。

I would like to confirm if the date with MM-DD-YYYY (02-31-2016) should return invalid as 31day in feb is not a real date so should 04-31-1980 also return invalid. 我想确认是否带有MM-DD-YYYY (02-31-2016)的日期应返回无效,因为2月中的31day不是真实日期,所以1980年4月31日也应返回无效。

Would not like to use Joda time API from Java 8 however any suggestion on this would be greatly appreciated. 不想使用Java 8中的Joda time API,但是对此的任何建议将不胜感激。

Using Java Time API, this can be done using the STRICT ResolverStyle : 使用Java Time API,可以使用STRICT ResolverStyle完成此操作:

Style to resolve dates and times strictly. 严格解析日期和时间的样式。

Using strict resolution will ensure that all parsed values are within the outer range of valid values for the field. 使用严格的分辨率将确保所有解析的值都在该字段的有效值的外部范围内。 Individual fields may be further processed for strictness. 为了严格起见,可能会进一步处理各个字段。

For example, resolving year-month and day-of-month in the ISO calendar system using strict mode will ensure that the day-of-month is valid for the year-month, rejecting invalid values. 例如,使用严格模式在ISO日历系统中解析年月和月日将确保月日对年月有效,而拒绝无效值。

public static void main(String[] args) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd-yyyy")
                                                   .withResolverStyle(ResolverStyle.STRICT);
    LocalDate.parse("02-31-2016", formatter);
}

This code will throw a DateTimeParseException since it is not a valid date. 此代码将引发DateTimeParseException因为它不是有效日期。

By default, a formatter has the SMART ResolverStyle : 默认情况下,格式化程序具有SMART ResolverStyle

By default, a formatter has the SMART resolver style. 默认情况下,格式化程序具有SMART解析程序样式。

but you can change this by calling withResolverStyle(resolverStyle) on the formatter instance. 但是您可以通过在格式化程序实例上调用withResolverStyle(resolverStyle)来更改此设置。

final static String DATE_FORMAT = "dd-MM-yyyy";

public static boolean isDateValid(String date) 
{
        try {
            DateFormat df = new SimpleDateFormat(DATE_FORMAT);
            df.setLenient(false);
            df.parse(date);
            return true;
        } catch (ParseException e) {
            return false;
        }
}

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

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