3

I made a scanner which asks for 3 integer : month / day / year of birth For every integer, i check if this is a valid entry (i.e month <12 & month >0). If three entrees are valid i do a :

System.out.println(month + "-" + day + "-" + year);

full code following :

// TODO code application logic here
    Scanner sc = new Scanner(System.in);
    int intJour;
    int intMois;
    int intAnnee;

    do {
        System.out.println("Year of birth :");
        intJour = sc.nextInt();
        sc.nextLine();
    } while (intJour < 0 || intJour > 31);

    do {
        System.out.println("Month of birth :");
        intMois = sc.nextInt();
        sc.nextLine();
    } while (intMois < 0 || intMois > 12);

    do {
        System.out.println("Year of birth :");
        intAnnee = sc.nextInt();
        sc.nextLine();
    } while (intAnnee < 0 || intAnnee > Calendar.getInstance().get(Calendar.YEAR));

    System.out.println("Birth date: " + intJour + "-" + intMois + "-" + intAnnee);

I would like to make a date out of the user inputs, and check if that date existed. I.E not every month has a 31 regarding of the year involved.

Aod Ren
  • 681
  • 1
  • 8
  • 17
  • 1
    use the `if(condition)` clause for putting all the conditions. Also, it looks like a homework problem. – YoungHobbit Jan 11 '16 at 10:08
  • conditions are working. The user cannot input 13/32/2017 for instance. But sometimes conditions are met : 31/02/2015 But the date didn't exist because it was a bisextile (its an exemple). – Aod Ren Jan 11 '16 at 10:10
  • use the `else` for taking action, when a `if` condition is not true. – YoungHobbit Jan 11 '16 at 10:12
  • I need to refer to a calendar and that's what i struggle to do. My question may not be accurate enough. – Aod Ren Jan 11 '16 at 10:14
  • What is this `calendar` you are referring ? – YoungHobbit Jan 11 '16 at 10:14
  • Use multiple conditions together, like: if(month == 2 && day <29) and so on. – Igorovics Jan 11 '16 at 10:15
  • Do you want to check each unit (month, day, year) for correctness i.e. if the year has max. 365 days or do you want to check if the entered date is valid? – hamena314 Jan 11 '16 at 10:16
  • 1
    i edited cause i think my question is not clear. It's not the validation of each condition that is a problem. I would like to make a date out of the user inputs, and check if that date existed. I.E not every month has a 31 regarding of the year involved. – Aod Ren Jan 11 '16 at 10:24

2 Answers2

3

Using statements to check if a certain date is valid can be quite confusing. Therefore you should use standardized methods.

If you are using Java 8, then you could directly use this:

public static boolean isDateValid(int year, int month, int day) {
    boolean dateIsValid = true;
    try {
        LocalDate.of(year, month, day);
    } catch (DateTimeException e) {
        dateIsValid = false;
    }
    return dateIsValid;
}

Just input your intAnnee into year, intMois into month and intJour into day.

If you are on an earlier version of Java try this:

final static String DATE_FORMAT = "dd-MM-yyyy";

public static boolean isDateValid(String date) 
{
        try {
            DateFormat df = new SimpleDateFormat(DATE_FORMAT);
            df.setLenient(false);
            df.parse(date);
            return true;
        } catch (ParseException e) {
            return false;
        }
}
hamena314
  • 2,969
  • 5
  • 30
  • 57
  • The post is about taking date, month and year separately and validating. – YoungHobbit Jan 11 '16 at 10:13
  • I think you might be right, I focused on the "how can i check if the date is valid"-part. Maybe he did not word his question right? – hamena314 Jan 11 '16 at 10:15
  • no u got it right, i want to check if the date existed. For exemple the user could not input the 31 of Fabruary during a bisextile year. – Aod Ren Jan 11 '16 at 10:16
  • The only thing needed to change on this answer to be right is the parameter: it should accept 3 integer (day, month, year), and then they should be concatenated into a String. After that the parsing would do the rest. – Igorovics Jan 11 '16 at 10:18
  • @Igorovics That is right. If someone wants to use `SimpleDateFormat` parsing then why to take three `int`s. I guess he need to take three inputs and verify using `if-else` conditions. – YoungHobbit Jan 11 '16 at 10:22
  • at least put the link..... http://stackoverflow.com/questions/4528047/checking-the-validity-of-a-date – Jordi Castilla Jan 11 '16 at 10:36
3

If you want to do it in this way, you must reverse the order of your questions: ask first year, after month and finally the day. In this way you will be able to know prior to the user input if all possible conditions are met (i.e: day 29 of February for leap years or months with 31 days).

Also, to check finally if the date is correct, use, for example, a SimpleDateFormat.

final static String DATE_FORMAT = "dd-MM-yyyy";

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int intJour;
    int intMois;
    int intAnnee;

    do {
        System.out.println("Veuillez entrer votre année de naissance :");
        intAnnee = sc.nextInt();
        sc.nextLine();
    } while (intAnnee < 0 || intAnnee > Calendar.getInstance().get(Calendar.YEAR));

    do {
        System.out.println("Veuillez entrer votre mois de naissance (de 01 à 12) :");
        intMois = sc.nextInt();
        sc.nextLine();
    } while (intMois < 0 || intMois > 12);

    do {
        System.out.println("Veuillez entrer votre jour de naissance (de 01 à 31) :");
        intJour = sc.nextInt();
        sc.nextLine();
    } while (!isDateValid(intAnnee, intMois, intJour));

    System.out.println("Confirmation de votre année de naissance: " + intJour + "-" + intMois + "-" + intAnnee);
    sc.close();
}

public static boolean isDateValid(int year, int month, int day) 
{
        try {
            DateFormat df = new SimpleDateFormat(DATE_FORMAT);
            df.setLenient(false);
            df.parse(day + "-" + month + "-" + year);
            return true;
        } catch (ParseException e) {
            return false;
        }
}

OUTPUT:

Veuillez entrer votre année de naissance :
2012
Veuillez entrer votre mois de naissance (de 01 à 12) :
30
Veuillez entrer votre mois de naissance (de 01 à 12) :
02
Veuillez entrer votre jour de naissance (de 01 à 31) :
30
Veuillez entrer votre jour de naissance (de 01 à 31) :
30
Veuillez entrer votre jour de naissance (de 01 à 31) :
30
Veuillez entrer votre jour de naissance (de 01 à 31) :
10
Confirmation de votre année de naissance: 10-2-2012
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109