1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/* Java String to java.sql.Date Example This Java String to java.sql.Date example shows how to convert Java String object containing date to java.sql.Date object. */ import java.sql.Date; import java.text.ParseException; import java.text.SimpleDateFormat; public class ConvertStringToJavaSQLDateExample { public static void main(String args[]) throws ParseException{ //string containing date String strDate = "2011-12-31 00:00:00"; /* * To convert String to java.sql.Date, use * Date (long date) constructor. * * It creates java.sql.Date object from the milliseconds provided. */ //first convert string to java.util.Date object using SimpleDateFormat SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); java.util.Date date = sdf.parse(strDate); java.sql.Date sqlDate = new Date(date.getTime()); System.out.println("String converted to java.sql.Date :" + sqlDate); } } /* Output of above given String to java.sql.Date example would be 2011-01-31 */ |
How to get sqlDate output like this : 2011-12-31 00:00:00
Your example is wrong.
fix the “yyyy-mm-dd hh:mm:ss” to “yyyy-MM-dd hh:mm:ss”
datetime is not printing