String is an input and i have to check whether it is in any of these formats. If it is in sdf1 format then pass, if it is in sdf2,3.... then i add the missing the format and parse it with sdf1 format
SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ssa");
SimpleDateFormat sdf3 = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
SimpleDateFormat sdf3 = new SimpleDateFormat("MM/dd/yyyy hh:mma");
SimpleDateFormat sdf3 = new SimpleDateFormat("MM/dd/yyyy hh:mm");
SimpleDateFormat sdf3 = new SimpleDateFormat("MM/dd/yyyy");
Here is what i have
try{
// Parse if it is MM/dd/yyyy hh:mm:ss a
cal = Calendar.getInstance();
cal.setTime(sdf1.parse(inStr));
}catch(Exception exp){
try{
cal = Calendar.getInstance();
//Parse if it is MM/dd/yyyy hh:mm:ssa
sdf2.parse(inStr);
inStr = new StringBuilder(inStr).insert(str.length()-2, " ").toString();
cal.setTime(sdf1.parse(inStr));
}catch(Exception dte2){
try{
cal = Calendar.getInstance();
//Parse if it is MM/dd/yyyy hh:mma
sdf3.parse(inStr);
//if pass then set 00:00:00 AM
inStr = inStr+" AM";
inStr = new StringBuilder(inStr).insert(str.length()-2, ":00").toString();
cal.setTime(sdf1.parse(inStr));
it keeps going like try parsing and if failed in exception block check for the next one. Is there any easy way to do this, may be in JAVA 8? i could use the link from marked as duplicate it talks about how to parse, but i have additional requirement as auto complete with missing format.