fork(1) download
  1. import java.time.*;
  2. import java.time.format.*;
  3. import java.util.stream.*;
  4.  
  5. class Main {
  6. public static void main(String[] args) {
  7. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("[uuuu][uuuuu]-MM-dd");
  8. DateTimeFormatter outFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd");
  9.  
  10. Stream.of(
  11. "709-07-14",
  12. "2009-07-14",
  13. "10253-07-14"
  14. )
  15. .forEach(str -> {
  16. try {
  17. LocalDate date = LocalDate.parse(str, formatter);
  18. String fStr = date.format(outFormatter);
  19. System.out.format("LocalDate: %s. String: %s.%n", date, fStr);
  20. }
  21. catch (DateTimeException exc) {
  22. System.out.println(exc.getMessage());
  23. }
  24. });
  25. }
  26. }
Success #stdin #stdout 0.09s 35280KB
stdin
Standard input is empty
stdout
Text '709-07-14' could not be parsed at index 0
LocalDate: 2009-07-14. String: 2009-07-14.
LocalDate: +10253-07-14. String: +10253-07-14.