joda-time
Try using Joda Time instead of the standard java.util.Date class. The Joda Time library has a much better API for date processing.
https://codeday.me/jp/qa/20181217/35055.html
Time operation memo with 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.
https://github.com/JodaOrg/joda-time/blob/master/.github/issue_template.md
Remember this much for Java 8 date and time API for the time being https://qiita.com/tag1216/items/91a471b33f383981bfaa Handle date and time in 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
Defining Show may seem silly, as Scala already has a toString in Any. Any means anything, and you lose type safety. toString may be garbage written by some parent class:
http://eed3si9n.com/herding-cats/ja/Show.html
Enumeration 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
Recommended Posts