How to parse String to java.sql.date
Asked Answered
G

3

9

I have a String

String s = "01 NOVEMBER 2012";

Then I want parse it to sqlDate. And insert it into the database.

Is it possible to parse that string to sqlDate?!?!

Yup, sql date format is "yyyy-mm-dd"

Glennisglennon answered 11/10, 2012 at 16:13 Comment(1)
While in 2012 when the question was asked, java.sql.Date was the class we used for transferring a date without time of day to and from SQL databases, today it’s better to use LocalDate from java.time, the modern Java date and time API.Andorra
G
18

Use SimpleDateFormat to parse String date to java.util.Date

java.util.Date utilDate = new SimpleDateFormat("dd MMM yyyy").parse("01 NOVEMBER 2012");

and then convert it to java.sql.Date using millis

java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
Gowan answered 11/10, 2012 at 16:14 Comment(2)
Use java.sql.Date.valueOf()! It's better.Crowbar
java.sql.Date.valueOf() works for JDBC date escape format. The format is "yyyy-[m]m-[d]d" It won't work with every date format.Questionary
A
1

java.time

I am providing the modern answer. I recommend that you use java.time, the modern Java date and time API, for your date work.

    DateTimeFormatter dateFormatter = 
        new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .appendPattern("dd MMMM uuuu")
            .toFormatter(Locale.ENGLISH);
    
    String s = "01 NOVEMBER 2012";
    
    LocalDate date = LocalDate.parse(s, dateFormatter);
    
    System.out.println(date);

Output:

2012-11-01

You asked for a java.sql.Date? By all likelihood you don’t need any. I assume that you wanted one for use with your SQL database. Since JDBC 4.2 you can use LocalDate there too. For example:

    PreparedStatement statement = yourDatabaseConnection.prepareStatement(
            "insert into your_table (your_date_column) values (?);");
    statement.setObject(1, date);
    statement.executeUpdate();

Note the use of PreparedStatement.setObject() (not setDate()).

If you do need a java.sql.Date for a legacy API not yet upgraded to java.time, the conversion is easy and straightforward:

    java.sql.Date oldfashionedJavaSqlDate = java.sql.Date.valueOf(date);
    System.out.println(oldfashionedJavaSqlDate);

2012-11-01

Links

Andorra answered 4/7, 2020 at 15:54 Comment(0)
E
0

Expanding on Jigar Joshi answer.

This code has been able to handle whatever I've needed.

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

Date date1;
Date date2;
Date date3;

    //converting string into sql date);
    try {
         date1 = new java.sql.Date(
                     ((java.util.Date) new SimpleDateFormat("dd MMM yyyy").parse("01 NOVEMBER 2012")).getTime());

        date2 = new java.sql.Date(
                    ((java.util.Date)newSimpleDateFormat("dd/MM/yyyy").parse("02/09/2012")).getTime());

        date3 = new java.sql.Date(
                    ((java.util.Date) new SimpleDateFormat("ddMMyyyy").parse("03092012")).getTime());

    } catch (ParseException e) {
            e.printStackTrace();
        }

    System.out.println("sqlDate test1:" + date1);
    System.out.println("sqlDate test2:" + date2);
    System.out.println("sqlDate test3:" + date3);


    output:
           sqlDate test1:2012-11-01
           sqlDate test2:2012-09-02
           sqlDate test3:2012-09-03
Eulogistic answered 29/6, 2018 at 6:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.