I need to enter a date using the command console with this format: dd/mm/aa
eg: 15/9/2005
The things I can use are the library Scanner
Y LocalDate
How do I put the date in the cmd in this style: 15/9/2005
then read it with scanner
using .nextLine()
and then pass it to LocalDate
, I am currently entering first the day, I give ENTER
, then the month, I give ENTER
and then the year, I read that with .nextInt()
and then I put those variables in LocalDate nacimiento = LocalDate.of(yr,m,d);
but I want to put the whole date in a row without needing to give more ENTER's
in the console
I'm currently doing this:
int d;//dia
int m;//mes
int yr;//anio
System.out.println("");
System.out.println("Ingrese la fecha de nacimiento");
System.out.println("");
System.out.println("Ingrese el dia");
d = in.nextInt();
while( d<1 || d>31){//evita que el usuario ingrese una fecha no valida
System.out.println("Por favor ingrese una fecha valida");
d = in.nextInt();
System.out.println("");
}
System.out.println("");
System.out.println("Ingrese el mes");
m = in.nextInt();
while( m<1 || m>12){//evita que el usuario ingrese una fecha no valida
System.out.println("Por favor ingrese una fecha valida");
m = in.nextInt();
System.out.println("");
}
System.out.println("");
System.out.println("Ingrese el anio");
yr = in.nextInt();
System.out.println("");
LocalDate nacimiento = LocalDate.of(yr,m,d);