1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.contacts.common.util;
18 
19 import java.time.Instant;
20 import java.time.LocalDate;
21 import java.time.ZoneId;
22 import java.time.temporal.ChronoUnit;
23 
24 
25 /** Utility methods for processing dates. */
26 public class DateUtils {
27 
28   /**
29    * Determine the difference, in days between two dates. Uses similar logic as the {@link
30    * android.text.format.DateUtils.getRelativeTimeSpanString} method.
31    *
32    * @param time Instance of time object to use for calculations.
33    * @param date1 First date to check.
34    * @param date2 Second date to check.
35    * @return The absolute difference in days between the two dates.
36    */
getDayDifference(ZoneId timeZone, long date1, long date2)37   public static int getDayDifference(ZoneId timeZone, long date1, long date2) {
38     LocalDate localDate1 = Instant.ofEpochMilli(date1).atZone(timeZone).toLocalDate();
39     LocalDate localDate2 = Instant.ofEpochMilli(date2).atZone(timeZone).toLocalDate();
40     return Math.abs((int) ChronoUnit.DAYS.between(localDate2, localDate1));
41   }
42 }
43