I am creating a Rest API applicaton which tracks users` expenses. I have date variable of type LocalDate and the default format is yyyy-mm-dd but I want to store dd/mm/yyyy in the database
what I tried was get the localDate from RequestBody, format it to dd/mm/yyyy (which returns string) and convert it back to LocalDate. However, date is still being stored default format, yyyy-mm-dd. I think the problem is in LocalDate.parse(dateString, formatter); because it converts back to the default format or ignores the new format. Not sure but this my assumptionHere is my code:
@PostMapping() public ResponseEntity<Expense> addExpense(@RequestBody @Valid Expense expense) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.UK); LocalDate date = expense.getDate(); String dateString = date.format(formatter); LocalDate formattedDate = LocalDate.parse(dateString, formatter); expense.setDate(formattedDate); I dont know if there a way to take input as dd/mm/yyyy format in the request instead of yyyy-mm-dd