0

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

Shashi Kiran
  • 362
  • 2
  • 9
  • 27
  • 1
    you print a `Date` object. What it does print is determined by the `Date#toString` method and not by the format you created previously. If you want to print it formated reuse the format to get the propper format in `String` representation. – SomeJavaGuy Apr 04 '16 at 09:38
  • @KevinEsche This is the exception I get when I try to update my table using that date - Data truncation: Incorrect date value: ' Tue Apr 08 00:00:00 IST 27 ' for column 'completion_date' at row 1 – Shashi Kiran Apr 04 '16 at 09:45
  • @zombie It's not a duplicate, that is converting current date to string, my question is String to Date and change in format as well. – Shashi Kiran Apr 04 '16 at 10:00
  • @Shashi Kiran The issue is same as you are also facing problem while converting Date to String. – Prabhat Apr 04 '16 at 10:17
  • @ShashiKiran A `Date` object does not have a format, it's just a number of millis since the epoch. Its `toString` method has a user-friendly output but you can't change it. – assylias Apr 04 '16 at 10:21

4 Answers4

4

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

singhakash
  • 7,891
  • 6
  • 31
  • 65
  • I need the completionDate to be in date format, so should I parse it again ? Is this the right way? – Shashi Kiran Apr 04 '16 at 10:01
  • Yes you have to parse String date to Date date to format it because [`DateFormat#format()`](https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html) takes Date type as argument – singhakash Apr 04 '16 at 10:07
1

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();
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

You can try this

DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
String completionDate = df.format(request.getParameter("completion_date"));
System.out.println(completionDate);
julianstark999
  • 3,450
  • 1
  • 27
  • 41
  • I need the completionDate to be in date format, so should I parse it again ? Is this the right way? – Shashi Kiran Apr 04 '16 at 10:01
  • 1
    No you don't need to parse it. You simply need to format your completionDate. If you take a look at your first try. The output is *21/10/2016* so in my example your request compeletion_date parameter gets formatted to the given format – julianstark999 Apr 04 '16 at 10:11
0

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);
Jeet
  • 248
  • 1
  • 8