I get date and time from client in the controller, here the signature of the function:
public ResponseEntity<Meeting> create(@RequestParam(name = "start") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start)
if I print the start variable it shows date and time in 24-hour format:
2020-12-10T16:52:42.014982500
But I want the value of the LocalDateTime variable will be in 12 hours format, so I tried to change DateTimeFromat in the function's signature
public ResponseEntity<Meeting> create(@RequestParam(name = "start") @DateTimeFormat(DateTimeFormatter.ofPattern("dd/MM/yyyy hh.mm aa")) LocalDateTime start).3
But on this row:
DateTimeFormatter.ofPattern("dd/MM/yyyy hh.mm aa")
I get error:
Attribute value must be constant
My question is what params @DateTimeFormat have to get so it can convert date and time from client to 12 hours format?
A date-time object does not store the formatting information. A
LocalDateTime
is supposed to store just the date and time components (i.e. year, month, day of the month, hour, minute, second and the fraction of second). When you print an object ofLocalDateTime
, you get what itstoString
function returns. If you need the value in a different format, you need to get a formatted string out of this object but in no case, you will be able to store the format into the instance ofLocalDateTime
.Given below is a simple demo:
Output:
Update
From your comment on the other answer (which has been deleted now), I learnt that your date-time string is
2020-12-08T21:34:18.119+00:00
which has a zone offset information (+00:00
) and therefore, the most appropriate type would beOffsetDateTime
.Change the annotation as
or
Check the Spring documentation page to learn more about the second option.
A quick demo:
Output: