--Description des 5 types suivants de méthodes de conversion fréquemment utilisées
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:Aucun
//exception:Aucun
//Le délimiteur est "tiret"(-)Uniquement autorisé "
java.sql.Date sqlDate= java.sql.Date.valueOf("2019-04-01");
-> LocalDate
//conversion: DateTimeFormatter
//exception:Aucun(DateTimeParseException)
LocalDate.parse("2019/4/1", DateTimeFormatter.ofPattern("yyyy/MM/dd")); //0 doit être rempli(Il y a une exception)
LocalDate.parse("2019/4/1", DateTimeFormatter.ofPattern("yyyy/M/d")) ; //0 Pas besoin de remplir
-> LocalDateTime
//conversion: DateTimeFormatter
//exception:Aucun(DateTimeParseException)
LocalDateTime.parse("2019/04/01", DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
java.util.Date -> String
//conversion: SimpleDateFormat
//exception:Aucun
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
format.format(new Date());
-> java.sql.Date
//conversion:Aucun
//exception:Aucun
//point:Une fois par milliseconde(Epoque temps:Prenons le temps écoulé depuis 1970 comme argument.)
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
-> LocalDate
//conversion: Instant, ZoneDateTime
//exception:Aucun
Date date = new Date();
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
-> LocalDateTime
//conversion: Instant, ZoneDateTime
//exception:Aucun
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
★ J'écrirai demain.
-> LocalDate
★ J'écrirai demain.
-> LocalDateTime
★ J'écrirai demain.
LocalDate
-> String
//conversion: DateTimeFormatter
//exception:Aucun
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:Les heures, minutes et secondes sont égales à 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