最适合存储日期和时间的SQL和Java数据类型 [英] Most appropriate SQL and Java data types for storing date and time

查看:185
本文介绍了最适合存储日期和时间的SQL和Java数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个较宽泛的问题,我们预先道歉.

使用以下格式处理日期和时间的最合适的MySQL和Java数据类型是什么:yyyy.MM.dd hh:mm:ss

我需要能够转换日期&的字符串表示形式时间转换为给定的日期格式,然后将该日期和时间存储在数据库中.然后,我需要做相反的工作,并转换MySQL日期&时间转换为相应的Java表示形式

我已经阅读了很多有关Date,DateTime,ZonedDateTime的问题,但似乎都无法正常工作.

谢谢.

解决方案

使用以下格式处理日期和时间的最合适的MySQL和Java数据类型是什么:yyyy.MM.dd hh:mm:ss

最合适的MySQL类型是DATETIME.参见我应该使用字段"datetime"还是"timestamp"?.

在持久层中使用的相应Java类型(jdbc类型)是java.sql.Timestamp.参见 Java,JDBC和MySQL类型.

我需要能够转换日期&的字符串表示形式时间转换为给定的日期格式,然后将该日期和时间存储在数据库中.

正确的转换是:java.lang.String -> java.util.Date -> java.sql.Timestamp.

  • java.lang.String -> java.util.Date:

Date javaDatetime = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss").parse(stringDatetime);

  • java.util.Date -> java.sql.Timestamp:

Timestamp jdbcDatetime = new Timestamp(javaDatetime.getTime());

然后,我需要做相反的工作,并将MySQL日期&转换为时间进入相应的Java表示形式

执行相反的路径:

  • java.sql.Timestamp -> java.util.Date:

Date javaDatetime = new java.util.Date(jdbcDatetime.getTime());

  • java.lang.Date -> java.util.String:

String stringDatetime = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss").format(javaDatetime);

我一直很简洁.您必须注意SimpleDateFormat的使用(例如,缓存已使用applyPattern和setLenient初始化的实例).

针对Java 8的更新 对jdbc类型进行了一些增强(请参见 JDBC 4.2 ),以简化转化.对于上述情况,您可以使用 toLocalDateTime 解决方案

What are the most appropriate MySQL and Java data types for handling date and times with the following format: yyyy.MM.dd hh:mm:ss

The most appropriate MySQL type is DATETIME. See Should I use field 'datetime' or 'timestamp'?.

The corresponding Java type to use in your persistence layer (jdbc type) is java.sql.Timestamp. See Java, JDBC and MySQL Types.

I need to be able to convert a String representation of the date & time into the given Date Format as well as then store that date&time in the database.

The correct transformation is: java.lang.String -> java.util.Date -> java.sql.Timestamp.

  • java.lang.String -> java.util.Date:

Date javaDatetime = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss").parse(stringDatetime);

  • java.util.Date -> java.sql.Timestamp:

Timestamp jdbcDatetime = new Timestamp(javaDatetime.getTime());

I then need to do the reverse and convert the MySQL date & time into the corresponding Java representation

Do the reverse path:

  • java.sql.Timestamp -> java.util.Date:

Date javaDatetime = new java.util.Date(jdbcDatetime.getTime());

  • java.lang.Date -> java.util.String:

String stringDatetime = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss").format(javaDatetime);

I've been concise. You have to pay attention about the use of SimpleDateFormat (e.g. cache an instance which has been initialized with applyPattern and setLenient).

Update for Java 8 Some enhancements have been done on jdbc types (see JDBC 4.2) that simplify conversions. For the case illustrated above you can use toLocalDateTime and valueOf to convert from java.sql.Timestamp to the new java.time.LocalDateTime and back.

这篇关于最适合存储日期和时间的SQL和Java数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