Java Hour Calculate addNHours(Date date, int hours)

Here you can find the source of addNHours(Date date, int hours)

Description

add N Hours

License

Open Source License

Declaration

public static Date addNHours(Date date, int hours) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static final String DF_YMD = "yyyy-MM-dd";
    public static final String DF_HMS = "HH:mm:ss";

    public static Date addNHours(Date date, int hours) {
        Calendar currentDate = Calendar.getInstance();
        currentDate.setTime(date);//from  ww w .  ja  va 2  s .  c o  m
        currentDate.add(Calendar.HOUR_OF_DAY, hours);
        return currentDate.getTime();
    }

    public static String getTime() {
        return dateToString(now(), DF_HMS);
    }

    public static String getTime(Date trialTime) {
        return dateToString(trialTime, DF_HMS);
    }

    public static String dateToString(Date date, String format) {
        if (date == null) {
            return "";
        }
        synchronized (date) {
            SimpleDateFormat df = new SimpleDateFormat(format);
            return df.format(date);
        }
    }

    public static String dateToString(Date date) {
        return dateToString(date, DF_YMD);
    }

    public static Date now() {
        return new Date();
    }
}

Related

  1. addHour(String now)
  2. addHours(int date, int hours)
  3. diffHour(String day1, String day2)
  4. getBothTime(String startTime, int hours)
  5. getCurrHourDate()
  6. getDelayEndTime(int hour, String timezone)