0

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;
}   
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • What shows blank? Please show what's being called, what the result is and what your expectations are? – Sotirios Delimanolis Sep 13 '13 at 23:00
  • 2
    I think the answer to this [post](http://stackoverflow.com/questions/13568543/how-do-you-specify-the-date-format-used-when-jaxb-marshals-xsddatetime) answers your question. – Jason Holmberg Sep 13 '13 at 23:06

1 Answers1

0

i doubt if this is problem of lenient mode of SDF class. you will need to set it to false else it will convert any numbers to a 'nearby' date.

Akhilesh Singh
  • 2,548
  • 1
  • 13
  • 10