jdk1.8后 关于时间的工具类

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));
    }


}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值