1
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?

【Kotlin】1年後・1年前の日付を取得する方法

1
Posted at

日付操作を行う際、「1年後」や「1年前」を取得したいケースはよくあります。
シンプルに日付を加減算する方法を紹介します。

.kt
import java.time.LocalDate 

val oneYearLater = LocalDate.now().plusYears(1) 

plusYears(1)を使うことで、現在日付から1年後の日付を簡単に取得できます。

.kt
import java.time.LocalDate 

val oneYearAgo = LocalDate.now().minusYears(1)

minusYears(1)を使うことで、1年前の日付を取得できます。

シンプルに書けるので、日付計算はLocalDateを使うのがおすすめです。

1
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
1
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?