1. import java.time.LocalDate;
  2. import java.time.LocalTime;
  3. import java.time.ZoneId;
  4. import java.time.format.DateTimeFormatter;
  5. import java.time.format.DateTimeParseException;
  6. class TimeValidator {
  7. /**
  8. * Validates a time string, handling potential errors and ensuring backward compatibility.
  9. * @param timeString The time string to validate.
  10. * @param expectedFormat The expected format of the time string.
  11. * @return A LocalTime object if the time string is valid, otherwise null.
  12. */
  13. public static LocalTime validateTime(String timeString, String expectedFormat) {
  14. try {
  15. // Use DateTimeFormatter to parse the time string.
  16. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(expectedFormat);
  17. LocalTime time = LocalTime.parse(timeString, formatter);
  18. return time;
  19. } catch (DateTimeParseException e) {
  20. // Handle parsing errors (invalid time format).
  21. System.err.println("Error: Invalid time format. Expected: " + expectedFormat + ", Actual: " + timeString);
  22. return null;
  23. } catch (NullPointerException e) {
  24. // Handle null input.
  25. System.err.println("Error: Time string cannot be null.");
  26. return null;
  27. }
  28. }
  29. /**
  30. * Validates a date and time string, handling potential errors and ensuring backward compatibility.
  31. * @param dateTimeString The date and time string to validate.
  32. * @param expectedFormat The expected format of the date and time string.
  33. * @return A LocalDate object if the date and time string is valid, otherwise null.
  34. */
  35. public static LocalDate validateDateTime(String dateTimeString, String expectedFormat) {
  36. try {
  37. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(expectedFormat);
  38. LocalDate date = LocalDate.parse(dateTimeString, formatter);
  39. return date;
  40. } catch (DateTimeParseException e) {
  41. System.err.println("Error: Invalid date/time format. Expected: " + expectedFormat + ", Actual: " + dateTimeString);
  42. return null;
  43. } catch (NullPointerException e) {
  44. System.err.println("Error: Date/time string cannot be null.");
  45. return null;
  46. }
  47. }
  48. public static void main(String[] args) {
  49. // Example Usage
  50. String timeStr1 = "14:30";
  51. String timeStr2 = "invalid_time";
  52. String dateTimeStr1 = "2024-01-01 14:30";
  53. String dateTimeStr2 = "invalid date";
  54. String dateTimeStr3 = null;
  55. LocalTime time1 = validateTime(timeStr1, "HH:mm");
  56. LocalTime time2 = validateTime(timeStr2, "HH:mm");
  57. LocalDate date1 = validateDateTime(dateTimeStr1, "yyyy-MM-dd HH:mm");
  58. LocalDate date2 = validateDateTime(dateTimeStr2, "yyyy-MM-dd HH:mm");
  59. LocalDate date3 = validateDateTime(dateTimeStr3, "yyyy-MM-dd HH:mm");
  60. if (time1 != null) {
  61. System.out.println("Valid Time: " + time1);
  62. }
  63. if (time2 == null) {
  64. System.out.println("Invalid Time: " + time2);
  65. }
  66. if (date1 != null) {
  67. System.out.println("Valid Date: " + date1);
  68. }
  69. if (date2 == null) {
  70. System.out.println("Invalid Date: " + date2);
  71. }
  72. if(date3 == null) {
  73. System.out.println("Invalid Date: " + date3);
  74. }
  75. }
  76. }

Add your comment