java - How to check if a Date object or Calendar object is a valid date (real date)? -


simpledateformat class's method:

public void setlenient(boolean lenient) 

specify whether or not date/time parsing lenient. lenient parsing, parser may use heuristics interpret inputs not precisely match object's format. strict parsing, inputs must match object's format.

or calendar class's method:

public void setlenient(boolean lenient) 

specifies whether or not date/time interpretation lenient. lenient interpretation, date such "february 942, 1996" treated being equivalent 941st day after february 1, 1996. strict (non-lenient) interpretation, such dates cause exception thrown. default lenient.

checks conformity of format or rolls date.

i confirm if date mm-dd-yyyy (02-31-2016) should return invalid 31day in feb not real date should 04-31-1980 return invalid.

would not use joda time api java 8 suggestion on appreciated.

using java time api, can done using strict resolverstyle:

style resolve dates , times strictly.

using strict resolution ensure parsed values within outer range of valid values field. individual fields may further processed strictness.

for example, resolving year-month , day-of-month in iso calendar system using strict mode ensure day-of-month valid year-month, rejecting invalid values.

public static void main(string[] args) {     datetimeformatter formatter = datetimeformatter.ofpattern("mm-dd-yyyy")                                                    .withresolverstyle(resolverstyle.strict);     localdate.parse("02-31-2016", formatter); } 

this code throw datetimeparseexception since not valid date.

by default, formatter has smart resolverstyle:

by default, formatter has smart resolver style.

but can change calling withresolverstyle(resolverstyle) on formatter instance.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -