简体   繁体   中英

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. The values it will accept into the table are in the format 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. 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

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"); Date myDate = formatter.parse(date);

2. enter link description here The TO_DATE function can be used in Oracle/PLSQL. For example:

TO_DATE('2003/07/09', 'yyyy/mm/dd') would return a date value of July 9, 2003

TO_DATE('070903', 'MMDDYY') would return a date value of July 9, 2003

TO_DATE('20020315', 'yyyymmdd') would return a date value of Mar 15, 2002

Your DB column has a DateTime type. 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. You can use the milli-second value of your Joda DateTime object to create whatever is required by the API.

You are working way too hard.

  1. Convert your Joda-Time DateTime object to a java.util.Date . Just call toDate() method.
  2. Pass Date instance to your framework or 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. Joda-Time knows how to convert to 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. 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. These classes supplant the mess that is java.util.Date/Calendar. They are inspired by Joda-Time but are entirely re-architected. As they are a built-in part of Java, expect JDBC and related frameworks to be updated to handle these new types directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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