fork(1) 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. // your code goes here
  14. System.out.println (isDateValid("12/10/2021"));
  15. }
  16.  
  17. static boolean isDateValid(String date) {
  18. try {
  19. DateTimeFormatter formatter = DateTimeFormatter
  20. .ofPattern("dd/MM/yyyy")
  21. .withResolverStyle(ResolverStyle.STRICT);
  22. LocalDate.parse(date, formatter);
  23. return true;
  24. } catch (DateTimeException ex) {
  25. ex.printStackTrace();
  26. return false;
  27. }
  28. }
  29. }
Success #stdin #stdout #stderr 0.09s 49256KB
stdin
Standard input is empty
stdout
false
stderr
java.time.format.DateTimeParseException: Text '12/10/2021' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=12, MonthOfYear=10, YearOfEra=2021},ISO of type java.time.format.Parsed
	at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2020)
	at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1955)
	at java.base/java.time.LocalDate.parse(LocalDate.java:428)
	at Ideone.isDateValid(Main.java:22)
	at Ideone.main(Main.java:14)
Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=12, MonthOfYear=10, YearOfEra=2021},ISO of type java.time.format.Parsed
	at java.base/java.time.LocalDate.from(LocalDate.java:396)
	at java.base/java.time.format.Parsed.query(Parsed.java:235)
	at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1951)
	... 3 more