0

Trying to find an out of the box solution to format the java LocalDateTime in chronological pattern available in Java 8 APIs.

class MyDateFormatter {
  public static void main(String[] args) {
    
    ZonedDateTime cetTime= LocalDateTime.parse(dateInput, DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"))
            .atZone(ZoneId.of("CET"));
    System.out.println(cetTime); // Prints 2008-11-01T12:24:01+01:00[CET]

    String estTime = cetTime.withZoneSameInstant(ZoneId.of("US/Eastern")).format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL));

    System.out.println(estTime) // Prints Saturday, November 1, 2008 7:24:01 AM EDT

  }
}

The estTime seems not to fit my expected pattern. I am looking for an output with ordinal on the date.

Actual   : Saturday, November 1, 2008 7:24:01 AM EDT
Expected : Saturday, November 1st, 2008 7:24:01 AM

Could someone please help if this possible with java 8 APIs. My question is specific to Java8 API support for this

technoJ
  • 175
  • 1
  • 1
  • 16
  • There is no "out of the box solution" for that (ordinal date). – Andreas Oct 06 '20 at 14:07
  • Why are you asking about `LocalDateTime`, but then show code using `ZonedDateTime`? If you don't want the `EDT`, use `LocalDateTime`, not `ZonedDateTime`. – Andreas Oct 06 '20 at 14:09
  • There is no direct way in JDK to format dates with the day ordinal information – MarkAddison Oct 06 '20 at 14:09
  • 2
    A quick search finds a related question asked in the past [https://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st-or-23rd-ordinal](https://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st-or-23rd-ordinal) – MarkAddison Oct 06 '20 at 14:11
  • I did see that, but it was referring to SimpleDateFormatter. My question was more on java8 API support for ordinals – technoJ Oct 06 '20 at 14:20
  • 1
    @technoJ While that question *asks* about the long obsolete `SimpleDateFormat` class, three of the answers are using java.time, the modern Java date and time API since Java 8: [my answer](https://stackoverflow.com/a/50369812/5772882), [this one](https://stackoverflow.com/a/53202905/5772882) and [this one](https://stackoverflow.com/a/53740517/5772882). – Ole V.V. Oct 06 '20 at 15:06
  • And after I wrote my comment (I think) WJS has added [a Java 14 answer](https://stackoverflow.com/a/64229359/5772882). Don’t expect it to get much more modern any time soon. :-) – Ole V.V. Oct 07 '20 at 20:48

0 Answers0