Java tutorial
//package com.java2s; /* * Copyright (C) 2016 The Android Open Source Project * * 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.concurrent.TimeUnit; public class Main { public static final long DAY_IN_MILLIS = TimeUnit.DAYS.toMillis(1); /** * Normalizes a date (in milliseconds). * * Normalize, in our usage within Sunshine means to convert a given date in milliseconds to * the very beginning of the date in UTC time. * * For example, given the time representing * * Friday, 9/16/2016, 17:45:15 GMT-4:00 DST (1474062315000) * * this method would return the number of milliseconds (since the epoch) that represents * * Friday, 9/16/2016, 00:00:00 GMT (1473984000000) * * To make it easy to query for the exact date, we normalize all dates that go into * the database to the start of the day in UTC time. In order to normalize the date, we take * advantage of simple integer division, noting that any remainder is discarded when dividing * two integers. * * For example, dividing 7 / 3 (when using integer division) equals 2, not 2.333 repeating * as you may expect. * * @param date The date (in milliseconds) to normalize * * @return The UTC date at 12 midnight of the date */ public static long normalizeDate(long date) { long daysSinceEpoch = elapsedDaysSinceEpoch(date); long millisFromEpochToTodayAtMidnightUtc = daysSinceEpoch * DAY_IN_MILLIS; return millisFromEpochToTodayAtMidnightUtc; } /** * This method returns the number of days since the epoch (January 01, 1970, 12:00 Midnight UTC) * in UTC time from the current date. * * @param utcDate A date in milliseconds in UTC time. * * @return The number of days from the epoch to the date argument. */ private static long elapsedDaysSinceEpoch(long utcDate) { return TimeUnit.MILLISECONDS.toDays(utcDate); } }