Java TimeUnit Usage millisToDays(long millisLocal)

Here you can find the source of millisToDays(long millisLocal)

Description

Return number of days since the unix epoch.

License

Apache License

Declaration

private static int millisToDays(long millisLocal) 

Method Source Code

//package com.java2s;
/*//w w  w.j a  v  a  2s. c  o m
 * Copyright 2017 StreamSets Inc.
 *
 * 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 {
    private static final long MILLIS_PER_DAY = TimeUnit.DAYS.toMillis(1);
    private static TimeZone localTimeZone = Calendar.getInstance().getTimeZone();

    /**
     * Return number of days since the unix epoch.
     *
     * This function has been copied from Apache Hive project.
     */
    private static int millisToDays(long millisLocal) {
        // We assume millisLocal is midnight of some date. What we are basically trying to do
        // here is go from local-midnight to UTC-midnight (or whatever time that happens to be).
        long millisUtc = millisLocal + localTimeZone.getOffset(millisLocal);
        int days;
        if (millisUtc >= 0L) {
            days = (int) (millisUtc / MILLIS_PER_DAY);
        } else {
            days = (int) ((millisUtc - 86399999 /*(MILLIS_PER_DAY - 1)*/) / MILLIS_PER_DAY);
        }
        return days;
    }
}

Related

  1. logWatchStop(long start, String... params)
  2. mergeResults(List>> futures)
  3. millisBetweenNanoTimes(long startNanos, long endNanos)
  4. millisecondsToHumanReadable(long duration)
  5. millisElapsedSince(long startNanoTime)
  6. millisToHMSms(long millis)
  7. millisToReadableTime(long milliseconds)
  8. millisToString(double millis)
  9. millisToString(long millis)