如何处理国际时间?
我建立了一个新网站。但主机在美国。我不在美国。 我需要获取网站页面上的时间来与一个本地变量进行比较。 但由于时差的原因,有8个小时的差异。如何解决这个问题?
我的代码
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
java.util.Date currentTime = new java.util.Date();
String dateString = formatter.format(currentTime); `
如何修改这些代码?
i build a new website.but the host is in USA.i am not in USA.
i need get the time on the website page to compare with one local Variable.
But because of time difference,it has 8 hous difference。how to solve this problom?
my code
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
java.util.Date currentTime = new java.util.Date();
String dateString = formatter.format(currentTime); `
how to revise these code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
java.util.Date
不支持时区。您应该通过调用formatter.setTimeZone(tz)
将TimeZone
传递给格式化程序。joda-time 被认为是处理日期时更好的选择。请注意,为了格式化,可以使用
Date
,但一般建议在 i18n 方面不要依赖它。 (请注意那里有许多已弃用的方法)然后让每个用户设置他的时区。理想情况下,建议/假设基于他的浏览器区域设置的时区。 请参阅此处
并始终将日期存储在固定时区 - 最好是 GMT/UTC。
java.util.Date
does not support timezones. You should pass theTimeZone
to the formatter instead, by callingformatter.setTimeZone(tz)
.joda-time is considered a better choice when working with dates. Note that for the sake of formatting it is fine to use
Date
, but it is a general advise not to rely on it when it comes to i18n. (Note the many deprecated methods there)Then make each user set his timezone. Ideally suggest / assume the timezone based on his browser locale. See here
And always store the dates in a fixed timezone - preferably GMT/UTC.
为了处理时区,Java 包含 Olson 时区数据库。在数据库中查找与您处于同一时区的城市。
首先,您需要获取
TimeZone object
为您想要的时区。然后,获取一个
Calendar
对象 与当前日期和时间(或您希望使用的日期和时间)。您可以使用 SimpleDateFormat 对象对其进行格式化。不过,如果你经常操纵时间,就像博佐说的那样,那就选择乔达时间。 Java 日期/时间系统令人困惑且设计相当糟糕。
In order to handle timezones, Java includes the Olson timezone database. Find the city in the database that is in the same time zone as you are.
First, you need to get a
TimeZone
object for the timezone you want. Then, get aCalendar
object with the current date and time (or the date and time you wish to use). You can format that with a SimpleDateFormat object.Though if you're doing a lot of time manipulation, like Bozho says, go for joda-time. The Java date/time system is confusing and rather poorly designed.
在这种情况下,我总是更改 Linux 中的时区:
这也很有帮助例如,用于读取日志文件(我总是看到我的当地时间,而不是每次需要深入研究它们时都计算它)
In such cases I always change timezone in Linux:
It also can be helpful for reading log files for example (I always see my local time instead of calculating it each time when I need to dig into them)
我认为您需要使用日历(它们通常比日期对象更有用)。如果您创建一个日历,并使用您的区域设置和时区进行初始化,则可以使用您创建的日期执行
calendar.setDate()
操作。如果您使用输入的字段创建另一个日历对象,则可以在两个日历对象之间进行比较。I think you need to use a Calendar (they are more useful generally than just Date objects). If you create a Calendar, initialised with your locale and timezone, you can do
calendar.setDate()
using the date you created. If you create another Calendar object with the fields that were entered, you can then do comparisons between the two Calendar objects.