2

I want to convert from string to date using Java 8.

I can easily convert using SimpleDateFormat and yyyy-MM-dd format

   String startDate2="2017-03-24";
   SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
   System.out.println(new java.sql.Date(sdf1.parse(startDate2).getTime()));

output: 2017-03-24

   String startDate2="2017-03-24";
   SimpleDateFormat sdf1 = new SimpleDateFormat("uuuu-MM-dd");
   System.out.println(new java.sql.Date(sdf1.parse(startDate2).getTime()));

But when I use 'uuuu-MM-dd' instead of 'yyyy-MM-dd'

output : 1970-03-24(wrong)

now in Java 8:

   String startDate1="2017-03-23";
   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd");

But I don't know how I can get the date which would be sql date type same as above correct output.

Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
jitendra varshney
  • 3,484
  • 1
  • 21
  • 31

3 Answers3

6

java.sql.Date has a static valueOf method that takes a Java 8 LocalDate so you can do:

String startDate1 = "2017-03-23";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd");

LocalDate date = LocalDate.parse(startDate1, formatter);

java.sql.Date sqlDate = java.sql.Date.valueOf(date);
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • 1
    Yes it is necessary to use LocalDate. Java 8 date/time classes won't convert directly to old style date classes – greg-449 Mar 24 '17 at 07:55
  • but why SimpleDateFormat is giving wrong output when i use 'uuuu' instead of 'yyyy'? – jitendra varshney Mar 24 '17 at 07:57
  • 2
    Because 'u' means 'day number of week' in `SimpleDateFormat`. It only means year in `DateTimeFormatter` – greg-449 Mar 24 '17 at 08:01
  • 2
    @greg-449 No need for that formatting pattern. That pattern is defined by the ISO 8601 standard, and the java.time uses standard formats by default when parsing & generating strings. `LocalDate.parse( "2017-03-23" )` You could even combine it together: `java.sql.Date.valueOf( LocalDate.parse( "2017-03-23" ) )` – Basil Bourque Mar 24 '17 at 09:10
1

As far as I can see, you have a text in yyyy-MM-dd format and you want it in uuuu-MM-dd format. So you need two formats:

String startDate2="2017-03-24";
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat targetFormat = new SimpleDateFormat("uuuu-MM-dd");
java.sql.Date date = new java.sql.Date(sourceFormat.parse(startDate2).getTime());
String formattedAsDayOfWeek = targetFormat.format(date);
System.out.println(formattedAsDayOfWeek);       

Bottom line is that Date contains a millisecond value. java.sql.Date.toString() uses the yyyy-MM-dd format regardless how you parsed it. java.util.sql.Date uses another format: EEE MMM dd hh:mm:ss zzz yyyy with English Locale.

You can do other formatting with DateFormat -s.

I presume you need the uuuu-MM-dd format for inserting data to the database. What does that logic look like?

Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
  • Tamas i have used 'uuuu-MM-dd' instead of yyyy-MM-dd you can see this https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns – jitendra varshney Mar 24 '17 at 08:51
  • @jitendravarshney your input date, ` String startDate2="2017-03-24";` is in `yyyy-MM-dd` format, as long as you use `SimpleDateFormat`. I.e. `SimpleDateFormat` is a subclass of `DateFormat`. On the other hand, you are trying to use `DateTimeFormatter`, which uses another set of pattern characters. – Tamas Rev Mar 24 '17 at 09:17
  • can you give me a example of 'uuuu-MM-dd' @Tamas – jitendra varshney Mar 24 '17 at 10:31
  • @jitendravarshney If you use `DateFormat` like `SimpleDateFormat`, then uuuu-MM-dd will be `0005-03-24`, but that's sort of meaningless.On the other hand, if you interpret if wit `DateTimeFormatter`, then `2017-03-24` is a good example. – Tamas Rev Mar 24 '17 at 12:01
1

You don’t want a java.sql.Date. You want a LocalDate. Your SQL database wants one too.

    String startDate2 = "2017-03-24";              

    LocalDate date = LocalDate.parse(startDate2);
    System.out.println(date);

Output is:

2017-03-24

I am exploiting the fact that your string is in ISO 8601 format. The classes of java.time including LocalDate parse this format as their default, that is, without any explicit formatter.

You also note that we don’t need any explicit formatter for formatting back into uuuu-MM-dd format for the output. The toString method implicitly called from System..out.println() produces ISO 8601 format back.

Assuming that you are using a JDBC 4.2 compliant driver (I think we all are now), I am taking the way to pass it on to your SQL database from this question: Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2:

myPreparedStatement.setObject ( 1 , date ); // Automatic detection and conversion of data type.

Refer to the linked question for much more detail.

The java.sql.Date class is poorly designed, a true hack on top of the already poorly designed java.util.Date class. Both classes are long outdated. Don’t use any of them anymore.

One more link: Wikipedia article: ISO 8601

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161