28 lines
960 B
Java
28 lines
960 B
Java
// Die Klasse Time überprüfft eine beliebigen String, ob es das erlaubte
|
|
// Zeitformat "SS:MM" hat.
|
|
public class Time {
|
|
public static boolean timeCheck(String time) {
|
|
// Initialsisieren der Reglularexpression für das erlaubte Format
|
|
String regex = "([01]\\d|2[0-3]):([0-5]\\d)";
|
|
boolean formatRichtig = false;
|
|
// Überprüfung, ob der String dem Format enspricht
|
|
if (time == null) {
|
|
formatRichtig = false;
|
|
// schnelle Überprüfung der "Grundformats"
|
|
} else if (time.length() != 5 || time.charAt(2) != ':') {
|
|
formatRichtig = false;
|
|
} else if (time.matches(regex)) {
|
|
formatRichtig = true;
|
|
}
|
|
// Rückgabe des Ergebnis
|
|
return formatRichtig;
|
|
}
|
|
// Für eine schnellere Überprüfung des Codes, ausführlichen Untersuchung
|
|
// erfolgt via eigene JUnit
|
|
public static void main(String[] args) {
|
|
System.out.println(timeCheck("23:56"));
|
|
System.out.println(timeCheck("00:00"));
|
|
System.out.println(timeCheck(null));
|
|
}
|
|
}
|