简体   繁体   English

如何将Joda DateTime对象添加到我的MSSQL数据库中

[英]How do i add a Joda DateTime Object into my MSSQL database

so i have a mssql database with a DateObjectCreated column of type DateTime. 所以我有一个mssql数据库,其DateTime类型的DateObjectCreated列。 The values it will accept into the table are in the format 2013-12-23 12:23:56.567. 它将接受到表中的值的格式为2013-12-23 12:23:56.567。 However, my java program is creating a joda DateTime object with the format 2013-12-23T 12:23:56.567Z. 但是,我的Java程序正在创建格式为2013-12-23T 12:23:56.567Z的joda DateTime对象。 this wont insert into my db. 这不会插入我的数据库。 i need to either convert "2013-12-23T 12:23:56.567Z" to 2013-12-23 12:23:56.567 or find a way to allow my db table to accept "2013-12-23T 12:23:56.567Z" format any help on this matter will be much appreciated Many thanks Billy Controller 我需要将“ 2013-12-23T 12:23:56.567Z”转换为2013-12-23 12:23:56.567或找到一种方法来允许我的数据库表接受“ 2013-12-23T 12:23”: 56.567Z“格式,对此问题的任何帮助将不胜感激非常感谢Billy Controller

Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String formattedDate = formatter.format(date);//this gives me the string as i need it 
DateTime dt = new DateTime(formattedDate);//here it adds the 'T' and 'Z'

ive tried 我试过了

Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String formattedDate = formatter.format(date);//this gives me the string as i need it 
Date dt = formatter.parse(formattedDate);//here it gives me the same as new Date()
  1. DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); DateFormat formatter = new SimpleDateFormat(“ yyyy-MM-dd”); Date myDate = formatter.parse(date); 日期myDate = formatter.parse(date);

2. enter link description here The TO_DATE function can be used in Oracle/PLSQL. 2. 在此处输入链接描述 TO_DATE函数可以在Oracle / PLSQL中使用。 For example: 例如:

TO_DATE('2003/07/09', 'yyyy/mm/dd') would return a date value of July 9, 2003 TO_DATE('2003/07/09','yyyy / mm / dd')将返回日期值为2003年7月9日

TO_DATE('070903', 'MMDDYY') would return a date value of July 9, 2003 TO_DATE('070903','MMDDYY')将返回2003年7月9日的日期值

TO_DATE('20020315', 'yyyymmdd') would return a date value of Mar 15, 2002 TO_DATE('20020315','yyyymmdd')将返回日期值为2002年3月15日

Your DB column has a DateTime type. 您的数据库列具有DateTime类型。 The text representation of your date is irrelevant for persisting it. 日期的文本表示形式与保留日期无关。 Regardless of the API you are using (JDBC, JPA, Hibernate) there will be something like a "setDateTime(Date date)" method that allows you to pass in a java.util.Date or java.sql.Date or a Java long. 无论您使用的是哪种API(JDBC,JPA,Hibernate),都会有类似“ setDateTime(Date date)”的方法,该方法使您可以传入java.util.Date或java.sql.Date或Java long。 。 You can use the milli-second value of your Joda DateTime object to create whatever is required by the API. 您可以使用Joda DateTime对象的毫秒值来创建API所需的任何内容。

You are working way too hard. 你工作太辛苦了。

  1. Convert your Joda-Time DateTime object to a java.util.Date . 将您的Joda-Time DateTime对象转换为java.util.Date Just call toDate() method. 只需调用toDate()方法即可。
  2. Pass Date instance to your framework or SQL. 将Date实例传递到您的框架或SQL。

The answer by Ralf is correct in its first part, but is wrong in the end in that you need not deal with milliseconds. Ralf答案在第一部分是正确的,但最终是错误的,因为您无需处理毫秒。 Joda-Time knows how to convert to java.util.Date. Joda-Time知道如何转换为java.util.Date。

org.joda.time.DateTime now = new org.joda.time.DateTime();
java.util.Date nowAsJavaUtilDate = now.toDate();

By the way, the java.sql.Date class is simply a very thin subclass of java.util.Date. 顺便说一句, java.sql.Date类只是java.util.Date的一个非常薄的子类。 So don't let that throw you. 所以不要让那丢给你。


In the future, this will become even easier. 将来,这将变得更加容易。 Java 8 brings the new JSR 310 classes in the java.time.* package. Java 8在java.time。*包中引入了新的JSR 310类。 These classes supplant the mess that is java.util.Date/Calendar. 这些类取代了java.util.Date/Calendar的混乱局面。 They are inspired by Joda-Time but are entirely re-architected. 它们受到Joda-Time的启发,但已完全重新设计。 As they are a built-in part of Java, expect JDBC and related frameworks to be updated to handle these new types directly. 由于它们是Java的内置部分,因此期望JDBC和相关框架进行更新以直接处理这些新类型。

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

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