13

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);
user3509208
  • 473
  • 1
  • 5
  • 14
  • 1
    You also need a formatter for your output, assuming you expect that in a specific format. – Elliott Frisch Feb 27 '16 at 03:58
  • 1
    `LocalDate` doesn't have a format of it's own (it's `toString` method is simply providing useful information about the value it represents), you need to use a different formatter to format the `LocalDate` back to a `String` in the format you want. Conceptually similar to [this](http://stackoverflow.com/questions/12575990/calendar-date-to-yyyy-mm-dd-format-in-java/12576219#12576219) – MadProgrammer Feb 27 '16 at 03:59
  • @MadProgrammer But my method which is calling this function has a return type of Date. – user3509208 Feb 27 '16 at 04:01
  • @Elliott It means the formatter will convert this date to string again? – user3509208 Feb 27 '16 at 04:02
  • So? You shouldn't care what format `LocalDate#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 – MadProgrammer Feb 27 '16 at 04:02
  • @user3509208 Are you getting the correct `Date`? – Elliott Frisch Feb 27 '16 at 04:07
  • Yes my date is correct, it's just the wrong format. – user3509208 Feb 27 '16 at 04:08
  • I am getting a String input(10/10/2016) from a form and trying to convert it into a date to store it in my database table as my database accepts data as Datetime. that is why I need to convert my string to date. – user3509208 Feb 27 '16 at 04:14
  • It's 2018 and I still don't get it how to easily parse a simple date as 'yyyy-MM-dd' to a java.util.Date object. – CleitonCardoso Aug 14 '18 at 22:16

3 Answers3

8

That is because you are using the toString method which states that:

The output will be in the ISO-8601 format uuuu-MM-dd.

The DateTimeFormatter that you passed to LocalDate.parse is used just to create a LocalDate, but it is not "attached" to the created instance. You will need to use LocalDate.format method like this:

String str = "01/01/2015";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate dateTime = LocalDate.parse(str, formatter);
System.out.println(dateTime.format(formatter)); // not using toString
marcospereira
  • 11,462
  • 3
  • 42
  • 49
3

LocalDate is a Date Object. It's not a String object so the format in which it will show the date output string will be dependent on toString implementation.

You have converted it correctly to LocalDate object but if you want to show the date object in a particular string format, you need to format it accordingly:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
System.out.println(dateTime.format(formatter))

This way you can convert date to any string format you want by providing formatter.

Ankit Bansal
  • 4,521
  • 20
  • 39
  • Would you recommend using something else instead of LocalDate, from Java 8 to meet my requirements as I need to convert string to date in format mm/dd/yyyy and store in my DB. – user3509208 Feb 27 '16 at 04:21
  • nope..your code is correct to store it in db. Most of the DBs have support for date. They will store it internally in milliseconds. When you make a query they are showing you in specific format. It doesn't mean date is stored in that format. – Ankit Bansal Feb 27 '16 at 04:24
3

You can use SimpleDateFormat class for that purposes. Initialize SimpleDateFormat object with date format that you want as a parameter.

String dateInString = "27/02/2016"
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(dateInString);
Zoka
  • 291
  • 2
  • 8