Java TimeUnit Usage getDays(long endTs, Long lookback)

Here you can find the source of getDays(long endTs, Long lookback)

Description

get Days

License

Apache License

Declaration

public static List<Date> getDays(long endTs, Long lookback) 

Method Source Code

//package com.java2s;
/**/* w  w  w  .java2s  .c  om*/
 * Copyright 2015-2016 The OpenZipkin Authors
 *
 * 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.*;
import java.util.concurrent.TimeUnit;

public class Main {
    static final TimeZone UTC = TimeZone.getTimeZone("UTC");

    public static List<Date> getDays(long endTs, Long lookback) {
        long to = midnightUTC(endTs);
        long from = midnightUTC(endTs - (lookback != null ? lookback : endTs));

        List<Date> days = new ArrayList<>();
        for (long time = from; time <= to; time += TimeUnit.DAYS.toMillis(1)) {
            days.add(new Date(time));
        }
        return days;
    }

    /** For bucketed data floored to the day. For example, dependency links. */
    public static long midnightUTC(long epochMillis) {
        Calendar day = Calendar.getInstance(UTC);
        day.setTimeInMillis(epochMillis);
        day.set(Calendar.MILLISECOND, 0);
        day.set(Calendar.SECOND, 0);
        day.set(Calendar.MINUTE, 0);
        day.set(Calendar.HOUR_OF_DAY, 0);
        return day.getTimeInMillis();
    }
}

Related

  1. getDate(final int oceanTime)
  2. getDateBefore(final Date day)
  3. getDateWithoutTime(final Calendar calendar)
  4. getDayFromTimestamp(final long unixTimestamp)
  5. getDays(final long ms)
  6. getDeltaBetweenTimestamps(final long start, final long end)
  7. getDifferenceDays(Date d1, Date d2)
  8. getDiffInMinutes(Calendar startTime, Calendar endTime)
  9. getDurationBreakdown(long millis)