0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

java.sql日時クラス↔Date And Time API系の変換

Last updated at Posted at 2020-07-09

いつも忘れるので、ここでメモ

Java8 時間類
LocalDateTime→UinxTimestamp(ミリ秒)

LocalDateTime yestoday = LocalDateTime.now().minusDays(1).withHour(0); // 分、秒、ミリ秒まで細かく設定。
long millis = ZonedDateTime.of(yestoday, ZoneId.systemDefault()).toInstant().toEpochMilli();

以下のこのページからパックリ:https://javazuki.com/articles/sql-date-conver-to-datetime.html
[Java]Unixタイムスタンプと日時の変換 (ZonedDateTime, LocalDateTime)
Timesatmp→LocalDateTime

Timestamp timestamp = new Timestamp(System.currentTimeMillis());
LocalDateTime localDateTime = timestamp.toLocalDateTime();

LocalDateTime→Timesatmp

LocalDateTime localDateTime = LocalDateTime.now();
Timestamp timestamp = Timestamp.valueOf(localDateTime);

java.sql.Date→LocalDate

java.sql.Date javaSqlDate = new java.sql.Date(System.currentTimeMillis());
LocalDate localDate = javaSqlDate.toLocalDate();

LocalDate→java.sql.Date

LocalDate localDate = LocalDate.now();
java.sql.Date javaSqlDate = java.sql.Date.valueOf(localDate);

java.sql.Time→LocalTime

java.sql.Time javaSqlTime = new java.sql.Time(System.currentTimeMillis());
LocalTime localTime = javaSqlTime.toLocalTime();

LocalTime→java.sql.Time

LocalTime localTime = LocalTime.now();
java.sql.Time javaSqlTime = java.sql.Time.valueOf(localTime);

LocalDateTime → java.util.Date

ZoneId zone = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zone);
Instant instant = zonedDateTime.toInstant();
return Date.from(instant);

java.util.Date → LocalDateTime

Instant instant = date.toInstant();
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

参考になった記事
https://qiita.com/riekure/items/d83d4ea5d8a19a267453

以上!

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?