Turn String into Localdatetime

Asked

Viewed 1,482 times

3

I created this Edit Fields button, to change the data of these users I created, the name, name and document are working, without the date of birth is updated, but when I try to edit the Date of Birth, comes a bad request, because the form expects a LocalDateTime, how do I turn this string into LocalDateTime?

Campo Data Nascimento

Bad request

EDIT:

The Get of the Date of Birth.

inserir a descrição da imagem aqui

EDIT 2: This could be so I’m just manipulating on the front

    editarAdicional(item: any) {

    this.edicaoAdicionais.idPessoa = item.idPessoa;
    this.edicaoAdicionais.nomeCompleto = item.nomeCompleto;
    this.edicaoAdicionais.nomeEmbossado = item.nomeEmbossado;
    this.edicaoAdicionais.dataNascimento = this.formataDatas(item.dataNascimento);
    this.edicaoAdicionais.documento = item.documento;

    this.editandoAdicionais = true;
}

atualizarAdicional(item: any) {
    let formatador = new Formatadores();

    item.idPessoa = this.edicaoAdicionais.idPessoa;
    item.nomeCompleto = this.edicaoAdicionais.nomeCompleto;
    item.nomeEmbossado = this.edicaoAdicionais.nomeEmbossado;
    let dataNascimento = formatador.parseDate(this.edicaoAdicionais.dataNascimento);
    item.dataNascimento = dataNascimento.toISOString();
    item.documento = this.edicaoAdicionais.documento;

    this.editandoAdicionais = false;
  • "25/07/1981" is what you send to Java backend and there he tries to turn into LocalDateTime? And the get that returns "1981-07-25T00:00:00" is what it returns when there already is a date there?

  • "25/07/1981" is the formatted date of get, I need to send it as String and transform as Localdatetime, I may have made a mistake about it, because I don’t quite understand how to manipulate dates.

1 answer

2


A LocalDateTime needs the date and time to be created, but the String that you send ("25/07/1981") only has the date.

An option to solve this in Java code would be to do the Parsing of this date and put any time. For this we use a java.time.format.DateTimeFormatter:

String s = "25/07/1981";
DateTimeFormatter parser = DateTimeFormatter.ofPattern("dd/MM/uuuu");
// faz o parsing e setar o horário para meia-noite
LocalDateTime dateTime = LocalDate.parse(s, parser).atStartOfDay();
System.out.println(dateTime); // 1981-07-25T00:00

First I create a LocalDate (since the String original only has day, month and year) and then I use the method atStartOfDay(), that returns a LocalDateTime with the set time for midnight. You can also use atTime(10, 30) to set the time to 10:30 in the morning, for example. Use whatever is best for your case.

Another option is to use a java.time.format.DateTimeFormatterBuilder, along with a java.time.temporal.ChronoField to set the time value to midnight. With this, it is no longer necessary to create the LocalDate, because we can create the LocalDateTime directly:

String s = "25/07/1981";
DateTimeFormatter parser = new DateTimeFormatterBuilder()
    // dia/mês/ano
    .appendPattern("dd/MM/uuuu")
    // valor default para o horário (zero para meia-noite)
    .parseDefaulting(ChronoField.HOUR_OF_DAY, 0).toFormatter();
LocalDateTime dateTime = LocalDateTime.parse(s, parser);
System.out.println(dateTime); // 1981-07-25T00:00

Setting the time to zero, the time of LocalDateTime will be midnight (minutes, seconds and fractions of seconds can be omitted as they will be automatically set to zero).


With this you do not change the type of the field nor the code of the frontend. Although I think better - if possible - use only LocalDate in the backend, because you are only keeping the date of birth (day, month and year), and the time does not seem to matter in this case. Of course this will require a change in the frontend, because the date returned will be without the time (it will only be 1981-07-25).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.