I'm doing some Excel to XML Conversion in Java. I have one date field in excel worksheet with the following format: dd/mm/yyyy
I want my Java code to validate the format when it reads the record in excel worksheet. I have the following code but it's not really working for me. Any input would be really helpful.
Note: No matter whatever format I change in worksheet it still shows blank.
public void setEffectiveDate(String effectiveDate) {
boolean result = isValidDateFormat(effectiveDate);
if(result == true){
Date date = HSSFDateUtil.getJavaDate(Double
.parseDouble(effectiveDate));
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
this.effectiveDate = dateFormat.format(date);
}
else{
this.effectiveDate = null;
}
}
public static boolean isValidDateFormat(String date) {
boolean isValid = false;
Date date2 = HSSFDateUtil.getJavaDate(Double
.parseDouble(date));
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
try {
cal.setTime(sdf.parse(date));
if (date2.equals(sdf.format(cal.getTime()))) {
isValid = true;
}
} catch (Exception e) {
isValid = false;
}
return isValid;
}