Java Calendar Calculate alignHours(int interval, Calendar timestamp)

Here you can find the source of alignHours(int interval, Calendar timestamp)

Description

Aligns the time fields to the start of given interval.

License

Apache License

Parameter

Parameter Description
interval The number of hours in the interval to align to.
timestamp The calendar to align.

Return

The calendar parameter, aligned.

Declaration

public static Calendar alignHours(int interval, Calendar timestamp) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**/* w  w w  . j av a 2  s .  com*/
     * Aligns the time fields to the start of given interval.
     * @param interval The number of hours in the interval to align to.
     * @param timestamp The calendar to align.
     * @return The calendar parameter, aligned.
     */
    public static Calendar alignHours(int interval, Calendar timestamp) {
        int value = timestamp.get(Calendar.HOUR_OF_DAY);
        value = value - (value % interval);
        timestamp.set(Calendar.HOUR_OF_DAY, value);
        return alignHour(timestamp);
    }

    /**
     * Aligns the time fields to the start of the hour.
     * @param timestamp The calendar to align.
     * @return The parameter.
     */
    public static Calendar alignHour(Calendar timestamp) {
        timestamp.set(Calendar.MINUTE, 0);
        timestamp.set(Calendar.SECOND, 0);
        timestamp.set(Calendar.MILLISECOND, 0);
        return timestamp;
    }
}

Related

  1. addQuarter(Calendar calendar, int quarterDelta)
  2. addTime(int calendarType, long time, int number)
  3. alignSecond(Calendar timestamp)
  4. cal2Str(Calendar pCal)
  5. calcAge(Calendar dateOfBirth, Calendar now)
  6. calcDifferenceAsDays(Calendar aBaseDate, Calendar aTargetDate)