Java数据类型仅保留日期 [英] Java data type to hold only date

查看:79
本文介绍了Java数据类型仅保留日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中哪种数据类型可以仅保存日期,并且不需要时间成分?例如,仅存储 2012年12月7日.我正在与具有仅日期数据类型的数据库进行数据持久化,因此我正在寻找Java中最佳的等效数据类型.

Which data type in Java can hold just the date and doesn't require a time component? For example, just to store 12/07/2012. I'm working with persisting the data to/from a database that has a date-only data type, so I'm looking for the best equivalent data type in Java.

推荐答案

:

一个毫秒值左右的精简包装器,它使JDBC能够识别这是一个SQL DATE值.毫秒值代表数字自 1970年1月1日00:00:00.000 GMT 以来经过的毫秒数.

要符合SQL DATE的定义,毫秒值由 java.sql.Date 实例包装的实例必须通过设置进行规范化"在小时,分钟,秒和毫秒中将零实例所关联的特定时区.

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 :

or from JodaTime: DateMidnight or LocalDate (thanks @cdeszaq)

DateMidnight 定义一个日期,其中时间部分固定为午夜.该课程使用时区,因此午夜是当地时间,除非使用UTC时区.

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.

重要的是要强调,该课程代表了在任何一天的午夜.请注意,午夜定义为00:00,这是一天的开始.

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.

这篇关于Java数据类型仅保留日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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