string to java.sql.date

www.laut‮moc.irut‬
string to java.sql.date

To convert a string to a java.sql.Date object in Java, you can use the valueOf method of the java.sql.Date class. This method takes a string in the yyyy-[m]m-[d]d format as an argument and returns a java.sql.Date object that represents the same date.

Here's an example of how to use the valueOf method:

String s = "2022-12-20";
java.sql.Date date = java.sql.Date.valueOf(s);

Note that the valueOf method only works for strings in the yyyy-[m]m-[d]d format. If the input string is in a different format, the valueOf method will throw an IllegalArgumentException.

You can also use the SimpleDateFormat class to parse a string in a different format into a java.sql.Date object. Here's an example of how to do this:

String s = "12/20/2022";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
try {
    java.util.Date utilDate = df.parse(s);
    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
} catch (ParseException e) {
    e.printStackTrace();
}
Created Time:2017-10-17 20:18:55  Author:lautturi