문제

나는 EST에있는 달력 개체 [LocalDate]가 있습니다 : 11 월 6 일, 15:34 ... 그리고 시간대를 GMT+5 : 30으로 설정했습니다 ... Calendar.HOUR_OF_DAY 그것은 나에게 2 ... 내가 아는 것이 완벽하다.

그러나 날짜는 여전히 11 월 6 일로 유지됩니다 ... localDate.getTime() 여전히 11 월 6 일에 반환됩니다.

나는 단순히 왜 그렇게 이해할 수 없습니다 ...

편집하다 ::

그래서 나는 시간대와 함께 날짜를 변경할 필요가 없다는 것을 이해합니다. 날짜가 해당 위치에 적합하며 설정된 시간대를 사용하여 수행 할 수있는 방법 만 변경합니다.

올바른 솔루션이 없습니다

다른 팁

localDate.getTime() 반환 a java.util.Date 고정 지점 이후의 원시 시간입니다. 시간 존은 시점의 인간 읽기 가능한 표현에만 영향을 미칩니다.

15:34 Nov 6th UTC - 5그리고02:04 Nov 7th UTC + 5:30

절대 시간에 정확히 같은 지점입니다. 그것은 같은 순간을 묘사하는 두 가지 다른 인간 방법 일뿐입니다.

따라서 달력에서 시간대를 변경하면 반환 된 값에 영향을 미치지 않습니다. getTime()

Date 물체에는 시간대가 없습니다 - a Date 객체는 "절대"순간을 나타냅니다. 인쇄 할 때 a 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의 정답이 말했듯이, 이동 시간 구역은 우주의 타임 라인의 지점을 바꾸지 않습니다. 뉴욕의 Bob이 미국의 Reykjavík 아이슬란드에서 Susan에게 전화를한다고 가정 해 봅시다. 아이슬란드는 UTC를 일년 내내 시간대로 사용합니다. 밥과 수잔은 동시에 서로 대화하고 있습니다. 그러나 Bob이 벽의 시계를 보면 Susan의 벽의 시계보다 5 시간 일찍 표시되는 시간이 보입니다. 뉴욕은 UTC (-5 : 00)보다 5 시간 오프셋을 가지고 있습니다.

질문의 또 다른 문제 : 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