[JAVA] A memorandum when I investigated how to convert from Epoch Time to Date type in Scala

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

A memorandum when I investigated how to convert from Epoch Time to Date type in Scala
How to write a migration from Rails datetime type to date type
[Java] How to convert a character string from String type to byte type
I tried to convert a string to a LocalDate type in Java
How to write a date comparison search in Rails
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
How to create a form to select a date from the calendar
How to batch initialize arrays in Java that I didn't know when I was a beginner
How to store a string from ArrayList to String in Java (Personal)
How to select a specified date by code in FSCalendar
I wrote a code to convert numbers to romaji in TDD
I can't log in to MySQL from Django when using docker-compose
I tried to write code like a type declaration in Ruby
[Ruby] I want to put an array in a variable. I want to convert to an array
[Convenient to remember !!!] How to convert from LocalDate type to character string and from character string to LocalDate type
How to convert param to hash with Rails controller (updated from time to time)
[Java] How to convert from String to Path type and get the path
Memorandum Poem (updated from time to time)
How to develop from VScode in a remote destination environment or a remote destination container environment
How to get the current date as a string in yyyyMMdd format
How to pass a proxy when throwing REST over SSL in Java
How to reference a column when overriding the column name method in ActiveRecord
A memorandum on how to use Eclipse
How to implement date calculation in Java
How to get date data in Ruby
[Rails] How to convert from erb to haml
How to insert a video in Rails
[JSR-310 Date and Time API] Precautions for format definition when converting from Japanese calendar character string to date type
Convert from java UTC time to JST time
[Java] How to set the Date time to 00:00:00
How to fix system date in JUnit
How to map tsrange type in Hibernate
How to get the date in java
How to publish a library in jCenter
[Java] How to convert one element of a String type array to an Int type
Points I stumbled upon when creating an Android application [Updated from time to time]
How to fix a crash when deleting Realm data in Swift UI List
[Spring Boot] How to get properties dynamically from a string contained in a URL
[Rails] How to get rid of flash messages in a certain amount of time
How to convert a value of a different type and assign it to another variable
I tried to automatically generate a class to convert from a data class to a Bundle with APT
I want to get only the time from Time type data ...! [Strftime] * Additional notes
I made a sample of how to write delegate in SwiftUI 2.0 using MapKit
[When using MiniMagick] A memorandum because I stumbled in the CircleCI test environment.
How to get the date from the JavaScript Date type that C # developers are addicted to
How to start a subscript from an arbitrary number in Ruby iterative processing
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
Convert to a tag to URL string in Rails
Convert from C String pointer to Swift String type
[Rails] How to write when making a subquery
[Android] How to convert a character string to resourceId
How to get a heapdump from a Docker container
How to display a web page in Java
How to get Class from Element in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
How to convert a solidity contract to a Java contract class
How to run a djUnit task in Ant
How to add a classpath in Spring Boot
How to create a theme in Liferay 7 / DXP
How to implement a like feature in Rails