Re: (Urgent)How to convert from String to java.sql.Time ? then from java.sql.Time to String?
Tuesday, September 25, 2001 08:19 AM
Remeber that java.sql.Time is sort of stupid...it only saves the time values of a Date/Time. Similarly, java.sql.Date only saves the date value. Important to remember when calling ResultSet.getDate() or ResultSet.getTime()! I find it much more useful to use setString() and getString and let the SQL server deal with the conversion.
Anyway, the above sample isn't that great because you rarely have well formed SQL dates to deal with. I utilize a couple of java.text classes, DateFormat and SimpleDateFormat, along with the long getTime() method of java.util.Date to set times using strings.
String myAnniversary = "09/01/2001 2:00 pm";
DateFormat convertDate = new SimpleDateFormat("M/d/yyyy H:mm a"); //look up the docs for more info on SimpleDateFormat, your friend and mine
java.util.Date anniversaryDate = convertDate.parse(myAnniversary);
java.sql.Time anniversarySQLDate = new java.sql.Time(anniversaryDate.getTime());
If you want to parse a date from a SQL server, use DateFormat's format(Date) method.