Dates with LocalDate

0

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);
    
asked by Joaquin Martinez 13.11.2018 в 18:12
source

2 answers

0

You can do it by concatenating the three text variables where you enter the date with that String you can

From Java 1.8, you can achieve this without an additional library by using the java.time classes. See tutorial .

String fechaConcatenada = d +'/'+m+'/'yr;
final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
final LocalDate dt = dtf.parseLocalDate(fechaConcatenada);
    
answered by 13.11.2018 в 21:29
0

The following makes use of the java.time package that already comes in version 8 of Java.

Development

If you wish to receive the complete date and pass it to LocalDate you must first change the nextInt() method to next() in order to extract a text string:

Scanner in = new Scanner(System.in);
String date = in.next();

Then you can create a method that allows you to read the entered text string and verify that it is a valid string with the format you want. You could achieve it in the following way:

public static LocalDate stringToLocalDate(String date) {
    final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("d/M/yyyy");
    LocalDate localDate;
    try {
        localDate = LocalDate.parse(date, dateTimeFormatter);
    } catch (DateTimeParseException e) {
        return null;
    }
    return localDate;
}

In this case, you choose to use the DateTimeFormatter.parse method with the% d/M/yyyy .

Because the method parse(CharSequence text, DateTimeFormatter formatter) can throw an exception of type DateTimeParseException , it is decided to enclose it in a block try / catch to capture the exception and have a value with which to verify if everything went right. In this case if you return an instance of LocalDate it means that you made the conversion correctly and in case of returning null there was an error.

To force a valid date to be entered, you could use the following:

while (stringToLocalDate(dateRequest) == null) {
    dateRequest = in.next();
}
LocalDate date = stringToLocalDate(dateRequest);

Tests

Finally, create a few basic tests so you can see the different results with different inputs.

@Test
public void testLocalDate() {
    assertEquals(LocalDate.parse("2005-09-15"), UtilLocalDate.stringToLocalDate("15/9/2005"));
    assertEquals(LocalDate.parse("1990-10-12"), UtilLocalDate.stringToLocalDate("12/10/1990"));
    assertEquals(LocalDate.parse("1990-01-01"), UtilLocalDate.stringToLocalDate("1/1/1990"));
    assertEquals(LocalDate.parse("1990-01-09"), UtilLocalDate.stringToLocalDate("9/1/1990"));
    assertEquals(LocalDate.parse("1990-01-10"), UtilLocalDate.stringToLocalDate("10/1/1990"));
    assertEquals(LocalDate.parse("1990-02-28"), UtilLocalDate.stringToLocalDate("31/2/1990"));
    assertNull(UtilLocalDate.stringToLocalDate("99/99/9999"));
    assertNull(UtilLocalDate.stringToLocalDate("/2/1991"));
}

The previous tests run successfully.

References

answered by 13.11.2018 в 22:37