0
String date = "1992-07-25";

LocalDate dt = LocalDate.parse(date);

System.out.println(dt) // 1992-07-25 ( output )

DateTimeFormatter f =  DateTimeFormatter.ofPattern("d/MM/yyyy");

String s1 = f.format(dt); 

System.out.println(s1) // 25/07/1992 ( output in string format )

I want this (String) s1 to convert into LocalDate of pattern (LocalDate - dd/MM/yyy)

Output - 25/07/1992 should be LocalDate Type, not String

Lucas Campos
  • 1,860
  • 11
  • 17
  • 1
    Recommend you to check Basil Bourque's answer in https://stackoverflow.com/questions/35913647/format-a-date-and-returning-a-date-not-a-string – sanjeevRm Jun 27 '21 at 04:50
  • Your Question makes no sense. You successfully parsed input text of standard ISO 8601 format to produce a `LocalDate` object. You then defined and used a formatter of a custom format to produce another string representing the value contained within that `LocalDate` object. All went well. What is your problem/question? – Basil Bourque Jun 27 '21 at 08:20
  • Duplicate or repost of: [Change string to LocalDate with specific format in JAVA](https://stackoverflow.com/q/68148621/642706) – Basil Bourque Jun 27 '21 at 08:29

1 Answers1

0

When you are printing LocalDate instance, it is actually calling toString() method of LocalDate class. And according to javadoc 11, this method

Outputs this date as a String, such as 2007-12-03.
The output will be in the ISO-8601 format uuuu-MM-dd.

So you are seeing, it will always print in uuuu-MM-dd format.

Think LocalDate as a DATE just, from where you can get the info of that particular date. When you are printing LocalDate itself, it has no meaning actually. If you need to show the date in any particular format, convert it into String, which you can do already.

Andreas
  • 154,647
  • 11
  • 152
  • 247
Mukit09
  • 2,956
  • 3
  • 24
  • 44