简体   繁体   中英

I need to change date format from dd-MM-yyyy to yyyy-MM-DD

I want to change the date format before it goes to the database.

I have a calendar. And I am sending date from frontend in dd-MM-yyyy format.

When I send with this format: yyyy-MM-dd everything works well. But I need dd-MM-yyyy

I am using PostgreSQL and I have an attribute to store the date in timestamp with time zone format

For example: my date in DB 2021-06-11 12:58:44.000000 +00:00

My rest controller

  @PutMapping("/edit")
  public void editTime(@RequestBody UserDto userDto) {
   //userDto.getTime() - has this format dd-MM-yyyy
   // I understand that i need to change format here. But i don't know how 
    LocalDate date = LocalDate.parse (userDto.getTime());
    Instant currentTime = date.atStartOfDay(ZoneId.of("UTC")).toInstant();
    userService.update(userDto.getId(),currentTime);
  }

Service method

public interface UserService {
  User update(String Id, Instant time);
}
LocalDate date = LocalDate.parse (userDto.getTime(), DateTimeFormatter.ofPattern("dd-MM-yyyy"));

Hopefully this should work fine if you are passing in this "dd-MM-yyyy" format.

https://help.gooddata.com/cloudconnect/manual/date-and-time-format.html >> this is a good link for Java DateTimeFormatter patterns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM