java.util.Dateを「java.time」タイプに変換しますか?

バジルブルク:

私が持っているjava.util.Dateオブジェクト、またはjava.util.Calendarオブジェクトを。それをjava.timeフレームワークで正しいタイプに変換するにはどうすればよいですか?

これで、java.time型を使用してビジネスロジックの大部分を実行する必要があると聞きました。java.time用にまだ更新されていない古いコードを使用する場合、前後に変換できる必要があります。どのような種類がにマップjava.util.Datejava.util.Calendar

バジルブルク:

はい、できる限りjava.timeフレームワークを使用する必要があります

古い日時クラスを避ける

古い日時を含むクラスjava.util.Datejava.util.Calendarおよびjava.text.SimpleTextFormatそのようなものであることが判明している悪い混乱、設計されており、面倒できる限り避けてください。ただし、これらの古い型と相互運用する必要がある場合は、古いものと新しいものの間で変換できます。

古い日付クラスと新しい日付時刻クラスの間を行き来するように方向付けるために、多少単純化しすぎた基本的な概要について読みます。

java.time

java.timeのフレームワークは以下のように定義されたJSR 310、高成功に触発ジョダタイムライブラリ、およびによって拡張ThreeTen-エクストラプロジェクト。機能の大部分は、ThreeTen-BackportプロジェクトでJava 6&7にバックポートされました。さらに、ThreeTenABPプロジェクトでAndroidにさらに適応しました

どのjava.timeタイプが一致しjava.util.Dateますか?まあ、java.util.Dateオブジェクトは基本的に、日付と時刻の組み合わせであるUTCのタイムライン上の瞬間を表します。これをjava.timeのいくつかの型のいずれかに変換できます。それぞれについて以下で説明します。変換を容易にするために、いくつかの新しいメソッドが古い日時クラスに追加されていることに注意してください。

java.util.Date/.CalendarからZonedDateTime(またはOffsetDateTime)を介して3つの<code> Local…</ code>タイプに変換する図

Instant

The building block in java.time is an Instant, a moment on the timeline in UTC with a resolution of nanoseconds.

Generally you should do much of your business logic in UTC. In such work, Instant will be used frequently. Pass around Instant objects, applying a time zone only for presentation to a user. When you do need to apply an offset or time zone, use the types covered further below.

From java.util.Date to Instant

Given that both Instant and java.util.Date are a moment on the timeline in UTC, we can easily move from a java.util.Date to an Instant. The old class has gained a new method, java.util.Date::toInstant.

Instant instant = myUtilDate.toInstant();

You can go the other direction, from an Instant to a java.util.Date. But you may lose information about the fractional second. An Instant tracks nanoseconds, for up to nine digits after the decimal place such as 2016-01-23T12:34:56.123456789Z. Both java.util.Date & .Calendar are limited to milliseconds, for up to three digits after the decimal place such as 2016-01-23T12:34:56.123Z. In this example going from Instant to Date means truncation of the 456789.

java.util.Date myUtilDate = java.util.Date.from(instant);

From java.util.Calendar to Instant

What about a java.util.Calendar instead of a java.util.Date? Internal to the Calendar object the date-time is tracked as a count of milliseconds from the epoch reference date-time of the first moment of 1970 in UTC (1970-01-01T00:00:00.0Z). So this value can be converted easily to an Instant.

Instant instant = myUtilCalendar.toInstant() ;

From java.util.GregorianCalendar to ZonedDateTime

Even better, if your java.util.Calendar object is actually a java.util.GregorianCalendar you can easily go directly to a ZonedDateTime. This approach has the benefit of retaining the embedded time zone information.

Downcast from the interface of Calendar to the concrete class of GregorianCalendar. Then call the toZonedDateTime and from methods to go back and forth.

if (myUtilCalendar instanceof GregorianCalendar) {
    GregorianCalendar gregCal = (GregorianCalendar) myUtilCalendar; // Downcasting from the interface to the concrete class.
    ZonedDateTime zdt = gregCal.toZonedDateTime();  // Create `ZonedDateTime` with same time zone info found in the `GregorianCalendar`
}

