Search code examples
javadatejava-17

Print date from input ID user in java


I have to make a code that take an user name input and then an ID number that is based format as ( 1102199344556699 - where the first 8 characters is his day of birth). My code:

import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {

        //user input
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your name: ");
        int length = scanner.nextLine()
                .length();
        System.out.println(length);

        //user id input
        Scanner scanner2 = new Scanner(System.in);
        System.out.println("Enter your id number");
        String idNUmber = scanner2.nextLine();
        if (idNUmber.length() < 11) {
            throw new InvlaidIdException("invalid");
        } else {
        String dateOfBirth = idNUmber.substring(0, 8);

    //found this way,on the output is giving me from input -110219935656525
   //output is ok but as you can see on My output i get some extra things (please see OUTPUT)
        TemporalAccessor date = DateTimeFormatter.ofPattern("ddMMyyyy").withLocale(Locale.FRANCE).parse(dateOfBirth);
            System.out.println(date);
                }
            }
        }

INPUT user Id -110219935656525

OUTPUT:

This is  the birthday date: {},ISO resolved to 1993-02-11

How can i get rid of {},ISO resolved to ? Thanks,


Solution

  • After some more resarches I got the output i needed :)

    Here is the full code:

    import java.util.Scanner;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
    
            //user input
            Scanner scanner = new Scanner(System.in);
            System.out.println("Enter your name: ");
            int length = scanner.nextLine()
                    .length();
            System.out.println(length);
    
            //id user input
            Scanner input = new Scanner(System.in);
            String civilIdStr = "";
            System.out.println("Enter your id number");
            civilIdStr = input.nextLine();
            if (civilIdStr.length() < 11) {
                throw new InvlaidIdException("invalid");
            } else {
    // sout the date from the Id user input  (format DD-MM-YY)
                    String dob = civilIdStr.substring(0, 8);
                    System.out.println("Date of birth from user ID: " +
                            dob.substring(0, 2) + "/" +   //DATE
                            dob.substring(2, 4) + "/" +   //MONTH
                            dob.substring(6, 8));         //YEAR
                }
            }
        }
    

    OUTPUT:

    Enter your name: 
    Victor
    6
    Enter your id number
    11021993554644
    Date of birth from user ID: 11/02/93