--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();
Sting -> java.sql.Date https://www.sejuku.net/blog/19077#StringSQLDate
String -> LocalDateTime https://qiita.com/riekure/items/d83d4ea5d8a19a267453
java.util.Date -> LocalDate https://qiita.com/hryshtk/items/43991beaabbb9d587360
LocalDate -> java.sql.Date https://code-examples.net/ja/q/15ddf55
Recommended Posts