fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.time.*;
  6. import java.time.format.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. String data = "31/04/2020";
  14. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/uuuu");
  15. LocalDate dt = LocalDate.parse(data, fmt);
  16. // 31 de abril foi ajustado para 30 de abril
  17. System.out.println(fmt.format(dt));
  18.  
  19. // usando modo STRICT, 31 de abril não é aceito
  20. DateTimeFormatter fmt2 = DateTimeFormatter.ofPattern("dd/MM/uuuu").withResolverStyle(ResolverStyle.STRICT);
  21. try {
  22. dt = LocalDate.parse(data, fmt2);
  23. } catch(DateTimeParseException e) {
  24. System.out.println("Erro: " + e.getMessage());
  25. }
  26. }
  27. }
Success #stdin #stdout 0.1s 37264KB
stdin
Standard input is empty
stdout
30/04/2020
Erro: Text '31/04/2020' could not be parsed: Invalid date 'APRIL 31'