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 5 years have passed since last update.

ScalaでEpochTimeからDate型に変換する方法を調べたときの備忘録

Last updated at Posted at 2019-08-10

joda-time

標準のjava.util.Dateクラスの代わりにJoda Timeを使用してみてください。 Joda Timeライブラリには、日付処理のためにはるかに優れたAPIがあります。

scalaで時間操作メモ
https://blog.shibayu36.org/entry/2015/07/27/093406
JodaOrg/joda-time
https://github.com/JodaOrg/joda-time/blob/master/.github/issue_template.md

scala> :paste
// Entering paste mode (ctrl-D to finish)

import org.joda.time.DateTime
val dt = new DateTime(1564441580 * 1000L)
val HH = dt.toString("HH")
val mm = dt.toString("mm")

// Exiting paste mode, now interpreting.

import org.joda.time.DateTime
dt: org.joda.time.DateTime = 2019-07-30T08:06:20.000+09:00
HH: String = 08
mm: String = 06

joda-time -> Java8 Time API

Java SE 8 contains a new date and time library that is the successor to Joda-Time.

Java8の日時APIはとりあえずこれだけ覚えとけ
https://qiita.com/tag1216/items/91a471b33f383981bfaa
Javaで日時を扱う(Java8)
https://qiita.com/kurukurupapa@github/items/f55395758eba03d749c9

scala> :paste
// Entering paste mode (ctrl-D to finish)

import java.time.{Instant, ZonedDateTime, ZoneId}
import java.time.format.DateTimeFormatter

val zdt       = ZonedDateTime.ofInstant(Instant.ofEpochSecond(1564441580), ZoneId.of("Asia/Tokyo"))
val HH        = zdt.format(DateTimeFormatter.ofPattern("HH"))
val mm        = zdt.format(DateTimeFormatter.ofPattern("mm"))
val dayOfWeek = zdt.getDayOfWeek.toString.toLowerCase

// Exiting paste mode, now interpreting.

import java.time.{Instant, ZonedDateTime, ZoneId}
import java.time.format.DateTimeFormatter
zdt: java.time.ZonedDateTime = 2019-07-30T08:06:20+09:00[Asia/Tokyo]
HH: String = 08
mm: String = 06
dayOfWeek: String = tuesday

toString -> getDisplayName

Scala には既に Any に toString があるため、Show を定義するのは馬鹿げているように一見見えるかもしれない。
Any ということは逆に何でも該当してしまうので、型安全性を失うことになる。 toString は何らかの親クラスが書いたゴミかもしれない:

列挙型DayOfWeek
https://docs.oracle.com/javase/jp/8/docs/api/java/time/DayOfWeek.html

scala> :paste
// Entering paste mode (ctrl-D to finish)

import java.time.{Instant, ZonedDateTime, ZoneId}
import java.time.format.{DateTimeFormatter, TextStyle}
import java.util.Locale

val zdt       = ZonedDateTime.ofInstant(Instant.ofEpochSecond(1564441580), ZoneId.of("Asia/Tokyo"))
val dayOfWeek = zdt.getDayOfWeek.getDisplayName(TextStyle.FULL, Locale.US).toLowerCase

// Exiting paste mode, now interpreting.

import java.time.{Instant, ZonedDateTime, ZoneId}
import java.time.format.{DateTimeFormatter, TextStyle}
import java.util.Locale
zdt: java.time.ZonedDateTime = 2019-07-30T08:06:20+09:00[Asia/Tokyo]
dayOfWeek: String = tuesday
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?