我有一个日历对象[localdate],该对象在est:例如11月6日,15:34 ...我将时区设置为GMT+5:30 ...现在我做了 Calendar.HOUR_OF_DAY 它使我2 ...我知道这是完美的..因为它的15:34 + 5小时到GMT,然后+5:30到TimeZone .. .. 26:04是7th的26:04。

但是,日期仍然保留为11月6日... localDate.getTime() 仍然返回11月6日..即使我打印了当地日期。它显示为+5:30的时区,但是一天和其他一切仍然是原始的当地时间。[IE 11月6日

我根本无法理解为什么...

编辑 ::

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

没有正确的解决方案

其他提示

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

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

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

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

Date 对象没有时区 - a Date 对象代表“绝对”时间的时刻。当您打印 Date 对象(通过隐式或明确调用 toString() 在上面):

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

然后,它将使用一些默认格式进行格式化,该格式将在您的本地时区显示日期 - 无论您是否获得 Date 来自a的对象 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));

问题不清楚

您的问题令人困惑。

您可能会对时区的含义感到困惑。正如Jesper和Affe所说的正确答案所说,改变时区并没有改变宇宙时间线上的点。假设鲍勃在纽约美国打电话给雷克雅未克冰岛的苏珊。冰岛一年四季都将UTC用作时区。鲍勃和苏珊在同一时刻互相交谈。但是,如果鲍勃看着墙上的时钟,他看到一个时间比苏珊墙上的时钟早5小时。纽约在UTC(-5:00)落后五个小时的偏移。

您的问题的另一个问题是:您还谈论5:00时区偏移以及5:30偏移。这是哪个?还是您有两个时区以及GMT/UTC?

乔达时间

我会为您提供一些示例源代码。

乔达时间 库使日期时间的工作更加容易。

// © 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 );

运行…

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

enter image description here

关于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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top