服务器时区java转换
好的,所以我几乎尝试了一切。我敢打赌这确实很简单,但我似乎无法掌握它。
服务器向我发送时间,即纪元。然而,当我将其放入日期对象时,它似乎会自动选择时区,并将服务器时间添加+3。因此,如果 GMT 时间为 00.00,则它会显示为 03.00。
我还需要添加自己的时区。假设纪元时间再次为 00.00,添加时区后它应该显示为 10.00。
任何帮助将不胜感激。谢谢
Ok, so I've pretty much tried everything. I bet it's something really simple but I can't seem to get a hold of it.
The server sends me the time, which is epoch. However when I put this into a date object it seems to automatically pick up the time zone and it adds +3 to the server time. So if the gmt time is 00.00, it says its 03.00.
I also need to add a timezone of my own. Let's say the epoch time is 00.00 again, it should read 10.00 after I add the timezone.
any help would be much appreciated. Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“似乎添加了” - 我怀疑您正在使用
Date.toString()
它确实使用了本地时区。不过,Date
对象本身实际上是 UTC。使用DateFormat
执行到字符串的转换,并且您可以指定要使用的时区。您可能还需要使用日历
- 这取决于您想要做什么。(或者,首先使用 Joda Time,这是一个更好的 API。它可能有点庞大不过,对于你的 Android 项目,如果某个地方有一个“Joda Time lite”项目正好适合这类事情,我不会感到惊讶......)
编辑:快速示例,尽管并不完全清楚你需要什么......
"It seems to add" - I suspect you're using
Date.toString()
which does indeed use the local time zone. TheDate
object itself is effectively in UTC though. UseDateFormat
to perform the conversion to a string instead, and you can specify which time zone to use. You may also need to useCalendar
- it depends what you're trying to do.(Alternatively, use Joda Time in the first place, which is a better API. It may be a little bulky for your Android project though. I wouldn't be surprised if there were a "Joda Time lite" project around somewhere for precisely this sort of thing...)
EDIT: Quick sample, although it's not entirely clear what you need...
java.time
java.util
Date-Time API 及其格式化 APISimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到 现代日期时间 API*。另外,下面引用的是Joda-Time主页的通知< /a>:
使用现代日期时间 API
java.time
的解决方案:输出:
在线演示
从跟踪:日期时间。
你的代码出了什么问题?
java.util.Date
对象仅表示时间轴上的一个时刻 - 自 UNIX 纪元(1970 年 1 月 1 日,00:00:00 GMT)以来的毫秒数的包装器。由于它不包含任何时区信息,因此其toString
函数应用 JVM 时区以返回格式为EEE MMM dd HH:mm 的
,源自此毫秒值。要获取不同格式和时区的String
:sszz yyyyjava.util.Date
对象的String
表示形式,您需要使用具有所需格式的SimpleDateFormat
和适用的时区,例如输出:
在线演示
* 无论出于何种原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport 将大部分 java.time 功能向后移植到 Java 6 和 Java 6 7. 如果您正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖提供 Java 8+ API 和 如何在Android项目中使用ThreeTenABP。
java.time
The
java.util
Date-Time API and their formatting API,SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.Also, quoted below is a notice from the home page of Joda-Time:
Solution using
java.time
, the modern Date-Time API:Output:
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
What went wrong with your code?
A
java.util.Date
object simply represents an instant on the timeline — a wrapper around the number of milliseconds since the UNIX epoch (January 1, 1970, 00:00:00 GMT). Since it does not hold any timezone information, itstoString
function applies the JVM's timezone to return aString
in the format,EEE MMM dd HH:mm:ss zzz yyyy
, derived from this milliseconds value. To get theString
representation of thejava.util.Date
object in a different format and timezone, you need to useSimpleDateFormat
with the desired format and the applicable timezone e.g.Output:
ONLINE DEMO
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.