简体   繁体   English

通过Java程序将字符串(包含时区)插入SQL Server 2008中的datetimeoffset列

[英]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, ie 2013-01-19 00:00:00 +0530) which is in String form and the datatype of the column is DATETIMEOFFSET. 我必须插入字符串形式的数据(也包含时区,即2013-01-19 00:00:00 +0530),并且该列的数据类型为DATETIMEOFFSET。 I have tried both java.util.date and sql.date but could not find any solution. 我已经尝试了java.util.date和sql.date但都找不到任何解决方案。

If you're using the Microsoft JDBC driver , you can use the DateTimeOffset class, constructing instances with the valueOf method. 如果使用的是Microsoft JDBC驱动程序 ,则可以使用DateTimeOffset类,并使用valueOf方法构造实例。

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 . 您需要将值解析为本地时间和偏移量(以便分别传递这两个部分),但是使用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. SimpleDateTimeFormatZ格式说明符将处理+0530之类的偏移量。)或者,使用Joda Time将使生活更加轻松,因为它将允许您解析为DateTime ,从而可以获取偏移量以及本地时间。一口气。 I would personally use Joda Time and create a method to convert from a DateTime to a DateTimeOffset . 我个人使用约达时间,并创建从一个转换方法DateTimeDateTimeOffset

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM