[Java] Date type conversion

Thing you want to do

--Description of the following 5 types of frequently used conversion methods

String -> java.util.Date

    //conversion: SimpleDateFormat
    //exception: ParseException
    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
    date = format.parse("2019/04/01");

-> java.sql.Date

    //conversion:None
    //exception:None
    //The delimiter is "hyphen"(-)Only allowed "
  java.sql.Date sqlDate= java.sql.Date.valueOf("2019-04-01");

-> LocalDate

    //conversion: DateTimeFormatter
    //exception:None(DateTimeParseException) 
    LocalDate.parse("2019/4/1", DateTimeFormatter.ofPattern("yyyy/MM/dd")); //0 must be filled(Exception occurred)
    LocalDate.parse("2019/4/1", DateTimeFormatter.ofPattern("yyyy/M/d")) ;   //0 No need to fill

-> LocalDateTime

    //conversion: DateTimeFormatter
    //exception:None(DateTimeParseException)
    LocalDateTime.parse("2019/04/01", DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));

java.util.Date -> String

    //conversion: SimpleDateFormat 
    //exception:None
    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
    format.format(new Date());

-> java.sql.Date

    //conversion:None
    //exception:None
    //point:Once milliseconds(Epoch time:The elapsed time from 1970 is used as an argument.)
    java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());

-> LocalDate

    //conversion: Instant, ZoneDateTime
    //exception:None
    Date date = new Date();
    LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

-> LocalDateTime

    //conversion: Instant, ZoneDateTime
    //exception:None
    LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

java.sql.Date -> String

    //conversion- 
    //exception- 
    java.sql.Date date = java.sql.Date.valueOf("2019-04-01");
    String strDate = date.toString();

-> java.util.Date

★ I will write tomorrow.

-> LocalDate

★ I will write tomorrow.

-> LocalDateTime

★ I will write tomorrow.

LocalDate

-> String

    //conversion: DateTimeFormatter
    //exception:None
    LocalDate localDate = LocalDate.of(2019, 04, 01);
    String strDate = localDate.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));

-> java.util.Date

    //conversion: SimpleDateFormat
    //exception: ParseException
    //point: LocalDate -> String -> Date
  localDate = LocalDate.of(2019, 04, 01);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    date = format.parse(localDate.toString());

-> java.sql.Date

    //conversion: -
    //exception: -
    LocalDate localDate = LocalDate.of(2019, 04, 01);
    java.sql.Date sqlDate = java.sql.Date.valueOf(localDate);

-> LocalDateTime

    //conversion: -
    //exception: -
    //point:Hours, minutes and seconds are 00:00:00
    LocalDate localDate = LocalDate.of(2019, 04, 01);    
    LocalDateTime localDateTime = localDate.atStartOfDay();

LocalDateTime

-> String

    //conversion: DateTimeFormatter
    //exception: -
    LocalDateTime localDate = LocalDateTime.of(2019, 04, 01, 23, 59, 58);
    String strDate = localDate.format(DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss"));

-> java.util.Date

    //conversion: LocalDateTime -> ZonedDateTime -> Instant -> Date
    //exception-
    LocalDateTime localDateTime = LocalDateTime.of(2019, 04, 01, 23, 59, 58);
    Date date = Date.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()).toInstant());

-> java.sql.Date

    //conversion: LocalDateTime -> LocalDate -> sql.Date
    //exception: -
    LocalDateTime localDateTime = LocalDateTime.of(2019, 04, 01, 23, 59, 58);
    java.sql.Date sqlDate = java.sql.Date.valueOf(localDateTime.toLocalDate());

-> LocalDate

    //conversion: -
    //exception: -
    LocalDateTime localDateTime = LocalDateTime.of(2019, 04, 01, 23, 59, 58);
    LocalDate localDate = localDateTime.toLocalDate();

Reference site

Recommended Posts

[Java] Date type conversion
Java type conversion
Java date data type conversion (Date, Calendar, String)
[Java] List type / Array type conversion
[Java] Precautions for type conversion
Java type conversion (String, int, Date, Calendar, etc.)
Java Primer Series (Type Conversion)
[JAVA] Stream type
[Java] Enumeration type
Java double type
Java-automatic type conversion
[Easy-to-understand explanation! ] Reference type type conversion in Java
[Java ~ Variable definition, type conversion ~] Study memo
Java study # 3 (type conversion and instruction execution)
[Basic knowledge of Java] About type conversion
[Java] Calculation mechanism, operators and type conversion
Type conversion from java BigDecimal type to String type
[Java] Full-width ⇔ half-width conversion
[Introduction to Java] About type conversion (cast, promotion)
[Java] Date / time operations
Uri → String, String → Uri type conversion
Java basic date manipulation
[Java, Kotlin] Type Variance
Java class type field
Type determination in Java
Java study # 1 (typical type)
Date manipulation in Java 8
[Java] About enum type
java: Add date [Note]
Endian conversion with JAVA
Java learning memo (data type)
Conversion of String, Date, LocalDate
Try functional type in Java! ①
Java study # 7 (branch syntax type)
[Java] Date Related Term Memo
View current date in Java
[Java Bronze] Learning memo (interface, static method, type conversion, etc.)
[Java] "T" is included in date type JSON in API response
Java release date and EOL summary
Date processing in Java (LocalDate: Initialization)
Java Unit Test Library-Artery-Current Date Judgment
[Java] Data type / matrix product (AOJ ⑧ Matrix product)
java (use class type for field)
Java
[Java] Date period duplication check sample
Java
[Java] Correct comparison of String type
[Java] Conversion from array to List
Java array / list / stream mutual conversion list
How to use Java enum type
[Java 8] Function that returns UTC time as Date type (calculated using OffsetZone)
Java review (2) (calculation, escape sequence, evaluation rule, type conversion, instruction execution statement)
[Java Siler] About type inference by var
Conversion between Kotlin nullable and Java Optional
[Java] Implicit type cast (AOJ10-sum of numbers)
How to implement date calculation in Java
Full-width → half-width conversion with Java String (full-width kana → half-width kana)
Regarding String type equivalence comparison in Java
Studying Java 8 (date API in java.time package)
Use PostgreSQL data type (jsonb) from Java
[Personal memo] Java data type is annoying