【问题标题】:android adding or removing hour from timestamp in databaseandroid从数据库中的时间戳添加或删除小时
【发布时间】:2012-10-26 19:15:40
【问题描述】:

我想从我在 Android 中的时间戳中减去几天。

我有一个SQLite 数据库,其日期字段类型为long。 当我执行查询时,我说date = date - (3 * 86400000) 有时会在将其转换为可读日期时遇到一小时的差异。

但这怎么可能呢?这与我所在时区的一小时夏令时有关吗?

因为如果这是因为您正在使用两个长值进行计算并且在将它们转换回日期时间之后不应该出现夏令时问题,我会觉得很奇怪?

【问题讨论】:

  • 你怎么知道相差一小时?您是否将这些转换为DateCalendar 并检查?查看一些示例代码以了解您在做什么会很有帮助。
  • 如果设置了一个而另一个未设置,则时区可能是一个问题。由于 Java 日期默认为 GMT,这将是您确定谁在哪个时区的基础。

标签: java database sqlite datetime


【解决方案1】:

感谢您的回复,但我找到了解决方案。 似乎当您在时间戳中添加以毫秒为单位的天数并且两者之间的日光设置有一个小时的差异时,日期会自动更正。

当我执行以下操作时,小时会被忽略:

Date d = new Date();
d.setDays(d.getDays() - 2);

但是,我建议使用 Joda 库,因为它更加灵活且易于使用。

【讨论】:

【解决方案2】:

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 中将其解析为 InstantInstant 是 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/MontrealAfrica/CasablancaPacific/Auckland。切勿使用 2-4 个字母的缩写,例如 ESTIST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。

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.

zdtzdtLater 之间经过的时间可能是也可能不是 ( 3 * 24 ) 小时。如果在此期间发生 DST 切换,则小时数将是年初“提前春季”中的 ( ( 3 * 24 ) - 1 ),或秋季“后退”中的 ( ( 3 * 24 ) + 1 )


关于java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

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 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 2014-06-10
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    相关资源
    最近更新 更多