手動でJavaで夏時間(DST)のシフト日付を設定する方法

Evandro Pomatti:

私の国では、「11月4日」に「10月21日」から夏時間シフト日付を変更し、私たちはバックエンドでこれを適用する必要があります。

適切なソリューションは、オペレーティングシステムの構成を更新することですが、私たちはそう(レガシー依存関​​係)を行うには制限があります。私たちは、回避策を探しています。

それは、コードを使用すると、プログラムDSTシフト日付を変更することは可能ですか?

GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(0);
gc.set(2018, Calendar.OCTOBER, 21, 0, 0, 0);
gc.setTimeZone(TimeZone.getTimeZone("Brazil/East"));
XMLGregorianCalendar xml = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
System.out.println("XML Date: " + xml.toString());

出力がなければなりません-03:00

XML Date: 2018-10-21T01:00:00.000-02:00
バジルボーク:

OSの無関係の

お使いのオペレーティングシステムの設定は関係ありません。デフォルトでは、ほとんどのJava実装では、起動時に、ホストOSから彼らの初期デフォルトのタイムゾーンを拾います。しかし、定義のタイムゾーンのは、保存されているのJava実装。

Javaタイムゾーンアップデータ

つまり、あなたのJava実装内のタイムゾーン定義を更新する必要があります。ほとんどの実装では、使用のTZデータベースとしても知られているのtzdataを

OracleのブランドのJava実装では、Oracleが提供してタイムゾーンアップデータツールそのランディングページはので、おそらく、あなたのタイムゾーンの変更が含まれている、として-の2018から08日を持っています。しかし、私はあなたが確認するために、より密接に検討をお勧めします。

他の実装では、ベンダーに確認してください。彼らは新鮮なのtzdataを含めるようにJVMの更新版を提供している可能性があります。それとも、彼らはあまりにもアップデータのツールを提供しています。それとも手動でのtzdataファイルを置き換えることができます。

コードとマングリングゾーンを避けます

私は強くあなたがコードの中で自分自身を相殺するために人工的な調整をしようとしないようお勧めします。あなたはおそらく、それは間違って取得します。驚くほどトリッキーと混乱の日付タイムの仕事。

あなたが主張する場合は、まずなどの恐ろしい古いレガシー日時クラス避けるGregorianCalendarCalendar&をDateあなたはまだに更新されていない古いコードと相互運用しなければならない場合、これらは、JSR 310で年前に取って代わられたjava.time、現代の授業にし、古いクラスに追加された新しいメソッドを介したエンド変換であなたの仕事を行います。

現代の使用java.timeの具体的には、クラスを:

  • Instant (UTCでの瞬間のために)
  • OffsetDateTime (しばらく時間 - 分 - 秒のオフセットUTC-からではなく、時間がないゾーンを持ちます)
  • ZonedDateTime (特定の時間帯におけるモーメントのために)

あなたはこれらのクラスを使用して、既存の多くの例や説明についてはスタックオーバーフローを検索することができます。あなたは上の焦点を当てるべきであるOffsetDateTimeZoneOffset(というよりもZoneId)、そしてInstant、あなたは避けなければならないので、ZonedDateTimeあなたは、あなたの知っていればのtzdata時代遅れするファイルを。

同じ瞬間、異なる壁時計時間

ここでは、画像の説明を入力します。

OffsetDateTime::withOffsetSameInstant​

OffsetDateTime odt = OffsetDateTime.parse( "2018-10-21T01:00:00.000-02:00" ) ;
ZoneOffset offset = ZoneOffset.ofHours( -3 ) ;
OffsetDateTime odt2 = odt.withOffsetSameInstant​( offset ) ;  // Same moment, same point on the timeline, different wall-clock time.

)(odt.toString:2018-10-21T01:00〜02:00

)(odt2.toString:2018-10-21T00:00〜03:00

その例では、両方のodtodt2同じ同時瞬間を表し、タイムライン上の同じ点。あなたが抽出した場合Instant(UTCでの値)を、あなたの結果は、同じ瞬間になります。唯一の彼らの壁時計の時間が異なっています。

Instant instant1 = odt.toInstant() ;  // Adjust to UTC.
Instant instant2 = odt2.toInstant() ;
boolean sameMoment = instant1.equals( instant2 ) ; 

)(instant1.toString:2018-10-21T03:00:00Z

)(instant2.toString:2018-10-21T03:00:00Z

sameMoment =真

Z端部には、オフセットから-UTCゼロの、UTCを意味します+00:00Z「ズールー」と発音されます。定義されたISO 8601標準。

異なる瞬間、同じ壁時計時間

ここでは、画像の説明を入力します。

OffsetDateTime::withOffsetSameLocal​

In contrast, you may want to force the time-of-day thereby representing a different moment. For that, use withOffsetSameLocal method. Be very aware that you are changing the meaning of data, you are moving to another point on the timeline.

OffsetDateTime differentMomentButSameTimeOfDay = odt. withOffsetSameLocal( offset ) ;

differentMomentButSameTimeOfDay.toString(): 2018-10-21T01:00-03:00

Extract the instant to see we have a different moment.

Instant differentInstant = differentMomentButSameTimeOfDay.toInstant() ;

differentInstant.toString(): 2018-10-21T04:00:00Z

Notice the 4 AM UTC versus 3 AM UTC seen above. This moment here occurs an hour after the moment above. Two different points on the timeline.

Do not attempt this work until you fully comprehend the concept of points on the timeline, and changing between points being entirely different than adjusting offsets. Practice extensively before doing real work. Half-hearted guessing will land you in a world of hurt and headache.

And, as I suggested above, your time would be much better spent installing updated tzdata files rather than hacking these offsets.

Live code

See all the code above run live at IdeOne.com.

Update tzdata everywhere

For best results, you should be updating the tzdata (or equivalent) in all these various places:

  • Your operating systems
  • Your JVMs
  • Your database engines, such as Postgres
  • Any libraries bundling their own time zone info (ex: Joda-Time)

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

ThreeTen-エクストラプロジェクトでは、追加のクラスでjava.timeを拡張します。このプロジェクトは、java.timeに将来の追加のための試験場です。あなたはここにいくつかの有用なクラスのような見つけることがIntervalYearWeekYearQuarter、および多くを

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=173774&siteId=1