Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert string(containing timezone) into datetimeoffset column in SQL server 2008 through Java program

I have to insert data(which is also containing the timezone, i.e. 2013-01-19 00:00:00 +0530) which is in String form and the datatype of the column is DATETIMEOFFSET. I have tried both java.util.date and sql.date but could not find any solution.

like image 634
abhijeet Avatar asked Dec 15 '25 06:12

abhijeet


1 Answers

If you're using the Microsoft JDBC driver, you can use the DateTimeOffset class, constructing instances with the valueOf method.

You'll need to parse the value out into local time and offset (in order to pass the two parts separately) but that shouldn't be too bad using SimpleDateFormat. (The Z format specifier in SimpleDateTimeFormat will handle offsets like +0530.) Alternatively, use Joda Time which will make life easier still, as it will allow you to parse to a DateTime which lets you get the offset as well as the local time in one go. I would personally use Joda Time and create a method to convert from a DateTime to a DateTimeOffset.

like image 180
Jon Skeet Avatar answered Dec 16 '25 20:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!