0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Javaの日付け型の種類といくつかのメソッドについて

Last updated at Posted at 2022-08-30

日付を扱うのが苦手で毎度混乱するので、整理しました。
(僕自身が業務で多用する4つの型について)

日付け型の種類

Date

・日付けデータをミリ秒まで扱える
  しかし、時分秒をセットするメソッドは非推奨になっている

よく利用するメソッド

・date型インスタンス.toString
→ Date型をString型に変換

・date型インスタンス.toLocalDate()
→ Date型をLocalDate型に変換

・Date.valueOf(LocalDate date)
→ LocalDate型をDate型に変換

Timestamp

・日付けデータをナノ秒まで扱える

よく利用するメソッド

・Timestamp.valueOf(String s)
→ 文字列をTimestamp型に変換

・Timestamp型インスタンス.getTime()
→ Timestamp型のナノ秒単位を丸めこむ

・Timestamp型インスタンス.toString()
→ Timestamp型を文字列に変換

・Timestamp.valueOf(LocalDateTime dateTime)
→ LocalDateTime型をTimestamp型に変換

LocalDate

日付(年-月-日として表示されることが多い)を表す不変の日付/時間オブジェクト

よく利用するメソッド

LocalDate.now()
→ 現在の日付を年月日まで取得

LocalDateTime

日付/時間(年-月-日-時-分-秒として表示されることが多い)を表す不変の日付/時間オブジェクト

よく利用するメソッド

LocalDateTime.now() 
→ 現在の日付を年月日まで取得

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?