Java - 存储 GMT 时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/847496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:02:02  来源:igfitidea点击:

Java - Store GMT time

javatimezonegmt

提问by StoneHeart

  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
  1. 我的服务器有 GMT+7,所以如果我移动到另一台有另一个 GMT 时区的服务器,存储在 db 中的所有日期都会不正确吗?
  2. 是的 Q1 是正确的,我将日期存储在 GMT+0 时区并在每个成员选择的自定义 GMT 时区中显示它如何
  3. 我如何在 Java 中使用 GMT+0 获取日期

回答by Mark

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

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

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

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

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.

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

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

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

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

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

回答by Vik David

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):

要补充一点,一旦你有了一个 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)

回答by takete.dk

By default when you get a date in java its in your current timezone, you can use the TimeZoneclass 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.

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

回答by Paul Gregtheitroade

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:

我建议您仅将 Date 用作“持有者”,并建议您改用 Calendar 实例。要在 GMT 时区获取日历,只需使用以下命令:

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 date = cal.getTime();

回答by Seed

For Q3, consider using ZonedDateTime class.

对于 Q3,请考虑使用 ZonedDateTime 类。