public class Demo {
public static void main ( String[ ] args) {
System. out. println ( checkDate ( "2020-12-16" ) ) ;
System. out. println ( checkDate ( "2020-01-16" ) ) ;
System. out. println ( checkDate ( "2020-13-32" ) ) ;
System. out. println ( checkDate ( "2020-1-32" ) ) ;
System. out. println ( checkDate ( "2019-13-16" ) ) ;
System. out. println ( checkDate ( "2020-1" ) ) ;
}
private static boolean checkDate ( String str) {
String[ ] dateFields = str. split ( "-" ) ;
int year = 0 ;
int month = 0 ;
int day = 0 ;
try {
year = Integer. parseInt ( dateFields[ 0 ] ) ;
month = Integer. parseInt ( dateFields[ 1 ] ) ;
day = Integer. parseInt ( dateFields[ 2 ] ) ;
} catch ( Exception e) {
return false ;
}
List< Integer> bigMonthList = Arrays. asList ( 1 , 3 , 5 , 7 , 8 , 10 , 12 ) ;
List< Integer> smallMonthList = Arrays. asList ( 4 , 6 , 9 , 11 ) ;
if ( month == 2 ) {
if ( year % 4 == 0 ) {
if ( day > 0 && day < 30 ) {
return true ;
}
} else {
if ( day > 0 && day < 29 ) {
return true ;
}
}
}
if ( bigMonthList. contains ( month) ) {
if ( day > 0 && day < 32 ) {
return true ;
}
} else if ( smallMonthList. contains ( month) ) {
if ( day > 0 && day < 31 ) {
return true ;
}
} else {
return false ;
}
return false ;
}
}