Javaの日時データ操作
2021-11-14 (日) 12:02:16
Javaの日時データ操作 †
日付データ †
- Calendar オブジェクト
Calendar cal = Calendar.getInstance(); cal.get(Calendar.YEAR) cal.get(Calendar.MONTH) cal.get(Calendar.DATE)
日付データの比較 †
- equal(Object)
- after(Object)
- before(Object)
日時データ †
- クラスの種類
- LocalDateTime
- デフォルト
- OffsetDateTime
- UTC(グリニッジ)からの時差を持つ
- ZonedDateTime
- タイムゾーンを持つ
- LocalDateTime
LocalDateTime datetime = LocalDateTime(year, month, date, hour, minute, second) OffsetDateTime datetime = OffsetDateTime(year, month, date, hour, minute, second, zone) ZonedDateTime datetime = ZonedDateTime(year, month, date, hour, minute, second, nanoSecond, zone)
- 日付の要素を取得
- getYear
- getMonth
- getDayOfMonth
- getHour
- getMinute
- getSecond
- フォーマット
- format(formatter)
- Dateオブジェクトを日時オブジェクトに変換
- ofInstance
LocalDateTime datetime = LocalDateTime.ofInstant(date.toInstant(), zoneId)
- ofInstance
- 日時オブジェクトをDateオブジェクトに変換
- toInstant
Date date = Date.from(localDt.toInstant(zoneOffset))
- toInstant