0

I want to support two date format in my code.

1) dd-MMM-yy

2) dd-MMM-yyyy

On the basis of year format in input dateString, I have to perform further date manipulation.

I have written below code for formatting the dateString.

    SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yy");

    SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yyyy");

    boolean doSomething = false;

    try {
        date = format1.parse(dateString);
    } catch (ParseException e1) {
        try {
            date = format2.parse(dateString);
            doSomething = true;
        } catch (ParseException e2) {
            System.out.println("Invalid format !");
        }
    }

    if (doSomething) {
        // Code
    }

Now my problem is, irrespective of whether the dateString has 2 digit or 4 digit year, it always get formatted by format1.

Example - 17-AUG-97 and 17-AUG-2097, both gets formatted by format1 only.

Is there a way that I can bring the control to format2.

0 Answers0