14
12

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.

java.util.Date, java.sql.Date, LocalDate, ZonedDateTimeの変換方法

Posted at

目的

Java8にLocalDateが追加されて、非常に便利ではある。しかしJDBC使用時はjava.sql.Date, POI使用時にはjava.util.Dateを使用するが、LocalDateでは相互運用は考えられていない。そこで、業務でよく使用する3つについて、ついでにZonedDateTimeも、相互運用が楽になるように変換方法をまとめた。

各変換方法

  • java.sql.Date→LocalDate
sqlDateとutilDateとLocalDate.java
public LocalDate convertToLocalDate(java.sql.Date sqlDate){
	return sqlDate.toLocalDate();
}
  • java.sql.Date→java.util.Date
sqlDateとutilDateとLocalDate.java
public java.util.Date convertToUtilDate(java.sql.Date sqlDate){
	return sqlDate;
}
  • java.util.Date→LocalDate
sqlDateとutilDateとLocalDate.java
public LocalDate convertToLocalDate(java.util.Date utilDate){
	return utilDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
  • java.util.Date→java.sql.Date
sqlDateとutilDateとLocalDate.java
public java.sql.Date convertToSqlDate(java.util.Date utilDate){
	return new java.sql.Date(utilDate.getTime());
}
  • LocalDate→java.util.Date
sqlDateとutilDateとLocalDate.java
public java.util.Date convertToUtilDate(LocalDate localDate){
	 return java.util.Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
}
  • LocalDate→java.sql.Date
sqlDateとutilDateとLocalDate.java
public java.sql.Date convertToSqlDate(LocalDate localDate){
	return java.sql.Date.valueOf(localDate);
}
  • java.util.Date→ZonedDateTime
sqlDateとutilDateとLocalDate.java
public ZonedDateTime convertToZonedDateTime(java.util.Date utilDate){
	return utilDate.toInstant().atZone(ZoneId.systemDefault());
}

  • java.sql.Date→ZonedDateTime
sqlDateとutilDateとLocalDate.java
public ZonedDateTime convertToZonedDateTime(java.sql.Date sqlDate){
	return sqlDate.toLocalDate().atStartOfDay(ZoneId.systemDefault());
}
  • LocalDate→ZonedDateTime
sqlDateとutilDateとLocalDate.java
public ZonedDateTime convertToZonedDateTime(LocalDate localDate){
	return localDate.atStartOfDay(ZoneId.systemDefault());
}
  • ZonedDateTime→java.util.Date
sqlDateとutilDateとLocalDate.java
public java.util.Date convertToUtilDate(ZonedDateTime zonedDateTime){
	return java.util.Date.from(zonedDateTime.toInstant());
}
  • ZonedDateTime→java.sql.Date
sqlDateとutilDateとLocalDate.java
public java.sql.Date convertToSqlDate(ZonedDateTime zonedDateTime){
	return java.sql.Date.valueOf(zonedDateTime.toLocalDate());
}
  • ZonedDateTime→LocalDate
sqlDateとutilDateとLocalDate.java
public LocalDate convertToLocalDate(ZonedDateTime zonedDateTime){
	return zonedDateTime.toLocalDate();
}

まとめ

java.util.Date, java.sql.Date,LocalDate,ZonedDateTimeの変換方法をまとめた。ソースは、テストも含めてこちらにあります。

14
12
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
14
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?