Created
January 5, 2021 08:30
-
-
Save alonWang/9067cb28b01ac6ecd777451e27961475 to your computer and use it in GitHub Desktop.
判断传入时间与今天是否是同一天的高效实现
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.alonwang.util; | |
import com.google.common.collect.Range; | |
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
import java.time.LocalTime; | |
import java.time.ZoneId; | |
/** | |
* @author alonwang | |
* @date 2021/1/4 21:57 | |
*/ | |
public class TimeUtil { | |
public static final long MILLIS_ONE_DAY = 24 * 60 * 60 * 1000; | |
/** | |
* 今日时间范围,[今日开始时间戳,今日结束时间戳) | |
*/ | |
private static final ThreadLocal<Range<Long>> todayTimeRangeCache = ThreadLocal.withInitial(TimeUtil::createTodayTimeRange); | |
/** | |
* 获取今日0点 | |
* @return | |
*/ | |
public static long getTodayZeroTime() { | |
return LocalDateTime.of(LocalDate.now(), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); | |
} | |
private static Range<Long> createTodayTimeRange() { | |
long todayStartTime = getTodayZeroTime(); | |
long todayEndTime = todayStartTime + MILLIS_ONE_DAY; | |
return Range.closedOpen(todayStartTime, todayEndTime); | |
} | |
/** | |
* 传入时间与今天是否是同一天 | |
* | |
* @param timeMillis | |
* @return | |
*/ | |
public static boolean isSameDayQuick(long timeMillis) { | |
long now = System.currentTimeMillis(); | |
Range<Long> todayTimeRange = todayTimeRangeCache.get(); | |
if (!todayTimeRange.contains(now)) { | |
Range<Long> timeRange = createTodayTimeRange(); | |
todayTimeRangeCache.set(timeRange); | |
todayTimeRange = timeRange; | |
} | |
return todayTimeRange.contains(timeMillis); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment