fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.text.*;
  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[] stringDate = {"04/31/2015", "02/29/2016", "06/40/2015"};
  14. SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
  15. dateFormat.setLenient(false);
  16.  
  17. for (String str: stringDate) {
  18. try {
  19. if(dateFormat.parse(str, new ParsePosition(0)) != null) {
  20. System.out.println(str + ": valid");
  21. } else {
  22. System.out.println(str + ": Invalid date");
  23. }
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. }
Success #stdin #stdout 0.15s 321280KB
stdin
Standard input is empty
stdout
04/31/2015: Invalid date
02/29/2016: valid
06/40/2015: Invalid date