javajsr310

How to format LocalDate to string?


I have a LocalDate variable called date, when I print it displays 1988-05-05 I need to convert this to be printed as 05.May 1988. How to do this?


Solution

  • SimpleDateFormat will not work if he is starting with LocalDate which is new in Java 8. From what I can see, you will have to use DateTimeFormatter, http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html.

    LocalDate localDate = LocalDate.now();//For reference
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy");
    String formattedString = localDate.format(formatter);
    

    That should print 05 May 1988. To get the period after the day and before the month, you might have to use "dd'.LLLL yyyy"