Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4802

Get LocalDate from the @RequestBody, convert it to dd/mm/yyyy format, convert it back to LocalDate and store it in the database

$
0
0

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


Viewing all articles
Browse latest Browse all 4802

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>