-1

I have a simple Java LocalDateTime like below:

    DateTimeFormatter theDtFrmtr = DateTimeFormatter.ofPattern("M/d/yy h:mm a");
    String strDate = "3/23/21 1:17 PM";
    LocalDateTime localDate = LocalDateTime.parse(strDate, theDtFrmtr);
    System.out.println("localDate - "+ localDate);

It results in: localDate - 2021-03-23T13:17

I am expecting it to be is - localDate - 2021-03-23 1:17 pm

What am I missing here?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46
  • 1
    When you print a date, its `toString()` is called, and that prints hours in 24h format. The only way to get the output in the format you want is to use that formatter. So print with `System.out.println("localDate - "+ theDtFrmtr.format(localDate));`. A `LocalDateTime` object does not carry intrinsic formats. – ernest_k Jul 07 '21 at 18:08
  • Does this answer your question? [Can’t rid of 'T' in LocalDateTime](https://stackoverflow.com/questions/52311884/can-t-rid-of-t-in-localdatetime). [This](https://stackoverflow.com/questions/54941872/parsing-timestamp-as-localdatetime) or [this](https://stackoverflow.com/questions/61203388/cannot-parse-threeten-datetime-to-a-specific-format)? – Ole V.V. Jul 08 '21 at 18:25
  • No Ole. It was just the issued about printing the date. – Ajay Kumar Jul 08 '21 at 19:50

1 Answers1

1

From the JAVA Oracle docs,

  • static LocalDateTime parse(CharSequence text) Obtains an instance of LocalDateTime from a text string such as 2007-12-03T10:15:30.

  • static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) Obtains an instance of LocalDateTime from a text string using a specific formatter.

DateTimeFormatter object needs to be formatted.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69