@@ -45,4 +45,124 @@ static Boolean isJsonString(String jsonString) {
4545 return false ;
4646 }
4747 }
48+
49+ /**
50+ * Validates a date and time string in the format "YYYY-MM-DD HH:mm:ss" or "YYYY-MM-DD HH:mm".
51+ *
52+ * @param dateTimeStr The date and time string to validate.
53+ * @return {@code true} if the input string matches the specified formats and represents a valid date and time,
54+ * {@code false} otherwise.
55+ */
56+ public static boolean isValidDateTimeFormatForConvertTimeZone (String dateTimeStr ) {
57+ // Regular expression to match the format YYYY-MM-DD HH:mm:ss or YYYY-MM-DD HH:mm
58+ String dateTimeRegex = "\\ d{4}-\\ d{2}-\\ d{2} \\ d{2}:\\ d{2}(:\\ d{2})?" ;
59+
60+ if (dateTimeStr .matches (dateTimeRegex )) {
61+ String [] parts = dateTimeStr .split (" " );
62+ String datePart = parts [0 ];
63+ String timePart = parts [1 ];
64+
65+ String [] dateParts = datePart .split ("-" );
66+ String [] timeParts = timePart .split (":" );
67+
68+ // Check if all parts are valid integers
69+ for (String part : dateParts ) {
70+ try {
71+ Integer .parseInt (part );
72+ } catch (NumberFormatException e ) {
73+ return false ;
74+ }
75+ }
76+ for (String part : timeParts ) {
77+ try {
78+ Integer .parseInt (part );
79+ } catch (NumberFormatException e ) {
80+ return false ;
81+ }
82+ }
83+
84+ // Check if the date and time parts are within valid ranges
85+ int year = Integer .parseInt (dateParts [0 ]);
86+ int month = Integer .parseInt (dateParts [1 ]);
87+ int day = Integer .parseInt (dateParts [2 ]);
88+ int hour = Integer .parseInt (timeParts [0 ]);
89+ int minute = Integer .parseInt (timeParts [1 ]);
90+ int second = timeParts .length == 3 ? Integer .parseInt (timeParts [2 ]) : 0 ;
91+
92+ // Basic validation for year, month, and day
93+ if (year < 0 || month < 1 || month > 12 || day < 1 || day > 31 ||
94+ hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59 ) {
95+ return false ;
96+ }
97+
98+ // Additional validation for month and day
99+ if ((month == 4 || month == 6 || month == 9 || month == 11 ) && day > 30 ) {
100+ return false ;
101+ }
102+ if (month == 2 ) {
103+ if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0 )) {
104+ return day <= 29 ;
105+ } else {
106+ return day <= 28 ;
107+ }
108+ }
109+
110+ return true ;
111+ }
112+ return false ;
113+ }
114+
115+ /**
116+ * Validates a date string in the format "YYYY-MM-DD".
117+ *
118+ * @param dateStr The date string to validate.
119+ * @return {@code true} if the input string matches the specified format and represents a valid date,
120+ * {@code false} otherwise.
121+ */
122+ public static boolean isValidDateFormatForAstronomy (String dateStr ) {
123+ // Regular expression to match the format YYYY-MM-DD
124+ String dateRegex = "\\ d{4}-\\ d{2}-\\ d{2}" ;
125+
126+ if (dateStr .matches (dateRegex )) {
127+ String [] dateParts = dateStr .split ("-" );
128+
129+ // Check if all parts are valid integers
130+ for (String part : dateParts ) {
131+ try {
132+ Integer .parseInt (part );
133+ } catch (NumberFormatException e ) {
134+ return false ;
135+ }
136+ }
137+
138+ // Check if the date parts are within valid ranges
139+ int year = Integer .parseInt (dateParts [0 ]);
140+ int month = Integer .parseInt (dateParts [1 ]);
141+ int day = Integer .parseInt (dateParts [2 ]);
142+
143+ // Basic validation for year, month, and day
144+ if (year < 0 || month < 1 || month > 12 || day < 1 || day > 31 ) {
145+ return false ;
146+ }
147+
148+ // Additional validation for month and day
149+ if ((month == 4 || month == 6 || month == 9 || month == 11 ) && day > 30 ) {
150+ return false ;
151+ }
152+ if (month == 2 ) {
153+ if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0 )) {
154+ if (day > 29 ) {
155+ return false ;
156+ }
157+ } else {
158+ if (day > 28 ) {
159+ return false ;
160+ }
161+ }
162+ }
163+
164+ return true ;
165+ }
166+ return false ;
167+ }
48168}
0 commit comments