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

StringaddHour(String now)
add Hour
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date dt;
try {
    dt = format.parse(now);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(dt);
    calendar.add(Calendar.HOUR, 1);
    return format.format(calendar.getTime());
...
StringaddHours(int date, int hours)
add Hours
try {
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    Calendar cd = Calendar.getInstance();
    cd.setTime(format.parse(String.valueOf(date)));
    cd.add(Calendar.HOUR_OF_DAY, hours);
    return format.format(cd.getTime());
} catch (ParseException e) {
    throw new RuntimeException(e);
...
DateaddNHours(Date date, int hours)
add N Hours
Calendar currentDate = Calendar.getInstance();
currentDate.setTime(date);
currentDate.add(Calendar.HOUR_OF_DAY, hours);
return currentDate.getTime();
intdiffHour(String day1, String day2)
diff Hour
Date parse = seconParse.parse(day1);
Date parse2 = dayParse.parse(day2);
long time = parse.getTime();
long time2 = parse2.getTime();
long diffTimes = time2 - time;
int hours = (int) (diffTimes / 1000 / 60 / 60);
return hours;
StringgetBothTime(String startTime, int hours)
get Both Time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
    Date date = sdf.parse(startTime);
    Date retDate = new Date(date.getTime() + hours * 3600000l);
    return sdf.format(retDate);
} catch (ParseException e) {
    e.printStackTrace();
return null;
StringgetCurrHourDate()
get Curr Hour Date
Calendar now = Calendar.getInstance();
String date = getDate(now.getTime(), "yyyy-MM-dd");
int hour = now.get(Calendar.HOUR_OF_DAY);
return date + "_" + hour;
StringgetDelayEndTime(int hour, String timezone)
get Delay End Time
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(new Date().getTime());
calendar.add(10, hour);
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd HH:59:59");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone(timezone));
return dateFormatGmt.format(calendar.getTime());
StringgetDelayTime(int hour, String date)
get Delay Time
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(string2Date(date).getTime());
calendar.add(10, hour);
SimpleDateFormat dateFormatGmt = new SimpleDateFormat(DATE_FORMAT);
return dateFormatGmt.format(calendar.getTime());
StringgetDiffDateTime(int diff, int hours)
get Diff Date Time
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, diff);
c.add(Calendar.HOUR, hours);
return dateFormat(c.getTime());
intgetDiffHour(String startTime, String endTime)
get Diff Hour
long diff = 0;
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
    Date startDate = ft.parse(startTime);
    Date endDate = ft.parse(endTime);
    diff = startDate.getTime() - endDate.getTime();
    diff = diff / (1000 * 60 * 60);
} catch (ParseException e) {
...