parse LocalDate using ISO_LOCAL_DATE_TIME format - Java java.time

Java examples for java.time:Format

Description

parse LocalDate using ISO_LOCAL_DATE_TIME format

Demo Code



//package com.java2s;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Optional;

public class Main {
  private static DateTimeFormatter DEFAULT_DATE_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

  public static Optional<LocalDate> parseLocalDate(String dateString) {
    try {/*  w  w  w  .  j  av a2s  . c  om*/
      return Optional.of(DEFAULT_DATE_FORMATTER.parse(dateString, LocalDate::from));
    } catch (DateTimeParseException e) {
      return Optional.empty();
    }
  }
}

Related Tutorials