Example usage for java.util GregorianCalendar set

List of usage examples for java.util GregorianCalendar set

Introduction

In this page you can find the example usage for java.util GregorianCalendar set.

Prototype

public final void set(int year, int month, int date, int hourOfDay, int minute, int second) 

Source Link

Document

Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, MINUTE, and SECOND.

Usage

From source file:org.sakaiproject.calendar.impl.BaseCalendarService.java

/**
 * Returns a list of daily time ranges for every day in a range.
 * /*  w  ww  .  j a  va 2 s.  c  om*/
 * @param timeRange
 *        overall time range
 * @param dailyTimeRange
 *        representative daily time range (start hour/minute, end hour/minute). If null, this parameter is ignored.
 */
protected ArrayList splitTimeRangeIntoListOfSingleDayTimeRanges(TimeRange timeRange, TimeRange dailyTimeRange) {

    TimeBreakdown startBreakdown = timeRange.firstTime().breakdownLocal();
    TimeBreakdown endBreakdown = timeRange.lastTime().breakdownLocal();

    GregorianCalendar startCalendarDate = new GregorianCalendar();
    startCalendarDate.set(startBreakdown.getYear(), startBreakdown.getMonth() - 1, startBreakdown.getDay(), 0,
            0, 0);

    long numDaysInTimeRange = getNumberDaysGivenTwoDates(startBreakdown.getYear(),
            startBreakdown.getMonth() - 1, startBreakdown.getDay(), endBreakdown.getYear(),
            endBreakdown.getMonth() - 1, endBreakdown.getDay());

    ArrayList splitTimeRanges = new ArrayList();

    TimeBreakdown dailyStartBreakDown = null;
    TimeBreakdown dailyEndBreakDown = null;

    if (dailyTimeRange != null) {
        dailyStartBreakDown = dailyTimeRange.firstTime().breakdownLocal();
        dailyEndBreakDown = dailyTimeRange.lastTime().breakdownLocal();
    }

    for (long i = 0; i < numDaysInTimeRange; i++) {
        Time curStartTime = null;
        Time curEndTime = null;

        if (dailyTimeRange != null) {
            //
            // Use the same start/end times for all days.
            //
            curStartTime = m_timeService.newTimeLocal(startCalendarDate.get(GregorianCalendar.YEAR),
                    startCalendarDate.get(GregorianCalendar.MONTH) + 1,
                    startCalendarDate.get(GregorianCalendar.DAY_OF_MONTH), dailyStartBreakDown.getHour(),
                    dailyStartBreakDown.getMin(), dailyStartBreakDown.getSec(), dailyStartBreakDown.getMs());

            curEndTime = m_timeService.newTimeLocal(startCalendarDate.get(GregorianCalendar.YEAR),
                    startCalendarDate.get(GregorianCalendar.MONTH) + 1,
                    startCalendarDate.get(GregorianCalendar.DAY_OF_MONTH), dailyEndBreakDown.getHour(),
                    dailyEndBreakDown.getMin(), dailyEndBreakDown.getSec(), dailyEndBreakDown.getMs());

            splitTimeRanges.add(m_timeService.newTimeRange(curStartTime, curEndTime, true, false));
        } else {
            //
            // Add a full day range since no start/stop time was specified.
            //
            splitTimeRanges.add(getFullDayTimeRangeFromYMD(startCalendarDate.get(GregorianCalendar.YEAR),
                    startCalendarDate.get(GregorianCalendar.MONTH) + 1,
                    startCalendarDate.get(GregorianCalendar.DAY_OF_MONTH)));
        }

        // Move to the next day.
        startCalendarDate.add(GregorianCalendar.DATE, 1);
    }

    return splitTimeRanges;
}

From source file:org.sakaiproject.calendar.impl.BaseCalendarService.java

/**
 * Calculate the number of days in a range of time given two dates.
 * //from  w  ww.j  av  a2  s .c o  m
 * @param startMonth
 *        (zero based, 0-11)
 * @param startDay
 *        (one based, 1-31)
 * @param endYear
 *        (one based, 1-31)
 * @param endMonth
 *        (zero based, 0-11
 */
protected long getNumberDaysGivenTwoDates(int startYear, int startMonth, int startDay, int endYear,
        int endMonth, int endDay) {
    GregorianCalendar startDate = new GregorianCalendar();
    GregorianCalendar endDate = new GregorianCalendar();

    startDate.set(startYear, startMonth, startDay, 0, 0, 0);
    endDate.set(endYear, endMonth, endDay, 0, 0, 0);

    long duration = endDate.getTime().getTime() - startDate.getTime().getTime();

    // Allow for daylight savings time.
    return ((duration + MILLISECONDS_IN_HOUR) / (24 * MILLISECONDS_IN_HOUR)) + 1;
}

From source file:org.sakaiproject.calendar.impl.BaseCalendarService.java

/**
 * Generates a list of time ranges for a week. Each range in the list is a day.
 * //w  w w .  j  av a2  s  .  c o  m
 * @param timeRange start & end date range
 * @param calendarReferenceList list of calendar(s) 
 * @param dailyTimeRange start and end hour/minute time range
 */
protected ArrayList getTimeRangeListForWeek(TimeRange timeRange, List calendarReferenceList,
        TimeRange dailyTimeRange) {
    TimeBreakdown startBreakdown = timeRange.firstTime().breakdownLocal();

    GregorianCalendar startCalendarDate = (GregorianCalendar) GregorianCalendar
            .getInstance(m_timeService.getLocalTimeZone(), rb.getLocale());
    startCalendarDate.set(startBreakdown.getYear(), startBreakdown.getMonth() - 1, startBreakdown.getDay(), 0,
            0, 0);

    ArrayList weekDayTimeRanges = new ArrayList();

    TimeBreakdown startBreakDown = dailyTimeRange.firstTime().breakdownLocal();

    TimeBreakdown endBreakDown = dailyTimeRange.lastTime().breakdownLocal();

    // Search all seven weekdays
    // Note: no assumption can be made regarding the first day being Sunday, 
    // since in some locales, the first weekday is Monday.
    for (int i = 0; i <= 6; i++) {
        //
        // Use the same start/end times for all days.
        //
        Time curStartTime = m_timeService.newTimeLocal(startCalendarDate.get(GregorianCalendar.YEAR),
                startCalendarDate.get(GregorianCalendar.MONTH) + 1,
                startCalendarDate.get(GregorianCalendar.DAY_OF_MONTH), startBreakDown.getHour(),
                startBreakDown.getMin(), startBreakDown.getSec(), startBreakDown.getMs());

        Time curEndTime = m_timeService.newTimeLocal(startCalendarDate.get(GregorianCalendar.YEAR),
                startCalendarDate.get(GregorianCalendar.MONTH) + 1,
                startCalendarDate.get(GregorianCalendar.DAY_OF_MONTH), endBreakDown.getHour(),
                endBreakDown.getMin(), endBreakDown.getSec(), endBreakDown.getMs());

        TimeRange newTimeRange = m_timeService.newTimeRange(curStartTime, curEndTime, true, false);
        weekDayTimeRanges.add(newTimeRange);

        // Move to the next day.
        startCalendarDate.add(GregorianCalendar.DATE, 1);
    }

    return weekDayTimeRanges;
}