在 Java 中将日期从 UTC 转换为 PST

发布于 2024-11-08 07:04:34 字数 431 浏览 0 评论 0 原文

我需要将日期从 Google App Engine 本地服务器时区转换为 Java 中的太平洋时间。

我尝试使用

Calendar calstart =
Calendar.getInstance();

calstart.setTimeZone(TimeZone.getTimeZone("PST"));
//calstart.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

Date startTime = calstart.getTime();

,但这给了我不正确的时间(大约下午 4 点,而实际太平洋标准时间是晚上 10 点)。还尝试了注释行 America/Los_Angeles 但在 GAE 服务器上给出了错误的时间。

有什么想法/建议吗?

I need to convert date from Google App Engine local server time zone to pacific time in Java.

I tried using

Calendar calstart =
Calendar.getInstance();

calstart.setTimeZone(TimeZone.getTimeZone("PST"));
//calstart.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

Date startTime = calstart.getTime();

but this gives me incorrect time (some 4pm when actual PST is 10pm). Also tried commented line America/Los_Angeles but gives incorrect time on GAE server.

Any thoughts/advice?

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

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

发布评论

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

评论(3

铁憨憨 2024-11-15 07:04:34

使用Joda Time,您所需要的只是DateTime.withZone 方法。下面的示例:

public static Date convertJodaTimezone(LocalDateTime date, String srcTz, String destTz) {
    DateTime srcDateTime = date.toDateTime(DateTimeZone.forID(srcTz));
    DateTime dstDateTime = srcDateTime.withZone(DateTimeZone.forID(destTz));
    return dstDateTime.toLocalDateTime().toDateTime().toDate();
}

作为建议,切勿使用默认 API 进行与时间相关的计算。这太糟糕了。 Joda 似乎是最好的替代 API。

Using Joda Time, all you need is the DateTime.withZone method. Example below:

public static Date convertJodaTimezone(LocalDateTime date, String srcTz, String destTz) {
    DateTime srcDateTime = date.toDateTime(DateTimeZone.forID(srcTz));
    DateTime dstDateTime = srcDateTime.withZone(DateTimeZone.forID(destTz));
    return dstDateTime.toLocalDateTime().toDateTime().toDate();
}

As an advice, never use the default API for time-related calculations. It is just awful. Joda seems to be the best replacement API around.

天暗了我发光 2024-11-15 07:04:34

除非您需要进行一些计算,因此您想要格式化此日期以将其显示给最终用户,否则您可以简单地使用 DateFormat:

Date startTime = new Date(); // current date time
TimeZone pstTimeZone = TimeZone.getTimeZone("America/Los_Angeles");
DateFormat formatter = DateFormat.getDateInstance(); // just date, you might want something else
formatter.setTimeZone(pstTimeZone);
String formattedDate = formatter.format(startTime);

但是,如果您确实需要转换日期(这确实很少见),您可能需要使用以下代码片段:

TimeZone pacificTimeZone = TimeZone.getTimeZone("America/Los_Angeles");
long currentTime = new Date().getTime();
long convertedTime = currentTime +
    pacificTimeZone.getOffset(currentTime);

这将为您提供自 1970 年 1 月 1 日以来以 PST 时区表示的毫秒数。您可以使用此信息轻松创建日期对象。
如果您需要经常执行日期计算,您可能需要使用 Apache Commons Lang 的 DateUtils。或者按照 mdrg 建议切换到 JodaTime。

Unless you need that to do some calculations, thus you want to format this date to display it to end user, you may simply use DateFormat:

Date startTime = new Date(); // current date time
TimeZone pstTimeZone = TimeZone.getTimeZone("America/Los_Angeles");
DateFormat formatter = DateFormat.getDateInstance(); // just date, you might want something else
formatter.setTimeZone(pstTimeZone);
String formattedDate = formatter.format(startTime);

However, if you really need to convert dates (which is really rare), you might want to use following code snippet:

TimeZone pacificTimeZone = TimeZone.getTimeZone("America/Los_Angeles");
long currentTime = new Date().getTime();
long convertedTime = currentTime +
    pacificTimeZone.getOffset(currentTime);

This will give you number of milliseconds that passed since January 1st, 1970 in PST TimeZone. You can easily create Date object with this information.
If you need to perform date calculations quite often, you may want to use Apache Commons Lang's DateUtils. Or switch to JodaTime as mdrg suggested.

笛声青案梦长安 2024-11-15 07:04:34

切勿依赖服务器的时区

切勿依赖或依赖服务器或主机 JVM 的当前默认时区。

始终明确指定您想要/预期的时区,作为可选参数传递。

java.time

java.util.Calendar 类现在已被遗留,并被 java.time 类取代。

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

Instant instant = Instant.now();

instant.toString(): 2017-01-19T22:01:21.321Z

当您想通过特定区域的镜头查看那一刻时 挂钟时间,应用 ZoneId 来获取分区日期时间

大陆/地区格式指定正确的时区名称 ,例如 美国/蒙特利尔非洲/卡萨布兰卡,或太平洋/奥克兰。切勿使用 3-4 个字母的缩写,例如 ESTPSTIST,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Los_Angeles" );
ZonedDateTime zdt = instant.atZone( z );

zdt.toString(): 2017-01-19T14:01:21.321-08:00[美国/洛杉矶]



关于java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如java.util.Date, 日历, & SimpleDateFormat

要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310

Joda-Time 项目,现已在 维护模式,建议迁移到java.time< /a> 类。

您可以直接与数据库交换java.time对象。使用符合 JDBC 驱动程序 /jeps/170" rel="nofollow noreferrer">JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类。 Hibernate 5 和 Hibernate 5 JPA 2.2 支持 java.time。

从哪里获取 java.time 类?

Never depend on server’s time zone

Never depend or rely on the server’s or host JVM’s current default time zone.

Always specify your desired/expected time zone explicitly, passed as optional argument.

java.time

The java.util.Calendar class is now legacy, supplanted by the java.time classes.

Get the current moment in UTC. 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();

instant.toString(): 2017-01-19T22:01:21.321Z

When you want to view that moment through the lens of a particular region’s wall-clock time, apply a ZoneId to get a ZonedDateTime.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or PST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Los_Angeles" );
ZonedDateTime zdt = instant.atZone( z );

zdt.toString(): 2017-01-19T14:01:21.321-08:00[America/Los_Angeles]


Table of all date-time types in Java, both modern and legacy


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.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

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