هل أحتاج إلى تغيير التاريخ مع المنطقة الزمنية

StackOverflow https://stackoverflow.com/questions/19823789

  •  04-07-2022
  •  | 
  •  

سؤال

لدي كائن تقويم [localdate] وهو في EST: قل 6 نوفمبر 15:34 ... وقمت بتعيين المنطقة الزمنية على GMT+5: 30 ... الآن عندما أفعل Calendar.HOUR_OF_DAY يعيدني 2 ... وهو ما أعرفه مثالي .. منذ 15:34 + 5 ساعات إلى GMT ثم +5: 30 إلى المنطقة الزمنية .. وهو ما يعني فقط .. 26:04 وهو 2 من 7.

ومع ذلك ، لا يزال التاريخ يبقى في 6 نوفمبر ... و localDate.getTime() لا يزال يعود في 6 نوفمبر .. وحتى عندما أقوم بطباعة المحلي .. يظهر المنطقة الزمنية +5: 30 ، ولكن اليوم وكل شيء آخر لا يزال بالتوقيت المحلي الأصلي .. [أي 6 نوفمبر

أنا ببساطة غير قادر على فهم سبب ذلك ...

يحرر ::

لذلك أفهم أنني لست بحاجة إلى تغيير التاريخ إلى جانب المنطقة الزمنية .. فقط تغيير الطريقة التي يتم عرضها مناسبة لهذا الموقع ويمكن القيام بذلك باستخدام المنطقة الزمنية التي تم تعيينها.

لا يوجد حل صحيح

نصائح أخرى

localDate.getTime() إرجاع أ java.util.Date وهي كمية من الوقت الخام منذ نقطة ثابتة. تؤثر المناطق الزمنية فقط على التمثيل البشري القابل للقراءة للنقطة الزمنية.

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

كلاهما نفس النقطة بالضبط في الوقت المطلق. إنها مجرد طريقتان بشريتين مختلفتين لوصف نفس اللحظة.

لذا فإن تغيير المنطقة الزمنية في التقويم ليس له أي تأثير على القيمة التي تم إرجاعها بواسطة getTime()

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

السؤال غير واضح

سؤالك محير.

قد تكون مرتبكًا بشأن معنى المناطق الزمنية. كما قال الإجابات الصحيحة من قبل 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 - 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