I have a String in java. I want to convert it into Date. I have used SimpleDateFormat. But the problem is that if I pass dd-mm-yy instead of dd-mm-yyyy, then also it parses the String and converts into Date. Below is the code
private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss");
public static void main(String args[]){
Date d = null;
String dat="14-01-16 21:59:59";
try {
d = dateFormat.parse(dat);
}
catch (ParseException e){
e.printStackTrace();
}
System.out.println(d.toString());
}
Ideally it show throw exception becuase in the dateFormatter I have given the format as dd-MM-YYYY but in the string I have passed dd-MM-YY format. So is there a way around to allow those Strings only which have only dd-MM-YYYY format only and disallowing the strings which have dd-MM-YY format.