Converting string to date using java8
Asked Answered
I

3

18

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);
Infirmary answered 27/2, 2016 at 3:55 Comment(9)
You also need a formatter for your output, assuming you expect that in a specific format.Spenserian
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 thisFluecure
@Fluecure But my method which is calling this function has a return type of Date.Infirmary
@Elliott It means the formatter will convert this date to string again?Infirmary
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 timeFluecure
@Infirmary Are you getting the correct Date?Spenserian
Yes my date is correct, it's just the wrong format.Infirmary
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.Infirmary
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.Tarr
G
12

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
Govan answered 27/2, 2016 at 4:31 Comment(0)
C
5

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);
Contention answered 27/2, 2016 at 9:45 Comment(0)
B
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.

Blowup answered 27/2, 2016 at 4:14 Comment(2)
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.Infirmary
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.Blowup

© 2022 - 2024 — McMap. All rights reserved.