How to convert date and time object 12 hours format?

246 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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 of LocalDateTime, you get what its toString 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 of LocalDateTime.

Given below is a simple demo:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        String strDateTime = "2020-12-10T16:52:42.014982500";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/uuuu hh.mm a", Locale.ENGLISH);
        LocalDateTime ldt = LocalDateTime.parse(strDateTime);
        System.out.println(ldt);// Prints the value of ldt#toString

        // Get a formatted string
        String formatted = ldt.format(dtf);
        System.out.println(formatted);
    }
}

Output:

2020-12-10T16:52:42.014982500
10/12/2020 04.52 PM

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 be OffsetDateTime.

Change the annotation as

public ResponseEntity<Meeting> create(@RequestParam(name = "start") @DateTimeFormat(pattern = DateTimeFormatter.ISO_ZONED_DATE_TIME) OffsetDateTime start)

or

public ResponseEntity<Meeting> create(@RequestParam(name = "start") @DateTimeFormat(iso = ISO.DATE_TIME) OffsetDateTime start)

Check the Spring documentation page to learn more about the second option.

A quick demo:

import java.time.OffsetDateTime;

class Main {
    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("2020-12-08T21:34:18.119+00:00");
        System.out.println(odt);
    }
}

Output:

2020-12-08T21:34:18.119Z