package com.util;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
/**
* jdk 1.8以后,线程安全的获取时间工具
*/
public class DataUtils {
public static String ZONEOFFSET = "+8";
public static String ZONE_ID = "Asia/Shanghai";
public static String PATTEN_DATE = "yyyyMMdd";
public static String PATTEN_TIME = "yyyyMMddHHmmss";
/**
* 获取现在的毫秒值
* @return Long类型毫秒值
*/
public static Long getNowMillSecond() {
return Instant.now().toEpochMilli();
}
/**
* 获取距离现在时间相差offSetDay时间的时间
* @param offSet 相差的时间
* @param offSetType 相差的时间的单位 0-6 时分秒年月日周
* @return OffSetDateTime
*/
public static OffsetDateTime getNowTime(long offSet, int offSetType) {
OffsetDateTime offsetDateTime= null;
switch (offSetType) {
case 0:
offsetDateTime = OffsetDateTime.now().plusDays(offSet);
break;
case 1:
offsetDateTime = OffsetDateTime.now().plusMonths(offSet);
break;
case 2:
offsetDateTime = OffsetDateTime.now().plusYears(offSet);
break;
case 3:
offsetDateTime = OffsetDateTime.now().plusHours(offSet);
break;
case 4:
offsetDateTime = OffsetDateTime.now().plusMinutes(offSet);
break;
case 5:
offsetDateTime = OffsetDateTime.now().plusSeconds(offSet);
break;
case 6:
offsetDateTime = OffsetDateTime.now().plusWeeks(offSet);
break;
default:
break;
}
return offsetDateTime;
}
/**
* 获取某一时间当天的00点
* @param offsetDateTime 某一天
* @return 某一天的00点
*/
public static OffsetDateTime getStartTimeOfDay(OffsetDateTime offsetDateTime) {
LocalDateTime localDateTime = offsetDateTime.toLocalDate().atStartOfDay();
return OffsetDateTime.of(localDateTime,ZoneOffset.of(ZONEOFFSET));
}
/**
* 获取某一时间当天的23:59:59
* @param offsetDateTime
* @return
*/
public static OffsetDateTime getEndTimeOfDay(OffsetDateTime offsetDateTime) {
return offsetDateTime.toLocalDate().plus(1L,
ChronoUnit.DAYS).atStartOfDay().minusSeconds(1L).atOffset(ZoneOffset.of(ZONEOFFSET));
}
/**
* 距离现在相差(天、月、年)的那天的的月初的日期
* @param offSet 相差的时间
* @param offSetType 相差的时间的单位(天、月、年)
* @return LocalDate日期
*/
/* public static LocalDate getMonthFirstTime(long offSet, int offSetType) {
return getNowDate(offSet,offSetType).with(TemporalAdjusters.firstDayOfMonth());
}*/
/**
* startTime到endTime之间的毫秒数
* @param startTime 起始时间
* @param endTime 结束时间
* @return 毫秒数
*/
public static long getDurationEpochMilli(LocalDateTime startTime,LocalDateTime endTime) {
return Duration.between(startTime, endTime).toMillis();
}
/**
* LocalDateTime转EpochMilli
* @param localDateTime
* @return EpochMilli
*/
public static Long localtime2EpochMilli(LocalDateTime localDateTime) {
return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* localDate转转EpochMilli
* @param localDate 日期
* @return 毫秒
*/
public static Long localDate2EpochMilli(LocalDate localDate) {
return localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* 毫秒转日期
* @param epochMilli 毫秒
* @return localDateTime xx年xx月xx日xx时xx分xx秒
*/
public static LocalDateTime EpochMilli2LocalDateTime(Long epochMilli) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli),ZoneId.systemDefault());
}
/**
* 获取具体currentTime相差offSetMonth个月在day天的0点时间
* @param currentTime 基准时间
* @param offSetMonth 相差的月数有正负
* @param day 天
* @return 毫秒
*/
public static Long getMonthOffFirstDay(long currentTime,long offSetMonth, int day){
Instant instant = Instant.ofEpochMilli(currentTime);
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, ZoneId.systemDefault())
.plusMonths(offSetMonth);
int year = offsetDateTime.getYear();
int month = offsetDateTime.getMonthValue();
long time = LocalDate.of(year,month,day).atStartOfDay()
.toInstant(ZoneOffset.of(ZONEOFFSET)).toEpochMilli();
return time;
}
/**
* 获取距离currentTime相差offSetMonth个月在day天的0点时间
* @param currentTime 基准时间
* @param offSetMonth 相差的月数有正负
* @param day 天
* @return 毫秒
*/
public static Long getMonthOffOneDayTime(long currentTime,long offSetMonth, int day){
Instant instant = Instant.ofEpochMilli(currentTime);
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, ZoneId.systemDefault())
.plusMonths(offSetMonth);
int year = offsetDateTime.getYear();
int month = offsetDateTime.getMonthValue();
long time = LocalDate.of(year,month,day).atStartOfDay()
.toInstant(ZoneOffset.of(ZONEOFFSET)).toEpochMilli();
return time;
}
/**
* 获取currentTime所在那天的0点毫秒值
* @param currentTime 基准时间
* @return 毫秒值
*/
public static Long getTodayStartTime(long currentTime){
Instant instant = Instant.ofEpochMilli(currentTime);
return instant.atZone(ZoneId.of(ZONE_ID)).toLocalDate().atStartOfDay()
.toInstant(ZoneOffset.of(ZONEOFFSET)).toEpochMilli();
}
/*public static Boolean isBeforeLocalDate(long currentTime, long standardTime){
return Instant.ofEpochMilli(currentTime).isBefore(Instant.ofEpochMilli(standardTime));
}*/
/**
* 获取currentTime所在这天的最后一秒23点59分59秒的毫秒值
* @param currentTime 基准时间
* @return
*/
public static Long getTodayEndTime(long currentTime){
Instant instant = Instant.ofEpochMilli(currentTime);
return instant.atZone(ZoneId.of(ZONE_ID)).toLocalDate().atTime(LocalTime.MAX)
.toInstant(ZoneOffset.of(ZONEOFFSET)).toEpochMilli();
}
/**
* 获取当前时间所在月的最后一天
* @param currentTime 基准时间
* @return 日期
*/
public static String getMaxDate(long currentTime) {
Instant instant = Instant.ofEpochMilli(currentTime);
return LocalDateTime.ofInstant(instant,ZoneId.of(ZONE_ID))
.with(TemporalAdjusters.lastDayOfMonth()).toLocalDate().toString();
}
/**
* 毫秒转日期
* @param currentTime
* @return 2020-03-17
*/
public static LocalDate long2Date(long currentTime) {
return Instant.ofEpochMilli(currentTime).atZone(ZoneId.of(ZONE_ID)).toLocalDate();
}
/**
* 毫秒转String日期yyyyMMdd
* @param currentTime
* @return
*/
public static String long2DateString(long currentTime) {
LocalDate localDate = Instant.ofEpochMilli(currentTime).atZone(ZoneId.of(ZONE_ID)).toLocalDate();
return localDate2String(localDate);
}
/**
* 日期转成String类型 yyyyMMdd
* @param currentDate
* @return 20200323
*/
public static String localDate2String(LocalDate currentDate) {
return currentDate.format(DateTimeFormatter.ofPattern(PATTEN_DATE));
}
/**
* 毫秒值转成String类型yyyyMMddHHmmss
* @param currentTime
* @return
*/
public static String localTime2String(long currentTime) {
return Instant.ofEpochMilli(currentTime).atZone(ZoneId.of(ZONE_ID)).format(DateTimeFormatter.ofPattern(PATTEN_TIME));
}
/**
* 比较两个(毫秒)所在日期的大小
* @param time1
* @param time2
* @return 1 -- time1所在日期大于 time2所在日期 0--相等; -1--小于;
*/
public static int compare2Date(long time1, long time2) {
LocalDate localDate1 = long2Date(time1);
LocalDate localDate2 = long2Date(time2);
return localDate1.compareTo(localDate2);
}
/**
* currentTime是否在standardTime之前
* @param currentTime
* @param standardTime 基准时间
* @return
*/
public static Boolean isBeforeLocalDate(long currentTime, long standardTime){
return Instant.ofEpochMilli(currentTime).isBefore(Instant.ofEpochMilli(standardTime));
}
}