Here you can find the source of isValidTime(int hour, int minute, boolean ampm)
public static boolean isValidTime(int hour, int minute, boolean ampm)
//package com.java2s; /*// w w w . j ava2 s .c om * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF * Institute of Systems Science, National University of Singapore * * Copyright 2012 Team 4(Part-Time), ISS, NUS, Singapore. All rights reserved. * Use of this source code is subjected to the terms of the applicable license * agreement. * * ----------------------------------------------------------------- * REVISION HISTORY * ----------------------------------------------------------------- * DATE AUTHOR REVISION DESCRIPTION * 10 March 2012 Chen Changfeng 0.1 Class creating * * * * * */ import java.text.SimpleDateFormat; import java.util.*; public class Main { private static String dateFormat = "dd/MM/yyyy"; private static String time12HrFormat = "hh:mm aa"; public static boolean isValidTime(int hour, int minute, boolean ampm) { if (minute < 0 || minute > 59) return false; if (ampm && (hour < 1 || hour > 12)) return false; else if (hour < 0 || hour > 23) return false; else return true; } /** * Tests the input string to ensure that a valid time can be created * according to the 12 Hr time format in the System property. * <p> * If the properties file is not available or the timeformat property has * not been specified, the default 12 Hr format "hh:mm aa" will be used. * <p> * @param time The time string. * @return True if it conforms to the system time format; False otherwise. */ public static boolean isValidTime(String time) { if (time == null) return false; String timeformat = null; // Use the default format of "hh:mm aa". timeformat = time12HrFormat; try { parse(time, timeformat); } catch (java.text.ParseException e) { return false; } return true; } /** * Tests the input string to ensure that a valid time can be created * according to the time format provided. * <p> * @param time The time string. * @param format Time format to conform to. * @return True if it conforms to the specified time format; False otherwise. */ public static boolean isValidTime(String time, String format) { if (time == null || format == null) return false; try { parse(time, format); } catch (java.text.ParseException e) { return false; } return true; } /** * Returns a Date object instance from the input string. * The input string is parsed to return a date object based * on the date format specified in the System property. * <p> * @param date The date string. * @return The date instance created. * @throws java.text.ParseException - if the beginning of the specified string cannot be parsed. */ public static Date parse(String date) throws java.text.ParseException { // Modified by Aris on 02/02/2009 for case 1296351 //if ( date == null || date.length()==0) return null; if (date == null) return null; String dateformat = null; // Use the default format of "dd/MM/yyyy". dateformat = dateFormat; return parse(date, dateformat); } /** * Returns a Date object instance from the input string. * The input date string is parsed to return a date object * based on the format provided. * <p> * Eg., to parse the date string "01/01/2003 9:2 PM", use the * format "dd/MM/yyyy h:m a". The resultant data object will have * the value "Mar 11 2003 09:02", as displayed in 24 hr format. * <p> * @param date The date string. * @param format The date format that the date string conforms to. * @return The date instance created. * @throws java.text.ParseException - if the beginning of the specified string cannot be parsed. */ public static Date parse(String date, String format) throws java.text.ParseException { SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setLenient(false); return sdf.parse(date); } }