简体   繁体   中英

Converting string to date format (yyyy/MM/dd) in JAVA

I am facing an issue when I convert my string to date format.

This is what I have tried -

First try:

        String completionDate1 = request.getParameter("completion_date");
        System.out.println(completionDate1); // O/P --> 21/10/2016 (Correct)
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        date = df.parse(completionDate1);
        System.out.println(date); // O/P --> Tue Apr 08 00:00:00 IST 27 (Inorrect)

Second try:

        String completionDate1 = request.getParameter("completion_date");
        System.out.println(completionDate1); // O/P --> 21/10/2016 (Correct)
        Date completionDate = new SimpleDateFormat("yyyy/MM/dd").parse(completionDate1);
        System.out.println(completionDate); // O/P --> Tue Apr 08 00:00:00 IST 27 (Inorrect)

The output I'm expecting is that, the completionDate should be Date format not String . I'm just printing the completionDate just to make sure the date format is getting getting converted.

The main intention of getting it in date format is I need this for an Update query so it has to be Date and not String.

Kindly let me know where I'm going wrong. I need this date format as I need to store this date in a database.

Thanks

You have to format the date after parsing it but your parsing format is also incorrect so its giving wrong output. Try

 String completionDate1 = "21/10/2016";
 System.out.println(completionDate1);
 DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
 Date date = new Date();
 date = df.parse(completionDate1);
 DateFormat df1 = new SimpleDateFormat("yyyy/MM/dd");
 System.out.println(df1.format(date));

DEMO

Wrong pattern

First problem is that your defined parsing pattern does not match your input string. For 21/10/2016 pattern should be dd/MM/yyyy .

java.sql.Date versus java.util.Date

Second problem is that for database access you should be using the java.sql.Date class for a date-only value without time-of-day and without time zone. Not to be confused with java.util.Date which is a date and time-of-day value. You should specify the fully-qualified class name in such discussions as this Question.

Neither of these classes have a “format”. They have their own internal representation, a count from epoch .

java.time

Third problem is that you are using old outdated classes. In Java 8 and later, use java.time framework now built-in. In Java 6 & 7, use its back-port, the ThreeTen-Backport project. For Android, use the adaptation of that back-port, ThreeTenABP.

In the java.time classes we now have the LocalDate class for date-only values.

String input = "21/10/2016"
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "dd/MM/yyyy" );
LocalDate localDate = LocalDate.parse( input , formatter );

Hopefully some day JDBC drivers will be updated to directly use the java.time types. Until then, use the new methods added to the old java.sql types to convert.

java.sql.Date sqlDate = java.sql.Date.valueOf( localDate );

Now pass that java.sql.Date object to the setDate method on a PreparedStatement object for transmission to a database.

Note that at no time did we make use of any more strings. We went from original input String to java.time.Date to java.sql.Date .

At no point did we use java.util.Date . Check your import statements to eliminate java.util.Date .

By the way, to go the other direction from java.sql.Date to LocalDate :

LocalDate localDate = mySqlDate.toLocalDate();

You can try this

DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
String completionDate = df.format(request.getParameter("completion_date"));
System.out.println(completionDate);

Use below code

 String completionDate1 = request.getParameter("completion_date");
 System.out.println(completionDate1); // O/P --> 21/10/2016 (Correct)
 Date completionDate = new SimpleDateFormat("dd/MM/yyyy").parse(completionDate1);

 String date = new SimpleDateFormat("yyyy/MM/dd").format(completionDate);
 System.out.println(date);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM