加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java数据类型只保留日期

发布时间:2020-12-14 05:28:02 所属栏目:Java 来源:网络整理
导读:Java中的哪种数据类型只能保存日期,不需要时间组件?例如,只是为了存储12/07/2012.我正在将数据持久化到具有日期数据类型的数据库中,因此我正在寻找Java中最佳的等效数据类型. 解决方法 从JDK: javax.sql.Date : A thin wrapper around a millisecond valu
Java中的哪种数据类型只能保存日期,不需要时间组件?例如,只是为了存储12/07/2012.我正在将数据持久化到具有日期数据类型的数据库中,因此我正在寻找Java中最佳的等效数据类型.

解决方法

从JDK: javax.sql.Date

A thin wrapper around a millisecond value that allows JDBC to identify
this as an SQL DATE value. A milliseconds value represents the number
of milliseconds that have passed since January 1,1970 00:00:00.000 GMT.

To conform with the definition of SQL DATE,the millisecond values
wrapped by a java.sql.Date instance must be ‘normalized’ by setting
the hours,minutes,seconds,and milliseconds to zero in the
particular time zone with which the instance is associated.

或从JodaTime:DateMidnightLocalDate(谢谢
@cdeszaq)

DateMidnight defines a date where the time component is fixed at
midnight. The class uses a time zone,thus midnight is local unless a
UTC time zone is used.

It is important to emphasise that this class represents the time of midnight on any given day. Note that midnight is defined as 00:00,which is at the very start of a day.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读