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