Q:

Date Formatting with two dates

I am converting the input string to date. When i try to give two dates as inputs Date of Birth and Date of Joining, i don't get any satisfied result. Also help me with re-promt if the input is invalid. Thanks in advance.   
public class EmployeeInfo {
int id;
static String name, DoBS, DoJS;
Date DoB, DoJ;

public void checkDate(String dt) throws ParseException {

    SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy");
    SimpleDateFormat sdf3 = new SimpleDateFormat("dd MMMM yyyy");

    Date date = null;

    try {
        date = sdf1.parse(dt);
    } catch (ParseException e) {
        try {
            date = sdf2.parse(dt);
        } catch (ParseException e1) {
            try {
                date = sdf3.parse(dt);
            } catch (ParseException e2) {
                String invalid="Invalid,Retry";
                System.out.println(invalid);
                // TODO: Whatever to do when it doesn't find a date
            }
        }
    }

        setDateOfBirth(date);
        setDateOfJoining(date);
}

void setDateOfBirth(Date DoB) {
    this.DoB = DoB;
}

void setDateOfJoining(Date DoJ) {
    this.DoJ = DoJ;
}

void print() {
    System.out.println("User ID: " + id);
    System.out.println("Name: " + name);
    System.out.println("Date Of Birth: " + DoB);
    System.out.println("Date of Joining: " + DoJ);
}

public static void main(String[] args) throws ParseException {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the name: ");
    name = scanner.nextLine();

    System.out.println("Enter the Date Of Birth: ");
    DoBS = scanner.nextLine();

    System.out.println("Enter the Date of Joining: ");
    DoJS = scanner.nextLine();

    EmployeeInfo e = new EmployeeInfo();

    e.checkDate(DoBS);
    e.checkDate(DoJS);
    e.print();

}
}

A:

So I went and fixed it up for you, its a bit hacked together, but will work just fine
First of all, you should have EmployeeInfo as separate class
EmployeeInfo.java
public class EmployeeInfo {
    int id;
    static String name;
    Date DoB, DoJ;

    public void setName(final String name) {
        this.name = name;
    }

    public void setDateOfBirth(final Date DoB) {
        this.DoB = DoB;
    }

    public void setDateOfJoining(final Date DoJ) {
        this.DoJ = DoJ;
    }

    void print() {
        System.out.println("User ID: " + id);
        System.out.println("Name: " + name);
        System.out.println("Date Of Birth: " + DoB);
        System.out.println("Date of Joining: " + DoJ);
    }
}

And then you should have your main class that runs and has all the logic
Main.java
public class Main {    
    public static Date checkDate(final String dt) throws ParseException {

        final SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
        final SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yyyy");
        final SimpleDateFormat sdf3 = new SimpleDateFormat("dd MM yyyy");

        Date date = null;

        try {
            date = sdf1.parse(dt);
        } catch (final ParseException e) {
            try {
                date = sdf2.parse(dt);
            } catch (final ParseException e1) {
                try {
                    date = sdf3.parse(dt);
                } catch (final ParseException e2) {
                    final String invalid = "Invalid,Retry";
                    System.out.println(invalid);
                }
            }
        }
        return date;
    }

    public static void main(final String[] args) throws ParseException {
        final Scanner scanner = new Scanner(System.in);
        final EmployeeInfo e = new EmployeeInfo();
        System.out.println("Enter the name: ");
        e.setName(scanner.nextLine());
        Date d = null;
        while (d == null) {
            System.out.println("Enter the Date Of Birth: ");
            d = checkDate(scanner.nextLine());
        }
        e.setDateOfBirth(d);
        d = null;
        while (d == null) {
            System.out.println("Enter the Date of Joining: ");
            d = checkDate(scanner.nextLine());
        }
        e.setDateOfJoining(d);

        e.print();

    }
}

Hope this gives you some insight of how it is done. I implore you to compare both source codes, your amd mine and try to understand what has been changed and try to find out why(on your own).

