This asset basically make use of regular expressions to validate against the 24 hour time format . It restricts the user to enter the time other than 24 hour format .
The code shown below uses pattern matcher to match the regular expression with the input.
The code shown below uses pattern matcher to match the regular expression with the input.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Time24HoursValidator{ private Pattern pattern; private Matcher matcher; private static final String TIME24HOURS_PATTERN = " (([0-1]?[0-9])|(2[0-3])):[0-5][0-9] "; public Time24HoursValidator(){ pattern = Pattern.compile(TIME24HOURS_PATTERN); } /** * Validate time in 24 hours format with regular expression * @param time time address for validation * @return true valid time fromat, false invalid time format */ public boolean validate(final String time){ matcher = pattern.matcher(time); return matcher.matches(); } }