(Urgent)How to convert from String to java.sql.Time ? then from java.sql.Time to String?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   stanley_ten
Posted On:   Tuesday, September 25, 2001 06:46 AM

How to convert from String to java.sql.Time ? then from java.sql.Time to String? Please give some code samples... thanks

Re: (Urgent)How to convert from String to java.sql.Time ? then from java.sql.Time to String?

Posted By:   das_Megabyte  
Posted On:   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.

Re: (Urgent)How to convert from String to java.sql.Time ? then from java.sql.Time to String?

Posted By:   vijay_grprasath  
Posted On:   Tuesday, September 25, 2001 07:37 AM

String - Time


String strDt = "2001-01-01 00:00:00.000000000";

Time time = Time.valueOf(strDt);


Time - String


time.toString();

About | Sitemap | Contact