基于 TimeZone Java/Groovy 的日期时间转换

2022-01-11 00:00:00 datetime calendar date-format java groovy

我在 MST,我希望我的日期在 PST.我设置了我想要的时区.现在,如果我这样做 c.getTime() 我总是得到我的服务器时间.相反,我想要太平洋日期时间.请帮忙如何获取指定时区的日期时间对象.

I am in MST and I want my Date in PST. I set the timeZone that I want. Now if i do c.getTime() I always get my server time. Instead I want Pacific Date time. Please help How to get the date time Object in the specified timezone.

   Calendar c= Calendar.getInstance();
   TimeZone timezone= TimeZone.getTimeZone("PST"); 
   c.setTimeZone(timezone)

推荐答案

更新: Joda-Time 项目已经被 java.time 类.请参阅其他答案.

UPDATE: The Joda-Time project has been succeeded by the java.time classes. See this other Answer.

(a) 使用 Joda-Time(或 Java 8 中内置的新 JSR 310).甚至不要考虑使用臭名昭著的糟糕 java.util.Date/Calendar.

(a) Use Joda-Time (or new JSR 310 built into Java 8). Don't even think about using the notoriously bad java.util.Date/Calendar.

(b) 你的问题不清楚.您对答案的评论谈论比较,但您在问题中没有提及比较.

(b) Your question is not clear. Your comments on answers talk about comparing, but you say nothing about comparing in your question.

(c) 避免使用 3 个字母的时区缩写.阅读 Joda-Time 文档中关于 TimeZone<的弃用说明/a> 类.

(c) Avoid the use of 3-letter time zone abbreviations. Read note of deprecation in Joda-Time doc for TimeZone class.

(d) 避免使用默认时区.说出你的意思.您计算机的时区可以有意或无意更改.

(d) Avoid default time zone. Say what you mean. The time zone of your computer can change intentionally or not.

(e) 在 StackOverflow 中搜索joda"以获取大量代码片段和示例.

(e) Search StackOverflow for 'joda' for lots of code snippets and examples.

(f) 这里有一些 Joda-Time 示例代码可以帮助您入门.

(f) Here's some Joda-Time example code to get you started.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

// Specify your time zone rather than rely on default.
org.joda.time.DateTimeZone californiaTimeZone = org.joda.time.DateTimeZone.forID( "America/Los_Angeles" );
org.joda.time.DateTimeZone denverTimeZone = org.joda.time.DateTimeZone.forID( "America/Denver" );

org.joda.time.DateTime nowDenver = new org.joda.time.DateTime( denverTimeZone );
org.joda.time.DateTime nowCalifornia = nowDenver.toDateTime( californiaTimeZone );

// Same moment in the Universe’s timeline, but presented in the local context.
System.out.println( "nowDenver: " + nowDenver );
System.out.println( "nowCalifornia: " + nowCalifornia );

运行时……

nowDenver: 2013-11-21T18:12:49.372-07:00
nowCalifornia: 2013-11-21T17:12:49.372-08:00

关于 Joda-Time…

About Joda-Time…

// Joda-Time - The popular alternative to Sun/Oracle's notoriously bad date, time, and calendar classes bundled with Java 7 and earlier.
// http://www.joda.org/joda-time/

// Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8.
// JSR 310 was inspired by Joda-Time but is not directly based on it.
// http://jcp.org/en/jsr/detail?id=310

// By default, Joda-Time produces strings in the standard ISO 8601 format.
// https://en.wikipedia.org/wiki/ISO_8601

// About Daylight Saving Time (DST): https://en.wikipedia.org/wiki/Daylight_saving_time

// Time Zone list: http://joda-time.sourceforge.net/timezones.html

相关文章