tl;博士
使用现代 java.time 类。
如果您的意思是日历日期中的 3 天,请执行以下操作:
Instant // Represent a moment in UTC with a resolution of nanoseconds.
.ofEpochSecond( x ) // Parse some number of whole seconds or milliseconds from the epoch reference of first moment of 1970 in UTC.
.atZone( // Adjust from UTC to the wall-clock time used by the people of a particular region (a time zone).
ZoneId.of( "Pacific/Auckland" ) // Assign the time zone by which you want the context of their calendar for adding dates.
) // Returns a `ZonedDateTime` object.
.plus( // Move to another moment by adding a span-of-time.
Period.ofDays( 3 ) // Define a span of time as a number of days on the calendar (date) without regard for the length of a day.
) // Returns another `ZonedDateTime` object.
.toInstant() // Adjust from a time zone to UTC. Same moment, different wall-clock time.
.toEpochSeconds() // or maybe `toEpochMillis` depending on your practice with your `long` number.
将您的 SQL 编写为:
SELECT * FROM event_ WHERE when_ >= ? ; -- Find events occurring later than the passed moment.
在带有 JDBC 4.2 或更高版本的 Java 中:
myPreparedStatement.setObject( 1 , secondsSinceEpoch ) ; // Pass seconds calculated above.
检索。
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
从纪元开始计数
您的long 类型数大概是自epoch reference date 以来的整秒或毫秒数。
如果您选择的纪元恰好是 “Unix time” 使用的纪元,即 1970 年 UTC 的第一刻,1970-01-01T00:00:00Z,那么我们可以在 Java 中将其解析为 Instant。 Instant 是 UTC 时间线上的时刻,分辨率为纳秒。 Instant 内部是一对数字:自纪元以来的整秒数,加上纳秒数的几分之一秒。
Instant instant = Instant.ofEpochMilli( x ) ;
…或:
Instant instant = Instant.ofEpochSecond( x ) ;
一天不是 24 小时
一天,就像日历上的日期一样,与 24 小时不同。政治家定义时区(例如夏令时 (DST))的方式异常意味着一天可能是 23、23.5、24、25 或其他一些小时-分钟-秒数。
Duration
如果您想添加 24 小时的数据块,请使用 Duration。
Duration d = Duration.ofDays( 3 ) ; // `Duration` defines a “day” as being a 24-hours without regard for the calendar dates.
或以小时为单位指定,这在Duration 类中具有相同的结果,但会更清楚您的意图和理解。
Duration d = Duration.ofHours( 3 * 24 ) ; // Same effect as the `Duration.ofDays( 3 )` seen above.
添加到您的Instant。
Instant instantLater = instant.plus( d ) ;
Period
如果您的意思是在日历上增加一天,请使用 Period 类。此类代表若干年-月-日。
Period p = Period.ofDays( 3 ) ; // `Period` defines a day as a date on the calendar without regard for its length in hours.
如果不考虑时区问题,我们无法将其添加到 Instant。如果添加到始终使用 UTC 的 Instant,我们将使用 UTC 日历。例如,在 UTC 值中添加一天可能会在印度转换为“明天”,而在魁北克仍然是“昨天”。时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因区域而异。
如果没有指定时区,JVM 会隐式应用其当前的默认时区。该默认值可能在运行时(!)期间change at any moment,因此您的结果可能会有所不同。最好将您的 desired/expected time zone 明确指定为参数。
以continent/region 的格式指定proper time zone name,例如America/Montreal、Africa/Casablanca 或Pacific/Auckland。切勿使用 2-4 个字母的缩写,例如 EST 或 IST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。
ZoneId z = ZoneId.of( "America/Montreal" ) ;
如果你想使用 JVM 当前的默认时区,请求它并作为参数传递。如果省略,则隐式应用 JVM 的当前默认值。最好是明确的,因为默认值可能会在任何时候在运行时被 JVM 中任何应用程序的任何线程中的任何代码更改。
将您的ZoneId 应用到Instant 以获得ZonedDateTime。
ZonedDateTime zdt = instant.atZone( z ) ;
现在您可以在ZonedDateTime 中添加日期,并且该地区的人们使用的日历将被考虑在内。
ZonedDateTime zdtLater = zdt.plus( p ) ; // By “later” we assume the number of days in the `Period` is positive. Alternatively, a `Period` could go backwards in time with a negative number of days.
要调整回 UTC,请提取 Instant。我们仍然拥有相同的时刻,时间轴上的相同点,但挂钟时间不同。
Instant instantLater = zdtLater.toInstant() ; // Same moment, different wall-clock time.
zdt 和 zdtLater 之间经过的时间可能是也可能不是 ( 3 * 24 ) 小时。如果在此期间发生 DST 切换,则小时数将是年初“提前春季”中的 ( ( 3 * 24 ) - 1 ),或秋季“后退”中的 ( ( 3 * 24 ) + 1 )。
关于java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.Date、Calendar 和 SimpleDateFormat。
Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。
要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310。
您可以直接与您的数据库交换 java.time 对象。使用符合JDBC 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。
从哪里获得 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如Interval、YearWeek、YearQuarter 和more。