Manipulating dates

Asked

Viewed 207 times

0

I have doubts about the steps to manipulate dates.

The steps below are ok? Can I start studies?

  • I have the attribute Date and I want to insert 2 days.
  • Convert Date in String.
  • Convert String in Calendar.
  • I make the code to manipulate the obtained date.
  • Convert of Calendar for String.
  • Convert of String for Date.
  • Save.

because if I try to declare as Calendar when saved from that mistake?

Unresolved Compilation problem: Type Mismatch: cannot Convert from

Code for more details:

// data em que o dinheiro foi entregue
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @Temporal(TemporalType.DATE)
    private Date dataEmprestimo;

if I put private Calendar dataEmprestimo, gives the error mentioned above.

  • Can you add the full stacktrace? Because this code is not making much sense with the error.

  • I’m sorry if I can’t express myself well. but I’ll try to explain here.

  • When you try to run, do you pop a stack of errors? If yes, add it to the question, it is easier to locate the source.

  • But why are you changing the type to Calendar? The annotation above indicates that it is Date, not Calendar.

  • I have two doubts. 1º if the step by step to manipulate Date is correct. 2º because if I insert Calendar instead of Date error: Type Mismatch: cannot Convert from Calendar to Date] with root cause java.lang.Error: Unresolved Compilation problem: Type Mismatch: cannot Convert from Calendar to Date

  • if I remove @Temporal(Temporaltype.DATE) from right? by to Calendar?

  • Gives error because they are different types, and your annotation before the field says it is a Date field.

  • I removed the note and it worked, a light started to appear here.... If it is not too much to ask, studying he asked to put the above note, why did he ask it? removing it implies that?

Show 3 more comments

1 answer

1


Using the Java 8 API makes it easier to handle dates

import java.time.LocalDate;
LocalDate dataInicio;
@JsonFormat(pattern = "yyyy-MM-dd")
LocalDate data;

In your object only retrieve the Localdate field and run

 objeto.getData().plusDays(1) 

//Adds a day

  • I started the study with Localdate and the date being saved aced00057372000d6a6176612e74696d652e536572955d84ba1b2248b20c00007870770703000007e1060278. 
se eu incluir @jsonFormat da erro Field error in object 'emprestimo' on field 'primeiraEmprestimo': rejected value [09/06/2017]; codes [typeMismatch.emprestimo.primeiraEmprestimo,typeMismatch.primeiraEmprestimo,typeMismatch.java.time.LocalDate,typeMismatch]; arguments [org.springframework.context.support.Defaultme

  • is because you are using the dd/mm/yyyy format, only change the format so that it is equal or change your date so that it has the format I declare

Browser other questions tagged

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