Java Hour Calculate isValidTime(int hour, int minute, boolean ampm)

Here you can find the source of isValidTime(int hour, int minute, boolean ampm)

Description

Tests if the inputs are valid time.

License

Open Source License

Parameter

Parameter Description
hour The Hour value. ( 0 - 23 or 1 - 12 )
minute The Minute value. ( 0 - 59 )
ampm If true, the time is in 12 hour format. Otherwise, it is in 24 hour format.

Return

True if the time inputs can be used to create a valid time instance.

Declaration

public static boolean isValidTime(int hour, int minute, boolean ampm) 

Method Source Code

//package com.java2s;
/**//from  w  ww .j  a  v  a 2  s  . co  m
*  Copyright 2012 NCS Pte. Ltd. All Rights Reserved
*
*  This software is confidential and proprietary to NCS Pte. Ltd. You shall
*  use this software only in accordance with the terms of the license
*  agreement you entered into with NCS.  No aspect or part or all of this
*  software may be reproduced, modified or disclosed without full and
*  direct written authorisation from NCS.
*
*  NCS SUPPLIES THIS SOFTWARE ON AN "AS IS" BASIS. NCS MAKES NO
*  REPRESENTATIONS OR WARRANTIES, EITHER EXPRESSLY OR IMPLIEDLY, ABOUT THE
*  SUITABILITY OR NON-INFRINGEMENT OF THE SOFTWARE. NCS SHALL NOT BE LIABLE
*  FOR ANY LOSSES OR DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
*  MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**
     * Tests if the inputs are valid time. When the ampm parameter is true,
     * the input hour will be tested for 12-hour format ( 1 ?12 ).
     * When it is false, the input hour will be tested for 24-hour format ( 0 ?23 ).
     * <p>
     * @param  hour   The Hour value. ( 0 - 23 or 1 - 12 )
     * @param  minute The Minute value. ( 0 - 59 )
     * @param  ampm   If true, the time is in 12 hour format.
     * Otherwise, it is in 24 hour format.
     *
     * @return True if the time inputs can be used to create a valid time instance.
     */
    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 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 {
            // Begin modify by chen haun on 2012-01-10 for PVD-208 
            //Date temp = parse(time, format);
            parse(time, format);
            // End modify by chen haun on 2012-01-10 for PVD-208 
        } catch (java.text.ParseException e) {
            return false;
        }

        return true;
    }

    /**
     * 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);
        //        TimeZone tz = TimeZone.getTimeZone( timezone );
        //        sdf.setTimeZone(tz);
        return sdf.parse(date);
    }
}

Related

  1. getTomorrowSixHours()
  2. getWeeHours()
  3. hoursMinsSecsFromMs(long ms)
  4. isHourMinute(String str)
  5. isValidTime(int hour, int minute, boolean ampm)
  6. parseDate(String date, String hours)
  7. plusHour(int hour)
  8. plusHour(String dateStr, int hour)
  9. plusHours(Date date, int hours)