Java TimeUnit Usage getNormalizedIntervalStartTimestamp(long intervalLengthMillis, long currentTimestamp)

Here you can find the source of getNormalizedIntervalStartTimestamp(long intervalLengthMillis, long currentTimestamp)

Description

get Normalized Interval Start Timestamp

License

Apache License

Declaration

public static long getNormalizedIntervalStartTimestamp(long intervalLengthMillis, long currentTimestamp) 

Method Source Code


//package com.java2s;
/*//  w ww . j a  v a2  s.c  o m
 Copyright 2013 Yen Pai ypai@reign.io
    
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
    
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import java.util.Calendar;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

public class Main {
    public static long getNormalizedIntervalStartTimestamp(long intervalLength, TimeUnit intervalLengthTimeUnit,
            long currentTimestamp) {
        return getNormalizedIntervalStartTimestamp(intervalLengthTimeUnit.toMillis(intervalLength),
                currentTimestamp);
    }

    public static long getNormalizedIntervalStartTimestamp(long intervalLengthMillis, long currentTimestamp) {

        long intervalStartTimestamp;

        /***** check interval lengths and create a normalized starting point, so all nodes are on the same interval clock *****/
        if (intervalLengthMillis >= 3600000) {
            // interval >= hour, set to previous hour start point
            Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            cal.setTimeInMillis(currentTimestamp);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            intervalStartTimestamp = cal.getTimeInMillis();
        } else if (intervalLengthMillis >= 1800000) {
            // interval >= 30 minutes, set to previous half-hour start point
            Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            cal.setTimeInMillis(currentTimestamp);

            if (cal.get(Calendar.MINUTE) >= 30) {
                cal.set(Calendar.MINUTE, 30);
            } else {
                cal.set(Calendar.MINUTE, 0);
            }

            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            intervalStartTimestamp = cal.getTimeInMillis();
        } else if (intervalLengthMillis >= 900000) {
            // interval >= 15 minutes, set to nearest previous quarter hour
            // start point
            Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            cal.setTimeInMillis(currentTimestamp);

            int minutes = cal.get(Calendar.MINUTE);
            int diff = minutes % 15;
            cal.set(Calendar.MINUTE, minutes - diff);

            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            intervalStartTimestamp = cal.getTimeInMillis();

        } else if (intervalLengthMillis >= 600000) {
            // interval >= 10 minutes, set to nearest previous 10 minute
            // start point
            Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            cal.setTimeInMillis(currentTimestamp);

            int minutes = cal.get(Calendar.MINUTE);
            int diff = minutes % 10;
            cal.set(Calendar.MINUTE, minutes - diff);

            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            intervalStartTimestamp = cal.getTimeInMillis();

        } else if (intervalLengthMillis >= 300000) {
            // interval >= 5 minutes, set to nearest previous 5 minute start
            // point
            Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            cal.setTimeInMillis(currentTimestamp);

            int minutes = cal.get(Calendar.MINUTE);
            int diff = minutes % 5;
            cal.set(Calendar.MINUTE, minutes - diff);

            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            intervalStartTimestamp = cal.getTimeInMillis();

        } else if (intervalLengthMillis >= 60000) {
            // interval >= 1 minute, set to nearest previous minute start point
            Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            cal.setTimeInMillis(currentTimestamp);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            intervalStartTimestamp = cal.getTimeInMillis();
        } else {
            // smaller resolutions we just start whenever
            intervalStartTimestamp = currentTimestamp;
        }

        return intervalStartTimestamp;
    }
}

Related

  1. getLongFromQueue(BlockingQueue queue)
  2. getMailServerProps(String toAddress, int smtpPort, int imapPort)
  3. getMinutes(final long milliseconds)
  4. getMinutesBetweenDates(Date begin_date, Date end_date)
  5. getMinutesBetweenDates(Date firstDate, Date secondDate)
  6. getNowInSeconds()
  7. getOceanTime(final Date date)
  8. getPrettyTime(long duration)
  9. getQuantilesString(final Map quantilesValues)