Java Program To Convert String Into Sql Date

01. Example:

import java.sql.Date;

public class StringToSQLDateExample {
  public static void main(String[] args) {
    String dataString = "2023-02-12";
    Date sqlDate = Date.valueOf(dataString);
    System.out.println(sqlDate);
  }
}

Output:

2023-02-12

This Java code uses the java.sql.Date class to convert a string representation of a date into a java.sql.Date object.

The code defines a string dataString with the value “2023-02-12”, which represents a date in the format “yyyy-mm-dd”. Then, it calls the valueOf method of the java.sql.Date class and passes dataString as an argument. This method returns an java.sql.Date object representing the specified date.

Finally, the code prints the resulting sqlDate object. The output of this code will be “2023-02-12”.


Leave a comment