简体   繁体   中英

Convert java.util.Date in YYYY-MM-DD HH:MM:SS format to compatible with MySQL DATETIME

For log entry of a web application I have table with primary key DATETIME in MySQL table.
But java.util.Date is in different format causing mapping error in Hibernate.

How can I map DATETIME type of MySQL table to java.util.Date ?

try this...

java.sql.Timestamp oracleDate = new java.sql.Timestamp(yourdate.getTime());

edit...

I just checked what I was doing in some of my code, and java.util.Date is working with no specific settings for hibernate. My code above probably only helps JDBC code. You might have a different type of problem. Can you share some code?

java.util.Date temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").parse("2014-01-24 11:23:00.000000");

I Think this is what you are looking for?

The code is

public class Test {

public static void main(String[] args) throws ParseException {
    java.util.Date temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS")
            .parse("2014-01-24 11:23:00.000000");
    System.out.println(temp);
}}

Now pass this to your database

If you're using hibernate, use a @Temporal annotation with TemporalType.DATE and Hibernate should handle things for you. If not, leave a comment.

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