2

我有一个日历对象 [ localDate ],它位于美国东部标准时间:比如 11 月 6 日 15:34... 我将 timeZone 设置为 GMT+5:30... 现在当我这样做时Calendar.HOUR_OF_DAY它返回我 2...我知道是完美的.. 因为它是 15:34 + 5 小时到格林威治标准时间,然后 +5:30 到时区.. 这只是意味着.. 26:04 是 2 of 7th 。

但是,日期仍然保持为 11 月 6 日......并且localDate.getTime()仍然返回 11 月 6 日.. 甚至当我打印 localDate.. 它显示时区为 +5:30,但日期和其他一切仍然是原始本地时间..[即 11 月 6 日]

我简直无法理解为什么会这样......

编辑 ::

所以我知道我不需要更改日期和时区。仅更改日期显示方式以适合该位置,并且可以使用已设置的时区来完成。

4

3 回答 3

6

localDate.getTime() 返回 a java.util.Date,它是自固定点以来的原始时间量。时区仅影响时间点的人类可读表示。

15:34 Nov 6th UTC - 502:04 Nov 7th UTC + 5:30

两者都是绝对时间的完全相同的点。这只是人类描述同一时刻的两种不同方式。

因此更改日历上的时区对返回的值没有影响getTime()

于 2013-11-06T22:19:58.987 回答
3

Date对象没有时区 -Date对象代表“绝对”时间。当您打印一个Date对象时(通过隐式或显式调用toString()它):

Date date = ...;
System.out.println(date);

然后它将使用一些默认格式进行格式化,该格式将显示您当地时区的日期 - 无论您是否从设置为不同时区的Date对象获取对象。Calendar

如果要Date在不同的时区显示,请使用DateFormat对象并设置要在该对象上显示日期的时区:

Date date = ...;

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
df.setTimeZone(TimeZone.getTimeZone("UTC"));  // For example, UTC

// Prints 'date' in UTC
System.out.println(df.format(date));
于 2013-11-06T22:26:29.870 回答
2

问题不明确

你的问题令人困惑。

您可能对时区的含义感到困惑。正如 Jesper 和 Affe 的正确答案所说,改变时区并不会改变宇宙时间线上的点。假设美国纽约的 Bob 给冰岛雷克雅未克的 Susan 打电话。冰岛全年使用 UTC 作为他们的时区。Bob 和 Susan 在同一时间互相交谈。但是,如果 Bob 查看他墙上的时钟,他会看到显示的时间比 Susan 墙上的时钟早 5 小时。纽约比 UTC (-5:00) 晚 5 小时。

您的问题的另一个问题:您还谈到了 5:00 时区偏移以及 5:30 偏移。它是哪一个?或者您是否有两个时区以及 GMT/UTC?

乔达时间

我将尝试为您提供一些示例源代码。

Joda-Time库使日期时间工作更容易。

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

// Use time zone names rather than explicit number of hours offset is generally a good thing.
// Affords Joda-Time an opportunity to make adjustments such as Daylight Saving Time (DST).

// Question asked:
// (1) Start with a US east coast time (Standard offset of -5:00) of November 6, 2013 15:34.
// (2) Move that datetime to UTC (GMT) time zone (no offset).
// (3) Move that datetime to Kolkata (formerly known as Calcutta) India time zone (Standard offset of +05:30).

// Joda-Time has deprecated use of 3-letter time zone codes because of their inconsistency. Use other identifier for zone.
// Time Zone list: http://joda-time.sourceforge.net/timezones.html  (Possibly out-dated, read note on that page)
org.joda.time.DateTimeZone newyorkTimeZone = org.joda.time.DateTimeZone.forID( "America/New_York" );
org.joda.time.DateTimeZone kolkataTimeZone = org.joda.time.DateTimeZone.forID( "Asia/Kolkata" );

// Question calls for: EST Nov 6, 15:34 (Standard offset of -5:00).
// This DateTime constructor calls for passing: year, month, day, time zone.
org.joda.time.DateTime dateTimeInNewYork = new org.joda.time.DateTime( 2013, org.joda.time.DateTimeConstants.NOVEMBER, 6, 15, 34, newyorkTimeZone );
// Move to UTC time zone (no offset).
org.joda.time.DateTime dateTimeUtc = dateTimeInNewYork.toDateTime( org.joda.time.DateTimeZone.UTC );
// Move to Kolkata IN time zone (Standard offlet of +05:30).
org.joda.time.DateTime dateTimeInKolkata = dateTimeUtc.toDateTime( kolkataTimeZone ); // Or invoke this method on dateTimeInNewYork, does not matter which.

// All three of these date-time objects represent the same moment in the time-line of the Universe,
// but present themselves with different time-zone offsets.
System.out.println( "dateTimeInNewYork: " + dateTimeInNewYork );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTimeInKolkata: " + dateTimeInKolkata );

运行时……</p>

dateTimeInNewYork: 2013-11-06T15:34:00.000-05:00
dateTimeUtc: 2013-11-06T20:34:00.000Z
dateTimeInKolkata: 2013-11-07T02:04:00.000+05:30

在此处输入图像描述

关于 Joda-Time……</p>

// 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
于 2013-11-23T21:08:39.513 回答