Going the other direction…

java.util.Calendar myUtilCalendar = java.util.GregorianCalendar.from(zdt); // Produces an instant of `GregorianCalendar` which implements `Calendar` interface.

As discussed above, beware that you may be losing information about the fraction of a second. The nanoseconds in the java.time type (ZonedDateTime) gets truncated to milliseconds in the .Calendar/.GregorianCalendar.

OffsetDateTime

From an Instant we can apply an offset-from-UTC to move into a wall-clock time for some locality. An offset is a number of hours, and possibly minutes and seconds, ahead of UTC (eastward) or behind UTC (westward). The ZoneOffset class represents this idea. The result is an OffsetDateTime object.

ZoneOffset offset = ZoneOffset.of("-04:00"); 
OffsetDateTime odt = OffsetDateTime.ofInstant(instant, zoneOffset);

You can go the other direction, from an OffsetDateTime to a java.util.Date. Extract an Instant and then proceed as we saw in code above. As discussed above, any nanoseconds get truncated to milliseconds (data loss).

java.util.Date myUtilDate = java.util.Date.from(odt.toInstant());

ZonedDateTime

Better yet, apply a full time zone. A time zone is an offset plus rules for handling anomalies such as Daylight Saving Time (DST).

Applying a ZoneId gets you a ZonedDateTime object. Use a proper time zone name (continent/region). Never use the 3-4 letter abbreviations commonly seen such as EST or IST as they are neither standardized nor unique.

ZoneId zoneId = ZoneId.of("America/Montreal");
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, zoneId);

You can go the other direction, from an ZonedDateTime to a java.util.Date. Extract an Instant and then proceed as we saw in code above. As discussed above, any nanoseconds get truncated to milliseconds (data loss).

java.util.Date myUtilDate = java.util.Date.from( zdt.toInstant() );

And we saw further above that a ZonedDateTime may be converted to a GregorianCalendar.

LocalDate

Sometimes you may want a date-only value, without time-of-day and without time zone. For that, use a java.time.LocalDate object.

See this Question for more discussion, Convert java.util.Date to java.time.LocalDate, especially this Answer written by the main man behind the invention of both Joda-Time and java.time.

The key is to go through a ZonedDateTime (as generated in code above). We need a time zone to determine a date. The date varies around the world, with a new day dawning earlier in the east. For example, after midnight in Paris is a new day while still “yesterday” in Montréal. So while a LocalDate does not contain a time zone, a time zone is required to determine a LocalDate.

LocalDate localDate = zdt.toLocalDate();

Converting in the other direction from LocalDate to a date-time means inventing a time-of-day. You can choose any time-of-day that makes sense in your business scenario. For most people, the first moment of the day makes sense. You may be tempted to hard code that first moment as the time 00:00:00.0. In some time zones, that time may not be valid as the first moment because of Daylight Saving Time (DST) or other anomalies. So let java.time determine the correct time with a call to atStartOfDay.

ZonedDateTime zdt = localDate.atStartOfDay(zoneId);

LocalTime

On rare occasion you may want only a time-of-day without a date and without a time zone. This concept is represented by the LocalTime class. As discussed above with LocalDate, we need a time zone to determine a LocalTime even though the LocalTime object does not contain (does not ‘remember’) that time zone. So, again, we go through a ZonedDateTime object obtained from an Instant as seen above.

LocalTime localTime = zdt.toLocalTime();

LocalDateTime

As with the other two Local… types, a LocalDateTime has no time zone nor offset assigned. As such you may rarely use this. It gives you a rough idea of a date-time but is not a point on the timeline. Use this if you mean some general date and some time that might be applied to a time zone.

For example, “Christmas starts this year” would be 2016-12-25T00:00:00.0. Note the lack of any offset or time zone in that textual representation of a LocalDateTime. Christmas starts sooner in Delhi India than it does in Paris France, and later still in Montréal Québec Canada. Applying each of those areas’ time zone would yield a different moment on the timeline.

LocalDateTime ldt = zdt.toLocalDateTime();

