Java Calendar Calculate roundBack(Calendar start)

Here you can find the source of roundBack(Calendar start)

Description

Rounds a date backwards if it found to be before th start of a work day.

License

Apache License

Declaration

public static void roundBack(Calendar start) 

Method Source Code

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

import java.util.*;

public class Main {
    /**// w  ww .ja  v a 2s  .c o m
     * The default when the workday ends (5PM)
     */
    public static final int END_WORKDAY = 17;
    /**
     * 1 minute, represented as millis
     */
    public static final long MINUTE = 1000 * 60;
    /**
     * The default when the workday starts (7AM)
     */
    public static final int START_WORKDAY = 7;

    /**
     * Rounds a date backwards if it found to be before th start of a work day.
     * It will round it back to the end of the previous work day
     *
     * @see #START_WORKDAY
     * @see #END_WORKDAY
     * @see #roundForward(Calendar)
     */
    public static void roundBack(Calendar start) {
        if (!(isInWorkDay(start) && isInWorkTime(start))) {
            if (isBeforeWorkTime(start) || !isInWorkDay(start)) {
                start.add(Calendar.DATE, -1);
            }

            if (start.get(Calendar.DAY_OF_WEEK) == 1) {
                start.add(Calendar.DATE, -2);
            }

            if (start.get(Calendar.DAY_OF_WEEK) == 7) {
                start.add(Calendar.DATE, -1);
            }

            start.set(Calendar.HOUR_OF_DAY, END_WORKDAY);
            start.set(Calendar.MINUTE, 0);
        }
    }

    /**
     * Determines whether or not the date falls on a work day (Mon-Fri)
     */
    public static boolean isInWorkDay(Calendar start) {
        final int dayOfWeek = start.get(Calendar.DAY_OF_WEEK);
        return dayOfWeek > 1 && dayOfWeek < 7;
    }

    /**
     * Determines whether or not the time of the date is within the work day
     *
     * @see #START_WORKDAY
     * @see #END_WORKDAY
     * @see #isBeforeWorkTime(Calendar)
     * @see #isAfterWorkTime(Calendar)
     */
    public static boolean isInWorkTime(Calendar start) {
        return !isBeforeWorkTime(start) && !isAfterWorkTime(start);
    }

    /**
     * Determines whether or not the date is before the start of the work day
     *
     * @see #START_WORKDAY
     */
    public static boolean isBeforeWorkTime(Calendar start) {
        final int hourOfDay = start.get(Calendar.HOUR_OF_DAY);
        return hourOfDay < START_WORKDAY;
    }

    /**
     * Determines whether or not the date is after the start of the work day
     *
     * @see #END_WORKDAY
     */
    public static boolean isAfterWorkTime(Calendar start) {
        final int hourOfDay = start.get(Calendar.HOUR_OF_DAY);
        return hourOfDay >= END_WORKDAY;
    }
}

Related

  1. millisToCalendar(long millis)
  2. normalize(final Calendar cal)
  3. normalizedClone(final Calendar cal)
  4. postpone(final Calendar calendar, final int measureUnit, final int amount)
  5. resetCalendarTime(Calendar calendar)
  6. roundCalToNextQuarterHour(GregorianCalendar cal)
  7. transform(Calendar cal)
  8. truncate(Calendar ca)
  9. truncateHour(Calendar ca)