I am trying to convert a string to date using java 8 to a certain format. Below is my code. Even after mentioning the format pattern as MM/dd/yyyy the output I am receiving is yyyy/DD/MM format. Can somebody point out what I am doing wrong?
String str = "01/01/2015";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate dateTime = LocalDate.parse(str, formatter);
System.out.println(dateTime);
LocalDate
doesn't have a format of it's own (it'stoString
method is simply providing useful information about the value it represents), you need to use a different formatter to format theLocalDate
back to aString
in the format you want. Conceptually similar to this – FluecureLocalDate#toString
generates, when it's time to present the value, then you format it, otherwise, you simply shouldn't care, it's just a container class which represents some point in time – FluecureDate
? – Spenserian