- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 Android 中使用 Joda-Time DateTime
。
DateTimeZone
似乎无法正常工作。也许这与夏令时有关?
此刻,我们有 GMT +2。这将在几周后改变,因为冬天。然后是 GMT +1。但现在是不正确的。
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.joda.time.LocalDateTime
// GMT: Thursday 26 October 2017 12:11:54
val epochTime: Long = 1509019914
var dateServer = LocalDateTime(epochTime * 1000).toDateTime(DateTimeZone.forID("Europe/Amsterdam"))
预计(阿姆斯特丹的正确时间):
14:11:54 GMT+02:00 DST
实际:
13:11 2017-10-26T13:11:54.000+01:00
最佳答案
LocalDateTime
构造函数获取 epochTime
值和 converts to the default timezone获取日期和时间值 - 检查 DateTimeZone.getDefault()
的值,它可能是当前使用 +01:00
偏移量的时区。
然后 toDateTime
方法创建一个 DateTime
,它对应于由 LocalDateTime
表示的相同日期和时间,但在指定的时区 (它只是将时区“附加”到日期/时间值,不进行任何转换。
如果要获取特定时区的epochTime
对应的日期和时间,只需create the DateTime
directly :
val dateServer = DateTime(epochTime * 1000, DateTimeZone.forID("Europe/Amsterdam"));
这样,dateServer
将是:
2017-10-26T14:11:54.000+02:00
我的默认时区的一个例子,只是为了更清楚。我的默认时区(由 DateTimeZone.getDefault()
返回)是 America/Sao_Paulo
,它使用偏移量 -02:00
(2 小时2017 年 10 月 26 日。
epochTime
1509019914 对应 UTC 2017-10-26T12:11:54Z
。
当我执行 LocalDateTime(epochTime * 1000)
时,它会获取相应的 UTC 值 (12:11) 并转换为默认时区:在我的例子中为 10:11,因此 LocalDateTime
的值为 2017-10-26T10:11:54
。
然后 toDateTime
方法只创建一个 DateTime
对应于相同的日期和时间 (2017-10-26T10:11:54
), 在指定的时区。因此它在阿姆斯特丹 (+02:00
) 创建 2017-10-26T10:11:54
。
您的默认时区可能是使用 +01:00
偏移量的时区,这可以解释您得到的差异(12:11 UTC 首先转换为 LocalDateTime
13:11,然后 toDateTime
在阿姆斯特丹创建 13:11。
Joda-Time 处于维护模式,正在被新的 API 取代,所以我不建议用它开始一个新项目。即使在 joda's website它说:“请注意,Joda-Time 被认为是一个基本“完成”的项目。没有计划进行重大增强。如果使用 Java SE 8,请迁移到 java.time (JSR-310)。” .
如果您不能(或不想)从 Joda-Time 迁移到新 API,您可以忽略此部分。
在 Android 中,您可以使用 ThreeTen Backport ,一个很好的 backport Java 8's new date/time classes .要使其正常工作,您还需要 ThreeTenABP (更多关于如何使用它的信息 here)。
要从 epochTime
获取相应的 UTC 时刻,您可以使用 org.threeten.bp.Instant
类。然后使用 org.threeten.bp.ZoneId
将其转换为时区,从而生成 org.threeten.bp.ZonedDateTime
:
val dateServer = Instant.ofEpochSecond(epochTime).atZone(ZoneId.of("Europe/Amsterdam"));
dateServer
将是一个 org.threeten.bp.ZonedDateTime
,其值对应于 2017-10-26T14:11:54+02:00 [欧洲/阿姆斯特丹]
。
关于android - Joda DateTime 时区未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46954570/
在我的 Rails (3.0) 应用程序中,我使用助手 time_zone_select() 来使用时区。它生成时区名称,如“(GMT+01:00) Paris”... 但这些名称与 MySQL 中的
我正在解析格式为的日期: SimpleDateFormat formatter = new SimpleDateFormat("EE MMM dd HH:mm:ss Z yyyy"); 当我这样做的时
我在 Android 上使用 ACTION_TIMEZONE_CHANGED Intent 过滤器来响应时区更改。 我注意到 Jodas 当前时区此时没有更新,使用:DateTimeZone.getD
我有一个旧版 Windows 应用程序,它从数据库读取数据。其中一列是“TimeZoneInfoId”。在传统世界中,它是由另一个 Windows 应用程序编写的,因此它存储 Windows 字符串:
我是数据库开发新手,目前从事 MySQL 工作。我有一列包含美国的所有时区。我希望能够获取每行中每个时区的当前时间(只有时间,没有日期)。 时区数据如下:+05:00、-03:00 等等 这就是我尝试
我正在尝试了解 session 时区在 PostgreSQL 中的工作原理。 当我将 session 时区设置为“01:00”时,返回日期为“-01:00”。符号总是颠倒的,我不理解这种行为。 这怎么
我写入我的 mongoDB 数据库,例如开始日期,开始日期始终是一周中的星期一(我使用并且我在欧洲中部时区)。 "startDate" : ISODate("2014-12-28T23:00:00Z"
是否有详尽的 MySQL 时区列表? 似乎 MySQL 设置中 time_zone 的有效值取决于主机操作系统,但我一直无法找到可能值的列表。 我需要时间显示卡尔加里本地时间。 最佳答案 默认情况下,
我已经在 config/app.php 中配置了时区至 Europe/Lisbon . 如果我做 return date_default_timezone_get(); , 返回 Europe/Lis
我在整个脚本中广泛使用 unix 时间,通常我将时区设置如下: date_default_timezone_set('Europe/London'); 这一直工作正常,直到上周末时钟回退一个小时,我怀
我有一个字符串,2013-10-07T23:59:51.205-07:00,想将其转换为 Java 日期对象。我收到解析错误。 date = new SimpleDateFormat("yyyy-MM
如何更改日志文件名中的 log4j 时区? 我的 log4j.xml 文件:
在JAVA中,如何确定所有日期都作为GMT日期返回? 例如,即使我尝试使用GMT语言环境强制使用DateFormat,它也会应用某种逻辑来检索BST日期。 public static void mai
好吧,这是我遇到过的最奇怪的错误之一! 首先:我不是 Python 程序员,该脚本是由 friend 编写的(我认为他的大部分内容来自示例)。 该脚本的用途:将日历信息(CET 时间)从 XML 文件
我们有一个网站,目前在某个时间(由用户选择)运行拍卖,这一切都工作正常,因为当服务器到达该时间时,拍卖就会开始。然后我们必须添加时区,具体取决于用户居住的地方。然后,根据所选的下拉菜单,这将增加或减少
我陷入了这个逻辑。我正在开发一个消息调度项目,我的客户来自不同的国家。让我们考虑一下亚洲(不遵守夏令时)和美国/纽约(遵守夏令时)。 现在,我正在编写以下查询来获取距当前时间 10 分钟间隔内的时间表
我尝试返回特定时区的值,但我遇到了一些奇怪的响应行为: SELECT created_at AT TIME ZONE 'US/Pacific' - created_at, NOW() A
我正在尝试将不带时区值(例如“31.05.2015”)的字符串转换为 NSDate。 我的 DateFormatter 有什么问题?它返回日期 2015-05-31 22:00:00 +0000当我尝
我已经成功地将我的本地 tzinfo 导入到 mysql 中,我几乎已经了解了关于如何存储和使用这些数据的所有细节,除了一个字段。有一个名为 Transition_type_id 的数字字段(在表 t
为什么以下行返回的 TimeZone 显示不正确的时间: TimeZone.getTimeZone("America/Ottawa") 现在显示晚上 10:26 [亚特兰大时间现在是 6:26,Ott
我是一名优秀的程序员,十分优秀!