TL; DR
try {
LocalDate localDate = LocalDate.parse(
"40:03:2010" , // "40:03:2010" is bad input, "27:03:2010" is good input.
DateTimeFormatter.ofPattern( "dd:MM:uuuu" )
) ;
} catch ( DateTimeParseException e ) {
… // Invalid input detected.
}
Using java.time
The modern way is to use the java.time classes built into Java 8 and later.
The data in your example does not match the format shown in your sample code. One uses the SOLIDUS character (slash), and the other uses the COLON character. I will go with the COLUMN.
DateTimeFormatter
Define a formatting pattern that matches the input line.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd:MM:uuuu" );
LocalDate
Disassemble as an object LocalDate, since the entrance has no time of day and does not have a time zone.
LocalDate localDateGood = LocalDate.parse( "27:03:2010" , f );
System.out.println( "localDateGood: " + localDateGood );
Now try typing the wrong entry. Trap for the corresponding exception.
try {
LocalDate localDateBad = LocalDate.parse( "40:03:2010" , f );
} catch ( DateTimeParseException e ) {
System.out.println( "ERROR - Bad input." );
}
IdeOne.com.
localDateGood: 2010-03-27
- .
ISO 8601
ISO 8601 / . , , .
YYYY-MM-DD, 2010-03-27.
java.time ISO 8601 / . .
LocalDate localDate = LocalDate.parse( "2010-03-27" );
String output = localDate.toString();
java.time
java.time Java 8 . legacy , java.util.Date, Calendar SimpleDateFormat.
Joda-Time, , java.time.
, . Oracle. Qaru . JSR 310.
java.time?
ThreeTen-Extra java.time . java.time. , Interval, YearWeek, YearQuarter .