ここに画像の説明を入力してください


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.

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

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

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?

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

java.time.LocalDateをjava.util.Dateタイプに変換します

java.util.Dateをjava.util.Dateに変換します。

java.util.Dateをjava.time.LocalDateに変換する

matlabシリアルタイムスタンプをjava.util.Dateに変換します

タイムスタンプ文字列をjava.util.Dateに変換する方法

DStream [java.util.Date]をDStream [String]に変換します

ZonedDateTimeをjava.util.Dateに変換します

java.util.Dateをjava.sql.Dateに変換する方法

java.sql.Dateとjava.util.Dateをorg.joda.time.LocalDateに変換する

java.util.DateをJava8に変換する方法java.time.YearMonth

タイプ「java.lang.String」の値を必要なタイプ「java.util.Date」に変換する方法は?

Java8で文字列をjava.util.Dateに変換します

アップキャストでJava.sql.dateをJava.util.dateに変換しても安全ですか?

java.util.GregorianCalenderをjava.lang.Integerに変換しますか?

タイプjava.util.Dateの値を文字列に変換できませんでした

GETリクエストを送信する際に必要な型「java.util.Date」にタイプ「はjava.lang.String型」の変換値に失敗しました。

文字列をjavaでjava.util.date形式に変換する

日時をsqlserverからjava.util.Dateに変換する方法

文字列の一部をjava.util.Dateに変換します

Java.util.Dateをcom.datastax.driver.core.LocalDateエラーに変換します

Swaggerでタイプ「java.lang.String」の値を必要なタイプ「java.util.Date」に変換できませんでした

java.util.DateからXMLGregorianCalendar

java.util.Dateをタイムスタンプの現在の時刻に変換する方法

ZoneId.systemDefault()を使用してjava.util.Dateをjava.time.LocalDateTimeに変換する

Joda LocalDateをjava.util.Dateに変換する方法

java.util.Dateを文字列に変換する

文字列をjava.util.Dateに変換する

文字列をjava.util.dateに変換する

Java.Util.DateをSystem.DateTimeに変換する方法

TOP 一覧

  1. 1

    モーダルダイアログを自動的に閉じる-サーバーコードが完了したら、Googleスプレッドシートのダイアログを閉じます

  2. 2

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  3. 3

    CSSのみを使用して三角形のアニメーションを作成する方法

  4. 4

    ドロップダウンリストで選択したアイテムのQComboBoxスタイル

  5. 5

    ZScalerと証明書の問題により、Dockerを使用できません

  6. 6

    PyCharmリモートインタープリターはプロジェクトタブにサイトパッケージのコンテンツを表示しません

  7. 7

    Windows 10でのUSB入力デバイスの挿入/取り外しの検出

  8. 8

    Excel - count multiple words per cell in a range of cells

  9. 9

    PictureBoxで画像のブレンドを無効にする

  10. 10

    Windows 10 Pro 1709を1803、1809、または1903に更新しますか?

  11. 11

    スタート画面にシャットダウンタイルを追加するにはどうすればよいですか?

  12. 12

    Python / SciPyのピーク検出アルゴリズム

  13. 13

    Luaの文字列から特定の特殊文字を削除するにはどうすればよいですか?

  14. 14

    Pythonを使用して、リストからデータを読み取り、特定の値をElasticsearchにインデックス付けするにはどうすればよいですか?

  15. 15

    LinuxでPySide2(Qt for Python)をインストールするQt Designerはどこにありますか?

  16. 16

    goormIDEは、ターミナルがロードするデフォルトプロジェクトを変更します

  17. 17

    QGISとPostGIS(マップポイント(米国の地図上にraduisを使用した緯度と経度)

  18. 18

    MLでのデータ前処理の背後にある直感

  19. 19

    ターミナルから「入力ソースの変更」ショートカットを設定する

  20. 20

    パンダは異なる名前の列に追加します

  21. 21

    同じクラスの異なるバージョンを使用したクラスローディング:java.lang.LinkageError:名前の重複クラス定義を試行しました

ホットタグ

アーカイブ