Java Hour getHourBeforeTime(int hour)

Here you can find the source of getHourBeforeTime(int hour)

Description

get Hour Before Time

License

Open Source License

Declaration

public static Date getHourBeforeTime(int hour) 

Method Source Code

//package com.java2s;

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

public class Main {

    public static Date getHourBeforeTime(int hour) {

        Calendar now = Calendar.getInstance();
        now.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY) - hour);
        return now.getTime();
    }/*from  ww  w  .j  av  a  2s  .  c o m*/

    public static String getTime(Calendar c) {
        return getDate(c.getTime(), "HH:mm:ss");
    }

    public static String getDate() {
        return getDate(getCurDate(), "yyyy-MM-dd");
    }

    public static String getDate(Date date, String format) {

        String dtstr = "";
        if (date == null) {
            return dtstr;
        }

        if (format == null || "".equals(format.trim())) {
            format = "yyyy-MM-dd";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        dtstr = sdf.format(date);
        return (dtstr == null ? "" : dtstr);

    }

    public static String getDate(Date date) {
        return getDate(date, "yyyy-MM-dd");
    }

    public static Date getDate(long time) {

        Calendar c = getCurCalendar();
        c.setTimeInMillis(time);

        return c.getTime();
    }

    public static Date getCurDate() {
        return getCurCalendar().getTime();
    }

    public static Calendar getCurCalendar() {
        return Calendar.getInstance();
    }
}

Related

  1. getHour12(Date date)
  2. getHourAfter(Date date, int hour)
  3. getHourAfter(final Date _from, final int _hours)
  4. getHourAndMin(long time)
  5. getHourAndMinutes(Date dt)
  6. getHourByZore(int hour)
  7. getHourDetails(int timeInfo)
  8. getHourFormatedTwoDigit()
  9. getHourFromDate(Date date)