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. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static void main(String[] args) {
  14. String dateFormat = "dd.MM.yyyy";
  15. String[] arr = {
  16. "19.02.1996",
  17. "25.03",
  18. null
  19. };
  20. for (String date: arr) {
  21. if (isValidFormat(dateFormat, date)) { // Проверка даты на валидность
  22. System.out.println(date);
  23. // Делаем, что надо
  24. }
  25. }
  26. }
  27.  
  28. public static boolean isValidFormat(String format, String value) {
  29. if (value == null)
  30. return false;
  31. Date date = null;
  32. try {
  33. date = sdf.parse(value);
  34. if (!value.equals(sdf.format(date))) {
  35. date = null;
  36. }
  37. } catch (ParseException ex) {}
  38. return date != null;
  39. }
  40.  
  41. }
Success #stdin #stdout 0.07s 4386816KB
stdin
Standard input is empty
stdout
19.02.1996