Java - 存储 GMT 时间

发布于 2024-07-19 03:37:41 字数 174 浏览 10 评论 0原文

  1. 我的服务器有 GMT+7,所以如果我移动到另一台有另一个 GMT 时区的服务器,数据库中存储的所有日期都会不正确?
  2. 是的,Q1 是正确的,我如何将日期存储在 GMT+0 时区中,并以每个成员选择的自定义 GMT 时区显示它
  3. 如何在 java 中获取 GMT+0 的日期
  1. My server has GMT+7, so if i move to another server has another GMT timezone, all date stored in db will incorrect?
  2. Yes Q1 is correct, how about i will store date in GMT+0 timezone and display it in custom GMT timezone chosen by each member
  3. How i get date with GMT+0 in java

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你的呼吸 2024-07-26 03:37:41

1)引用javadocs,Java毫秒时间戳是

当前时间与 UTC 时间 1970 年 1 月 1 日午夜之间的差异(以毫秒为单位)。

因此,由于时间戳是 UTC/GMT,如果将其存储在数据库中,无论服务器的时区如何,该日期都将是正确的。

2)根据时区格式化时间戳以进行显示将是正确的方法。

3) 创建一个新的 java.util.Date 实例并调用 getTime() 将获得 Java 中 GMT+0 的时间戳。

1) To quote from the javadocs, Java millisecond timestamps are

the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

Therefore, as the timestamp is UTC/GMT, if you store it in the database this date will be correct no matter the time zone of the server.

2) Formatting the timestamp for display depending on the timezone would be the correct approach.

3) Creating a new java.util.Date instance and calling getTime() will get you the timestamp for GMT+0 in Java.

尤怨 2024-07-26 03:37:41

为了添加标记的观点,一旦您拥有 java.util.Date,您可以将其“转换”为任何时区(由用户选择)。 下面是 Groovy 中的代码示例(经过轻微修改后即可在 Java 中运行):

// This is a test to prove that in Java, you can create ONE java.util.Date
// and easily re-display it in different TimeZones
fmtStr = "MM/dd/yyyy hh:mm aa zzz"
myDate = new java.util.Date()
sdf = new java.text.SimpleDateFormat(fmtStr)

// Default TimeZone
println "Date formatted with default TimeZone: " + sdf.format(myDate)

// GMT
sdf.setTimeZone(TimeZone.getTimeZone("GMT"))
println "Date formatted with GMT TimeZone: " + sdf.format(myDate)

// America/New_York
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"))
println "Date formatted with America/New_York TimeZone: " + sdf.format(myDate)

// PST
sdf.setTimeZone(TimeZone.getTimeZone("PST"))
println "Date formatted with PST TimeZone: " + sdf.format(myDate)

To add to mark's point, once you have a java.util.Date, you can "convert" it to any Timezone (as chosen by the User). Here's a code sample in Groovy (will work in Java with slight mods):

// This is a test to prove that in Java, you can create ONE java.util.Date
// and easily re-display it in different TimeZones
fmtStr = "MM/dd/yyyy hh:mm aa zzz"
myDate = new java.util.Date()
sdf = new java.text.SimpleDateFormat(fmtStr)

// Default TimeZone
println "Date formatted with default TimeZone: " + sdf.format(myDate)

// GMT
sdf.setTimeZone(TimeZone.getTimeZone("GMT"))
println "Date formatted with GMT TimeZone: " + sdf.format(myDate)

// America/New_York
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"))
println "Date formatted with America/New_York TimeZone: " + sdf.format(myDate)

// PST
sdf.setTimeZone(TimeZone.getTimeZone("PST"))
println "Date formatted with PST TimeZone: " + sdf.format(myDate)
迷路的信 2024-07-26 03:37:41

默认情况下,当您在 java 中获取当前时区的日期时,您可以使用 TimeZone 类来获取系统时间运行所在的时区。大多数数据库支持可以使用时区存储日期,这可能是正确的方法执行此操作,但具体格式取决于您使用的数据库。

By default when you get a date in java its in your current timezone, you can use the TimeZone class to get the timezone your system time is running in. Most databases supports that dates can be stored WITH timezone, which probably would be the right way to do this, but exactly what the format is can be dependant on which database you use.

蓬勃野心 2024-07-26 03:37:41

我建议您仅使用 Date 作为“持有者”,并建议您使用 Calendar 实例。 要获取 GMT 时区的日历,只需使用以下方法:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+00"))

当您处理时间和日期时,日历将提供很多好处。 例如,添加一个小时很简单:

cal.add(Calendar.HOUR, 1);

最后,当您需要一个日期时,只需使用:

Date date = cal.getTime();

I would suggest that you use Date only as a "holder" and propose that you use Calendar instances instead. To get a Calendar in the GMT time zone just use this:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+00"))

A Calendar will provide a great deal of benefit when you work with times and dates. For instance, adding an hour to this is simple:

cal.add(Calendar.HOUR, 1);

Lastly, when you need a Date you just use this:

Date date = cal.getTime();
那些过往 2024-07-26 03:37:41

对于第三季度,考虑使用 ZonedDateTime 类。

For Q3, consider using ZonedDateTime class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文