如何从 ISO 8601 字符串获取当前时区的时间?
在我的 sqlite 数据库中,我有一个表,其中包含一个字段 created_at
,该字段将 ISO8601 中的时间存储为 text
。在与该表对应的对象中,我有两个字段String date, String time
用于created_at
属性。现在,在查询此表并创建相应的对象时,我一直致力于将 ISO8601 字符串转换为当前用户时区的日期 (YYYY/MM/DD)、时间 (XX:YY pm)。有人可以帮我吗?我想过使用子字符串来分割字符串,但对于 ISO8601 字符串来说,时区是 UTC。
使用 Instant.now().toString() 将 ISO8601 字符串存储在数据库中
tldr:created_at = ISO8601 time string;需要从中提取当前用户的日期和时间部分。
In my sqlite database I have a table with a field created_at
that stores time in ISO8601 as text
. In the object corresponding to this table I have two fields String date, String time
for the created_at
attribute. Now, while querying for this table and creating a corresponding object I am stuck at converting the ISO8601 string to the current user's timezone's date (YYYY/MM/DD), time (XX:YY pm). Could anyone help me out? I thought of using substring to split the string but then the timezone is UTC for ISO8601 strings.
The ISO8601 string is stored in the database using Instant.now().toString()
tldr: created_at = ISO8601 time string; need to extract date and time portion from it for the current user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
java.Time
捕获UTC中看到的当前时刻。
生成ISO 8601字符串。
该结果字符串末尾的
z
是指零小时秒的偏移量。调整到特定时区。
不幸的是,ISO 8601标准不会像在时区看到的那样定义格式。
ZonedDateTime#ToString
方法使用类似于ISO 8601的格式,但在方括号中附加了时区的名称。这是一个明智的解决方案。您的大部分问题尚不清楚。我猜您想从
ZonedDateTime
中拉出日期和时间部分。但是我不确定使用这样的值查询您的数据库是有意义的。但是,如果您坚持:每个人都可以获得一个ISO 8601字符串。
请注意,您在问题中提到的字符串操作不需要。
Read the Wikipedia page on ISO 8601.
java.time
Capture the current moment as seen in UTC.
Generate ISO 8601 string.
The
Z
on the end of that resulting string means an offset-from-UTC of zero hours-minutes-seconds.Adjust into a particular time zone.
Unfortunately, the ISO 8601 standard does not define a format for a moment as seen in a time zone. The
ZonedDateTime#toString
method uses a format similar to ISO 8601 but appends the name of the time zone in square brackets. This is a smart solution.Much of your Question in not clear. I am guessing you want to pull the date and time portions from a
ZonedDateTime
. But I am not sure using such values to query your database makes sense. But if you insist:You can get an ISO 8601 string for each.
Notice there is no need for the string manipulations you mentioned in your Question.
我认为您可以看到此链接在这里可能会找到一些答案。
但是例如,您可以在此处看到此示例。我有一个字符串格式,然后将其转换为日期对象。
I think that you can see this link here you might find some answers.
But for example, you can see this example here. I have a String format and I transform it into a Date object.