Java Utililty Methods Hour Calculate

List of utility methods to do Hour Calculate

Description

The list of methods to do Hour Calculate are organized into topic(s).

Method

booleanisValidTime(int hour, int minute, boolean ampm)
Tests if the inputs are valid time.
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;
...
longparseDate(String date, String hours)
parse Date
return parseDate(date + " " + hours);
DateplusHour(int hour)
plus Hour
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, hour);
return cal.getTime();
StringplusHour(String dateStr, int hour)
plus Hour
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
Date date = formatter.parse(dateStr);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, hour);
String resultStr = formatter.format(cal.getTime());
return resultStr;
DateplusHours(Date date, int hours)
plus Hours
if (date == null)
    date = getToday();
return changeHours(date, hours);
StringplusOrMinusHours(Date dateTime, long count)
plus Or Minus Hours
try {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    dateTime.setTime(dateTime.getTime() + count * 3600000);
    return sdf.format(dateTime);
} catch (Exception e) {
    e.printStackTrace();
    return "";
DatesetHours(Date date, int amount)
Sets the hours field to a date returning a new object.
return set(date, Calendar.HOUR_OF_DAY, amount);
StringtoStringHour(int hours, int minutes, int seconds, int millis)
to String Hour
return String.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, millis);
StringtoTime(int hours, int minutes, int seconds)
to Time
DecimalFormat df = new DecimalFormat("00");
return df.format(hours) + ":" + df.format(minutes) + ":" + df.format(seconds);
DatetrimToHour(Date date)
trim To Hour
Calendar c = new GregorianCalendar();
c.setTime(date);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();