Java TimeUnit Usage getLastSlotDate(final int occurrences, final int periodicity, final long firstSlotDate, final int firstSlotDay, final String selectedDays)

Here you can find the source of getLastSlotDate(final int occurrences, final int periodicity, final long firstSlotDate, final int firstSlotDay, final String selectedDays)

Description

get Last Slot Date

License

Open Source License

Parameter

Parameter Description
occurrences : number of occurrences
periodicity : slots are repeated every "periodicity" week
firstSlotDate : Unix timestamp of the first slot's date (start or end date)
firstSlotDay : index of the first slot's day
selectedDays : bit string, used like an array of boolean, representing selected days. Char 0 is sunday, char 1 monday...

Exception

Parameter Description
IllegalArgumentException , IndexOutOfBoundsException

Return

Unix timestamp of the last slot's date (start or end date)

Declaration

public static long getLastSlotDate(final int occurrences, final int periodicity, final long firstSlotDate,
        final int firstSlotDay, final String selectedDays) 

Method Source Code

//package com.java2s;
/*//from  ww w .j  a v a 2s .c  o m
 * Copyright ? R?gion Nord Pas de Calais-Picardie,  D?partement 91, R?gion Aquitaine-Limousin-Poitou-Charentes, 2016.
 *
 * This file is part of OPEN ENT NG. OPEN ENT NG is a versatile ENT Project based on the JVM and ENT Core Project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation (version 3 of the License).
 *
 * For the sake of explanation, any module that communicate over native
 * Web protocols, such as HTTP, with OPEN ENT NG is outside the scope of this
 * license and could be license under its own terms. This is merely considered
 * normal use of OPEN ENT NG, and does not fall under the heading of "covered work".
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     *
     * @param occurrences : number of occurrences
     * @param periodicity : slots are repeated every "periodicity" week
     * @param firstSlotDate : Unix timestamp of the first slot's date (start or end date)
     * @param firstSlotDay : index of the first slot's day
     * @param selectedDays : bit string, used like an array of boolean, representing selected days. Char 0 is sunday, char 1 monday...
     * @return Unix timestamp of the last slot's date (start or end date)
     *
     * @throws IllegalArgumentException, IndexOutOfBoundsException
     */
    public static long getLastSlotDate(final int occurrences, final int periodicity, final long firstSlotDate,
            final int firstSlotDay, final String selectedDays) {

        long lastSlotDate = firstSlotDate;
        if (occurrences > 1) {
            int selectedDay = firstSlotDay;
            int intervalFromFirstDay = 0;

            for (int i = 1; i <= (occurrences - 1); i++) {
                int interval = getNextInterval(selectedDay, selectedDays, periodicity);
                intervalFromFirstDay += interval;
                selectedDay = (selectedDay + interval) % 7;
            }
            lastSlotDate += TimeUnit.SECONDS.convert(intervalFromFirstDay, TimeUnit.DAYS);
        }

        return lastSlotDate;
    }

    /**
     *
     * @param lastSelectedDay : index of last selected day
     * @param selectedDays : bit string, used like an array of boolean, representing selected days. Char 0 is sunday, char 1 monday...
     * @param periodicity : slots are repeated every "periodicity" week
     * @return Number of days until next slot
     *
     * @throws IllegalArgumentException, IndexOutOfBoundsException
     */
    public static int getNextInterval(final int lastSelectedDay, final String selectedDays, final int periodicity) {
        if (!selectedDays.contains("1")) {
            throw new IllegalArgumentException("Argument selectedDays must contain char '1'");
        }

        int count = 0;
        int k = lastSelectedDay;

        do {
            k++;
            if (k == 1) {
                // On monday, go to next week
                count += (periodicity - 1) * 7;
            } else if (k >= 7) {
                k = k % 7;
            }
            count++;
        } while (selectedDays.charAt(k) != '1');

        return count;
    }
}

Related

  1. getFormattedTime(long timeleft)
  2. getFormattedTimeForMS(Long timeInMillis)
  3. getFriendlyTime(long duration)
  4. getInsertionEndIndex(List list, Delayed key)
  5. getInStringSeconds(String pollInterval)
  6. getLongFromQueue(BlockingQueue queue)
  7. getMailServerProps(String toAddress, int smtpPort, int imapPort)
  8. getMinutes(final long milliseconds)
  9. getMinutesBetweenDates(Date begin_date, Date end_date)