Could anybody please tell me why I'm getting "10/09/2022" on the console?
String sFecha = "10/21/2021";
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(sdf.format(sdf.parse(sFecha)));
} catch (java.text.ParseException e) {
//Expected execution
}
Note: the input string is intentionally wrong - I am expecting the Exception!
CodePudding user response:
your format is day/month/year. 21 is not a valid month, It seems it subtracts 12 to get a valid one.
CodePudding user response:
When you do sdf.parse()
you convert your text to date with :
10 -> days
21 -> month
2021 -> year
And 21 as month is converted to 9 (because 21 % 12 = 9
).
Using setLenient(false)
it will throw an exception instead:
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.