How to convert java.util.Date, java.sql.Date, LocalDate, ZonedDateTime

Purpose

LocalDate has been added to Java8, which is very convenient. However, java.sql.Date is used when using JDBC, and java.util.Date is used when using POI, but interoperability is not considered in LocalDate. So, for the three that are often used in business, ZonedDateTime also summarized the conversion methods to facilitate interoperability.

Each conversion method

sqlDate, utilDate and LocalDate.java


public LocalDate convertToLocalDate(java.sql.Date sqlDate){
	return sqlDate.toLocalDate();
}

sqlDate, utilDate and LocalDate.java


public java.util.Date convertToUtilDate(java.sql.Date sqlDate){
	return sqlDate;
}

sqlDate, utilDate and LocalDate.java


public LocalDate convertToLocalDate(java.util.Date utilDate){
	return utilDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}

sqlDate, utilDate and LocalDate.java


public java.sql.Date convertToSqlDate(java.util.Date utilDate){
	return new java.sql.Date(utilDate.getTime());
}

sqlDate, utilDate and LocalDate.java


public java.util.Date convertToUtilDate(LocalDate localDate){
	 return java.util.Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
}

sqlDate, utilDate and LocalDate.java


public java.sql.Date convertToSqlDate(LocalDate localDate){
	return java.sql.Date.valueOf(localDate);
}

sqlDate, utilDate and LocalDate.java


public ZonedDateTime convertToZonedDateTime(java.util.Date utilDate){
	return utilDate.toInstant().atZone(ZoneId.systemDefault());
}

sqlDate, utilDate and LocalDate.java


public ZonedDateTime convertToZonedDateTime(java.sql.Date sqlDate){
	return sqlDate.toLocalDate().atStartOfDay(ZoneId.systemDefault());
}

sqlDate, utilDate and LocalDate.java


public ZonedDateTime convertToZonedDateTime(LocalDate localDate){
	return localDate.atStartOfDay(ZoneId.systemDefault());
}

sqlDate, utilDate and LocalDate.java


public java.util.Date convertToUtilDate(ZonedDateTime zonedDateTime){
	return java.util.Date.from(zonedDateTime.toInstant());
}

sqlDate, utilDate and LocalDate.java


public java.sql.Date convertToSqlDate(ZonedDateTime zonedDateTime){
	return java.sql.Date.valueOf(zonedDateTime.toLocalDate());
}

sqlDate, utilDate and LocalDate.java


public LocalDate convertToLocalDate(ZonedDateTime zonedDateTime){
	return zonedDateTime.toLocalDate();
}

Summary

The conversion method of java.util.Date, java.sql.Date, LocalDate, ZonedDateTime is summarized. The source, including the tests, can be found here [https://github.com/xaatw0/quiita/blob/master/src/sqlDate, utilDate and LocalDate.java).

Recommended Posts

How to convert java.util.Date, java.sql.Date, LocalDate, ZonedDateTime
How to convert LocalDate and Timestamp
How to convert Java radix
[Urgent recruitment] I can't convert from java.util.date to java.sql.date ...
How to convert erb file to haml
[Java] How to calculate age using LocalDate
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
[Ruby] How to convert CSV file to Yaml (Yml)
[Android] How to convert a character string to resourceId
How to deploy
How to convert a solidity contract to a Java contract class
Ruby How to convert between uppercase and lowercase
[Rails] How to convert UC time display to Japanese time display
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
[Kotlin] Convert ZonedDateTime to String by specifying the format
How to develop OpenSPIFe
How to call AmazonSQSAsync
How to use Map
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use map
How to use collection_select
How to adapt Bootstrap
How to use Twitter4J
How to use active_hash! !!
How to install Docker
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to write dockerfile
How to uninstall Rails
How to install docker-machine
[How to use label]
How to write docker-compose
How to use identity
How to use hashes
How to write Mockito
How to use JUnit 5
How to install MySQL
How to write migrationfile
How to build android-midi-lib
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to install ngrok
How to type backslash \
How to concatenate strings
[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
For Java beginners: List, Map, Iterator / Array ... How to convert?
[Ruby] How to batch convert strings in an array to numbers