fork download
  1. import java.time.*;
  2. import java.time.format.DateTimeFormatter;
  3. import java.time.format.DateTimeParseException;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. String[] strDates = {"19800229", "19800228", "19820229", "19820228"};
  8. for (String strDate : strDates)
  9. try {
  10. System.out.printf("Parsing %s --> %s%n",
  11. strDate,
  12. LocalDate.parse(strDate, DateTimeFormatter.BASIC_ISO_DATE));
  13. } catch (DateTimeParseException e) {
  14. System.out.printf("Error: %s%n", e.getMessage());
  15. }
  16. }
  17. }
Success #stdin #stdout 0.13s 56940KB
stdin
Standard input is empty
stdout
Parsing 19800229 --> 1980-02-29
Parsing 19800228 --> 1980-02-28
Error: Text '19820229' could not be parsed: Invalid date 'February 29' as '1982' is not a leap year
Parsing 19820228 --> 1982-02-28