I am trying to build a program that asks a user for their DOB and will throw an error if not in the DD/MM/YYYY format. Does anyone know how to make that happen? I apologize if I didn't do enough research but I couldn't really find anything about formatting the input for a date. Thank you for your help.
            Asked
            
        
        
            Active
            
        
            Viewed 424 times
        
    -2
            
            
        - 
                    Possible duplicate of [Java: Check the date format of current string is according to required format or not](https://stackoverflow.com/questions/20231539/java-check-the-date-format-of-current-string-is-according-to-required-format-or). Getting a String from the user is common enough that you can find info on how to do that as well. – takendarkk Mar 12 '19 at 16:05
- 
                    Parse the string as a date and catch the parse exception. – Andreas Mar 12 '19 at 16:05
- 
                    "and will throw an error if not in the DD/MM/YYYY format" in that case simply accept user data as string, then check if it matches `dd/dd/dddd` where `d` is digit. You are use regex for that, or if you want to check if not only format is correct but also content you can try parsing this string into actual date. – Pshemo Mar 12 '19 at 16:05
- 
                    1*"I couldn't really find anything about formatting the input for a date"* That is because you don't *format* the input, you *parse* it. Try searching for that instead. – Andreas Mar 12 '19 at 16:07
- 
                    @takendarkk Yes getting a string is easy to do, what I am trying to get is a string formatted a specific way. It doesn't appear that anything in that link addresses that. – Tyler1621 Mar 12 '19 at 16:29
- 
                    @Andreas I might have used the wrong language when asking the question. My apologies. I will look under parsing the string instead. – Tyler1621 Mar 12 '19 at 16:31
3 Answers
-1
            
            
        boolean isDOB(String input) {
        String[] inputs = input.split("/");
        if(inputs.length!=3)
            return false;
        int days = Integer.parseInt(inputs[0]);
        int month = Integer.parseInt(inputs[1]);        
        if(days>31 || month>12 || inputs[2].length()!=4) {
            return false;           
        }
        return true;        
    }
 
    
    
        Mahdi Khardani
        
- 52
- 1
- 10
-1
            You need to process the input after it's read.  Regular expressions are very efficient in this task.  ^\\d{2}/\\d{2}/\\d{4}$ will check that the user supplied string is in DD/MM/YYYY.
So, you could compare the user input as follows:
if(userStr.matches("^\\d{2}/\\d{2}/\\d{4}$")){
    //Do stuff if input is good
}else{
    //Do stuff if input is bad
}
 
    
    
        corporateWhore
        
- 617
- 2
- 12
- 23
-1
            
            
        Here is the entire program if this helps you. This also validates the date using DateTimeFormatter. So, if user enters 32/01/2000, it fails.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Scanner;
public class DateOfBirth {
    public static void main(String[] args) {
        System.out.println("Please enter your DOB");
        Scanner myScanner = new Scanner(System.in);
        String input = myScanner.next();
        while (!validDate(input)) {
            System.out.println("ERROR: Invalid date. Please enter your DOB");
            input = myScanner.next();
        }
        myScanner.close();
    }
    private static boolean validDate(String input) {
        try {
            DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy");
            LocalDate.parse(input, inputFormat);
        } catch (DateTimeParseException e) {
            return false;
        }
        return true;
    }
}
 
    
    
        Prasann
        
- 1,263
- 2
- 11
- 18
- 
                    Down voter - can you tell me why you have down voted this? Do you see any issue with this solution? – Prasann Mar 13 '19 at 12:11
