Example usage for java.util Calendar HOUR

List of usage examples for java.util Calendar HOUR

Introduction

In this page you can find the example usage for java.util Calendar HOUR.

Prototype

int HOUR

To view the source code for java.util Calendar HOUR.

Click Source Link

Document

Field number for get and set indicating the hour of the morning or afternoon.

Usage

From source file:net.firejack.platform.core.utils.DateUtils.java

/**
 * Decrements the date by one hour//from  w  w w  . j ava 2 s.c  o m
 * @param date - date to be decremented
 */
public static void decDateByHour(Date date) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.add(Calendar.HOUR, -1);
    date.setTime(cal.getTimeInMillis());
}

From source file:ezbake.data.elastic.test.TestUtils.java

public static Facet generateDateBucketFacet() {
    final SimpleDateFormat dtg = new SimpleDateFormat("ddHHmm'Z' MM yy");
    final Calendar calendar = new GregorianCalendar();
    final Facet ssrDateFacet = new Facet();
    final RangeFacet dateRangeFacet = new RangeFacet();
    final BaseFacetValue dateField = new BaseFacetValue();
    dateField.setFacetField("visit");
    dateRangeFacet.setField(dateField);//from ww w  .j av  a2  s .  co  m

    final FacetRange last24 = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -1);
    last24time = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    last24.setFrom(dtg.format(last24time));
    dateRangeFacet.addToRanges(last24);

    final FacetRange last48 = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -1);
    last48time = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    last48.setFrom(dtg.format(last48time));
    dateRangeFacet.addToRanges(last48);

    final FacetRange last72 = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -1);
    last72time = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    last72.setFrom(dtg.format(last72time));
    dateRangeFacet.addToRanges(last72);

    final FacetRange lastWeek = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -4);
    lastWeekTime = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    lastWeek.setFrom(dtg.format(lastWeekTime));
    dateRangeFacet.addToRanges(lastWeek);

    final FacetRange last30Days = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -23);
    last30DaysTime = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    last30Days.setFrom(dtg.format(last30DaysTime));
    dateRangeFacet.addToRanges(last30Days);

    final FacetRange last90Days = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -60);
    last90DaysTime = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    last90Days.setFrom(dtg.format(last90DaysTime));
    dateRangeFacet.addToRanges(last90Days);

    final FacetRange lastYear = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -275);
    lastYearTime = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    lastYear.setFrom(dtg.format(lastYearTime));
    dateRangeFacet.addToRanges(lastYear);

    final FacetRequest dateRequest = new FacetRequest();
    dateRequest.setRangeFacet(dateRangeFacet);

    ssrDateFacet.setLabel("Report Date");
    ssrDateFacet.setFacet(dateRequest);

    return ssrDateFacet;
}

From source file:helper.util.DateHelper.java

/**
 * Take a local time calendar and represent it as a day
 * (truncate to day, retain yy/mm/dd from original date)
 **///w  ww.ja v  a  2 s  . co m
public static Calendar asDay(Calendar localTimeCalendar) {
    Calendar asDay = Calendar.getInstance(localTimeCalendar.getTimeZone());
    asDay.clear();
    asDay.set(localTimeCalendar.get(Calendar.YEAR), localTimeCalendar.get(Calendar.MONTH),
            localTimeCalendar.get(Calendar.DATE), 0, 0, 0);
    asDay.set(Calendar.AM_PM, Calendar.AM);
    asDay.set(Calendar.MILLISECOND, 0);
    asDay.set(Calendar.HOUR, 0);
    return asDay;
}

From source file:com.beligum.core.accounts.UserManager.java

public static User getCurrentUser() {
    User retVal = null;/*from ww w .  j  a  va 2 s .  co  m*/
    if (Http.Context.current().session().get(SESSION_KEY_USER) != null) {
        Integer id;
        try {
            id = Integer.parseInt(Http.Context.current().session().get(SESSION_KEY_USER));
        } catch (Exception e) {
            return null;
        }
        retVal = UserRepository.find(id);
        if (retVal != null) {
            String currentTimeOut = Http.Context.current().session().get(SESSION_KEY_TIMEOUT);
            if (currentTimeOut == null) {
                retVal = null;
            } else if (DateTimeHelper.getCurrentTime()
                    .before(DateTimeHelper.formatStringToDate(currentTimeOut, "dd/MM/yyyy HH:mm"))) {
                Calendar timeOut = DateTimeHelper.getCurrentTime();
                timeOut.add(Calendar.HOUR, 1);
                Http.Context.current().session().put(SESSION_KEY_TIMEOUT,
                        DateTimeHelper.formatDate(timeOut, "dd/MM/yyyy HH:mm"));
            } else {
                retVal = null;
            }
        }

    }
    return retVal;

}

From source file:com.seyren.core.util.velocity.AbstractHelper.java

private String getTimeFromUntilString(Date date) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat format = new SimpleDateFormat("HH:mm_yyyyMMdd");
    cal.setTime(date);//w ww.  j a va  2  s.c o m
    cal.add(Calendar.HOUR, -1);
    String from = format.format(cal.getTime());
    cal.add(Calendar.HOUR, 1);
    String until = format.format(cal.getTime());

    return "&from=" + until.toString() + "&until=" + from.toString();
}

From source file:com.redhat.rhn.domain.common.ResetPassword.java

/**
 * @return true {@literal if CREATED < (now - expiration-hours)}
 *//*from w w  w .java2 s. c o  m*/
