如何从日历获取 UTC 时间戳?
在 Java 中,当我使用 Calendar.getInstance();
时,我会得到当前时区的 Calendar
对象。但是 java.sql.Timestamp 通常存储在 UTC 时间而不是本地时间。那么如何从日历实例中获取 UTC 时间呢?
DateFormat df = DateFormat.getTimeInstance();
Calendar cal = Calendar.getInstance();
Timestamp time = new Timestamp(cal.getTimeInMillis());
// Prints local time
System.out.println(df.format(new Date(time.getTime())));
// Printe local time, but I want UTT Time
System.out.println("timestamp: " + time);
In Java when I use Calendar.getInstance();
I get a Calendar
object for the current Timezone. But java.sql.Timestamp
is usually stored in UTC Time and not in local time. So how can I get the UTC Time from a Calendar instance?
DateFormat df = DateFormat.getTimeInstance();
Calendar cal = Calendar.getInstance();
Timestamp time = new Timestamp(cal.getTimeInMillis());
// Prints local time
System.out.println(df.format(new Date(time.getTime())));
// Printe local time, but I want UTT Time
System.out.println("timestamp: " + time);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码已经完全按照您的要求进行操作。
Timestamp
(以及Date
)没有时区信息,应始终包含 GMT 时间戳(这是Calendar.getTimeInMillis()
返回的内容) )。您看到打印的本地时间的原因是
DateFormat
工厂方法以及Timestamp.toString()
隐式使用系统时区。Your code is already doing exactly what you want.
Timestamp
(as well asDate
) does not have timezone information and should always contain a GMT timestamp (which ist whatCalendar.getTimeInMillis()
returns).The reson why you see local time printed is that the
DateFormat
factory methods as well asTimestamp.toString()
implicitly use the system timezone.tl;dr
避免遗留日期时间类
您正在使用麻烦的旧日期时间类,现在已被 java.time 类取代。无需使用
DateFormat
、Calendar
或Timestamp
。如果您必须与
日历
从尚未更新的旧代码到 java.time,使用添加到旧类的新方法进行转换。提取过时的
java.util.Date
,并转换为Instant
。使用 java.time 以
Instant
形式获取当前时刻。即时
类代表 UTC 中时间轴上的时刻,分辨率为 纳秒(最多九 (9) 位小数)。数据库
使用 JDBC 4.2 及更高版本,您可以直接与数据库交换
Instant
对象。因此不需要过时的java.sql.Timestamp
。和检索:
从纪元开始计数
显然您需要自纪元参考日期
1970-01-01T00:00:00Z
。您可以从Instant
中提取该号码。但要小心数据丢失,因为Instant
的分辨率为纳秒,您将其截断为毫秒。关于 java.time
java.time< /a> 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,<代码>YearQuarter,以及更多 。tl;dr
Avoid legacy date-time classes
You are using troublesome old date-time classes now supplanted by the java.time classes. No need to be using
DateFormat
,Calendar
, orTimestamp
.If you must interopt with a
Calendar
from old code not yet updated to java.time, convert using new methods added to the old classes. Extract an obsoletejava.util.Date
, and convert toInstant
.Using java.time
Get the current moment as an
Instant
. TheInstant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).Database
With JDBC 4.2 and later, you can directly exchange a
Instant
object with your database. So no need for the obsoletejava.sql.Timestamp
.And retrieval:
Count from epoch
Apparently you want a number of milliseconds since the epoch reference date of
1970-01-01T00:00:00Z
. You can extract that number from anInstant
. But beware of data loss, as aInstant
has a resolution of nanoseconds which you will be truncating to milliseconds.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.以下片段应该满足您的要求:
Following snippet should satisfy your requirements:
当您调用
Calendar.getTime()
时,它会立即为您提供一个没有相关时区的值 - 或者您可以将其视为 UTC 格式 /em> 日历代表的。因此,如果(例如)日历时区的上午 9 点,则可能是 UTC 下午 5 点。现在,当您调用
时,
(无论是隐式的还是显式的) - 但不要让它欺骗您,让您认为时区是对象本身数据的一部分。java.util.Date
(我想Timestamp
)将在系统默认时区格式化 toString()您需要非常清楚确切您想要实现的目标 - 然后您可能会发现Joda Time 让您可以比内置库更清楚地在代码中表达这一点。
When you call
Calendar.getTime()
, that will give you a value which doesn't have a related time zone - or you could think of it as being in UTC - for the instant that the calendar represents. So if it was (say) 9am in the calendar's time zone, it could be 5pm UTC.Now
java.util.Date
(and I'd imagineTimestamp
) will be formatted in the system default time zone when you calltoString()
(whether implicitly or explicitly) - but don't let that fool you into thinking that the time zone is part of the data within the object itself.You need to be very clear about exactly what you're trying to achieve - and then you'll probably find that Joda Time lets you express that in code more clearly than the built-in libraries do.