如何从日历获取 UTC 时间戳?

发布于 2024-10-06 07:19:11 字数 516 浏览 3 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(4

万人眼中万个我 2024-10-13 07:19:12

您的代码已经完全按照您的要求进行操作。 Timestamp(以及 Date)没有时区信息,应始终包含 GMT 时间戳(这是 Calendar.getTimeInMillis() 返回的内容) )。

您看到打印的本地时间的原因是 DateFormat 工厂方法以及 Timestamp.toString() 隐式使用系统时区。

Your code is already doing exactly what you want. Timestamp (as well as Date) does not have timezone information and should always contain a GMT timestamp (which ist what Calendar.getTimeInMillis() returns).

The reson why you see local time printed is that the DateFormat factory methods as well as Timestamp.toString() implicitly use the system timezone.

黎歌 2024-10-13 07:19:12

tl;dr

Instant.now() 

避免遗留日期时间类

您正在使用麻烦的旧日期时间类,现在已被 java.time 类取代。无需使用 DateFormatCalendarTimestamp

如果您必须与 日历从尚未更新的旧代码到 java.time,使用添加到旧类的新方法进行转换。提取过时的java.util.Date,并转换为Instant

Instant instant = myCal.getTime().toInstant() ;

使用 java.time 以

Instant 形式获取当前时刻。 即时 类代表 UTC 中时间轴上的时刻,分辨率为 纳秒(最多九 (9) 位小数)。

Instant instant = Instant.now() ;

2017-02-17T03:29:15.725Z

数据库

使用 JDBC 4.2 及更高版本,您可以直接与数据库交换 Instant 对象。因此不需要过时的java.sql.Timestamp

myPreparedStatement.setObject( … , instant ) ;

和检索:

Instant instant = myResultSet.getObject( … , Instant.class ) ;

从纪元开始计数

显然您需要自纪元参考日期 1970-01-01T00:00:00Z。您可以从Instant中提取该号码。但要小心数据丢失,因为 Instant 的分辨率为纳秒,您将其截断为毫秒。

long millis = instant.toEpochMilli() ;

1487302155725


关于 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

Instant.now() 

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, or Timestamp.

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 obsolete java.util.Date, and convert to Instant.

Instant instant = myCal.getTime().toInstant() ;

Using java.time

Get the current moment as an Instant. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.now() ;

2017-02-17T03:29:15.725Z

Database

With JDBC 4.2 and later, you can directly exchange a Instant object with your database. So no need for the obsolete java.sql.Timestamp.

myPreparedStatement.setObject( … , instant ) ;

And retrieval:

Instant instant = myResultSet.getObject( … , Instant.class ) ;

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 an Instant. But beware of data loss, as a Instant has a resolution of nanoseconds which you will be truncating to milliseconds.

long millis = instant.toEpochMilli() ;

1487302155725


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.

别忘他 2024-10-13 07:19:12

以下片段应该满足您的要求:

df.setTimeZone(TimeZone.getTimeZone("UTC"));

Following snippet should satisfy your requirements:

df.setTimeZone(TimeZone.getTimeZone("UTC"));
兰花执着 2024-10-13 07:19:11

当您调用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 imagine Timestamp) will be formatted in the system default time zone when you call toString() (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.

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