public boolean isExpired() {
    Calendar now = Calendar.getInstance();
    Calendar expireDate = Calendar.getInstance();
    expireDate.setTime(getCreated() == null ? new Date() : getCreated());
    expireDate.add(Calendar.HOUR,
            Config.get().getInt(PASSWORD_TOKEN_EXPIRATION, PASSWORD_TOKEN_EXPIRATION_DEFAULT));
    return now.after(expireDate);
}

From source file:TimeLib.java

/**
 * Based on code posted at/*from w w w .  ja  va 2  s  .  com*/
 *  http://forum.java.sun.com/thread.jspa?threadID=488676&messageID=2292012
 */
private static int estimateUnitsBetween(long t0, long t1, int field) {
    long d = t1 - t0;
    switch (field) {
    case Calendar.MILLISECOND:
        return (int) d; // this could be very inaccurate. TODO: use long instead of int?
    case Calendar.SECOND:
        return (int) (d / SECOND_MILLIS + .5);
    case Calendar.MINUTE:
        return (int) (d / MINUTE_MILLIS + .5);
    case Calendar.HOUR_OF_DAY:
    case Calendar.HOUR:
        return (int) (d / HOUR_MILLIS + .5);
    case Calendar.DAY_OF_WEEK_IN_MONTH:
    case Calendar.DAY_OF_MONTH:
        // case Calendar.DATE : // codes to same int as DAY_OF_MONTH
        return (int) (d / DAY_MILLIS + .5);
    case Calendar.WEEK_OF_YEAR:
        return (int) (d / WEEK_MILLIS + .5);
    case Calendar.MONTH:
        return (int) (d / MONTH_MILLIS + .5);
    case Calendar.YEAR:
        return (int) (d / YEAR_MILLIS + .5);
    case DECADE:
        return (int) (d / DECADE_MILLIS + .5);
    case CENTURY:
        return (int) (d / CENTURY_MILLIS + .5);
    case MILLENIUM:
        return (int) (d / MILLENIUM_MILLIS + .5);
    default:
        return 0;
    }
}

From source file:com.oncecorp.visa3d.bridge.utility.JdbcUtils.java

/**
 * This method converts the java date into the long that point to the beginning
 * of the day.//from w  w  w.j ava2 s  . c  o  m
 * @param date The converted date object.
 * @param startEnd <tt>true</tt> day start time, <tt>false</tt> day end time.
 * @return The long that point to the beginning or end of the day.
 */
public static long dayValue(Date date, boolean startEnd) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR, startEnd ? 0 : 23);
    calendar.set(Calendar.MINUTE, startEnd ? 0 : 59);
    calendar.set(Calendar.SECOND, startEnd ? 0 : 59);
    calendar.set(Calendar.MILLISECOND, startEnd ? 0 : 999);
    return calendar.getTime().getTime();
}

From source file:com.redhat.rhn.domain.common.CommonFactory.java

/**
 * Create a TinyUrl/*w w  w.  jav a 2s.co m*/
 * @param urlIn to tinyfy
 * @param expires the date we *ADD* 6 hours to to set the expiration on the URL
 * @return TinyUrl instance
 */
public static TinyUrl createTinyUrl(String urlIn, Date expires) {
    String token = RandomStringUtils.randomAlphanumeric(8);
    TinyUrl existing = lookupTinyUrl(token);
    while (existing != null) {
        log.warn("Had collision with: " + token);
        token = RandomStringUtils.randomAlphanumeric(8);
        existing = lookupTinyUrl(token);
    }

    TinyUrl url = new TinyUrl();
    Config c = new Config();
    url.setUrl(urlIn);
    url.setEnabled(true);
    url.setToken(token);
    Calendar pcal = Calendar.getInstance();
    pcal.setTime(expires);
    pcal.add(Calendar.HOUR, c.getInt("server.satellite.tiny_url_timeout", 4));
    url.setExpires(new Date(pcal.getTimeInMillis()));
    return url;
}

From source file:ymanv.forex.provider.impl.EuropeanCentralBank.java

private List<RateEntity> getRates(String response, boolean usdBase) throws IOException {
    SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMAT_STRNG);
    Calendar sfCalendar = sf.getCalendar();
    sfCalendar.setTimeZone(TZ);//  ww  w . j a v a 2  s  .c  om

    List<RateEntity> rates = new ArrayList<>();

    String cleanResponse = response.substring(response.indexOf("<Cube>"), response.lastIndexOf("</Cube>") + 7);
    try {
        JAXBContext context = JAXBContext.newInstance(Result.class);
        Unmarshaller un = context.createUnmarshaller();
        Result res = (Result) un.unmarshal(new StringReader(cleanResponse));

        for (Day d : res.getDays()) {
            Date date = d.getDate();
            sf.parse(sf.format(date));
            sfCalendar.set(Calendar.HOUR, 15);
            date = sf.getCalendar().getTime();

            if (!usdBase) {
                for (Rate r : d.getRates()) {
                    rates.add(new RateEntity(BASE_CURRENCY, r.getTocur(), new BigDecimal(r.getValue()), date));
                }
            } else {
                BigDecimal usdToEurValue = null;

                for (Rate r : d.getRates()) {
                    if (USD.equals(r.getTocur())) {
                        usdToEurValue = invert(new BigDecimal(r.getValue()));
                        break;
                    }
                }

                for (Rate r : d.getRates()) {
                    if (USD.equals(r.getTocur())) {
                        rates.add(new RateEntity(USD, BASE_CURRENCY, usdToEurValue, date));
                    } else {
                        rates.add(new RateEntity(USD, r.getTocur(),
                                new BigDecimal(r.getValue()).multiply(usdToEurValue), date));
                    }
                }
            }
        }

        return rates;
    } catch (ParseException | JAXBException e) {
        throw new IOException(e);
    }
}