[JAVA] Conversion of String, Date, LocalDate

Convert String to java.time.LocalDate

static final private String DATE_FORMAT = "yyyy-MM-dd";

public static LocalDate string2LocalDate(final String date) {
  return LocalDate.parse(date, DateTimeFormatter.ofPattern(DATE_FORMAT));
}

Convert java.util.Date to java.time.LocalDate

public static LocalDate date2LocalDate(final Date date) {
  return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}

Convert java.time.LocalDate to java.util.Date

public static Date localDate2Date(final LocalDate localDate) {
  return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
}

Recommended Posts

Conversion of String, Date, LocalDate
Java date data type conversion (Date, Calendar, String)
Java type conversion (String, int, Date, Calendar, etc.)
Use of Date class
URL to String conversion
Uri → String, String → Uri type conversion
Leet string conversion program
[Java] Date type conversion
Speed comparison at the time of generation at the time of date conversion
[Java1.8 +] Get the date of the next x day of the week with LocalDate
Various methods of Java String class
Numerical value (int) ⇔ character string (String) conversion
About Lambda, Stream, LocalDate of Java8
Date processing in Java (LocalDate: Initialization)
Various methods of the String class
[Java] Correct comparison of String type
I read the source of String