日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

LocalDateTimeUtils

發布時間:2023/12/13 综合教程 26 生活家
生活随笔 收集整理的這篇文章主要介紹了 LocalDateTimeUtils 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import org.assertj.core.util.Lists;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.*;
import java.util.Date;
import java.util.List;

public class LocalDateTimeUtils {
    public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";
    public static final String DATE_TIME = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_MINUTE_TIME = "yyyy-MM-dd HH:mm";
    public static final String DATE_TIME_MILLIS = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATE_HOUR_TIME = "yyyy-MM-dd HH";

    //獲取當前時間前,幾個時間單位的整小時時間戳(參數為毫秒值)如:參數為1,結果是當前時間前一毫秒的當前整小時時間戳
    public static long getNowMillisPre(Long millis) {
        Long milliByTime = getMilliByTime(LocalDateTime.now()) - millis;
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(milliByTime / 1000, 0, ZoneOffset.ofHours(8));
        String dateTimeStr = formatTime(localDateTime, DATE_HOUR_TIME);
        DateTimeFormatter df = DateTimeFormatter.ofPattern(DATE_HOUR_TIME);
        Long dateTime = LocalDateTime.parse(dateTimeStr, df).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        return dateTime;
    }

    //獲取當前時間的毫秒值
    public static long getNowMillis() {
        return getMilliByTime(LocalDateTime.now());
    }

    //獲取當日凌晨
    public static LocalDateTime getWeeHour() {
        return LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
    }
    //獲取本周周一凌晨
    public static long getMondayMill() {
        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        LocalDateTime monday = localDateTime.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)).plusDays(1).withHour(0).withMinute(0).withSecond(0);
        return getMilliByTime(monday);
    }


    //獲取當日凌晨 秒
    public static long getWeeHourSecend() {
        return getSecondsByTime(getWeeHour());
    }

    public static long getWeeHourSecend(int day) {
        return getSecondsByTime(getWeeHour().minusDays(day));
    }

    public static long getWeeHourMillis(int day) {
        return getMilliByTime(getWeeHour().minusDays(day));
    }

    //獲取當日凌晨 毫秒
    public static long getWeeHourMillis() {
        return getMilliByTime(getWeeHour());
    }

    //獲取幾天前的凌晨
    public static LocalDateTime getWeeHourAgo(int day) {
        return getWeeHour().minusDays(day);
    }

    //獲取幾天前的結束時間
    public static LocalDateTime getEndAgo(int day) {
        return getDayEnd(LocalDateTime.now()).minusDays(day);
    }

    //獲取以當前時間為止,幾小時前的整點 秒值
    public static long getOClockAgo(int number) {
        LocalDateTime hoursAgo = minu(LocalDateTime.now(), number, ChronoUnit.HOURS);
        LocalDateTime oClockAgo = hoursAgo.withMinute(0).withSecond(0).withNano(0);
        return getSecondsByTime(oClockAgo);
    }

    //獲取以當前時間為止,幾小時前的整點 毫秒值
    public static long getOClockAgoMillis(int number) {
        LocalDateTime hoursAgo = minu(LocalDateTime.now(), number, ChronoUnit.HOURS);
        LocalDateTime oClockAgo = hoursAgo.withMinute(0).withSecond(0).withNano(0);
        return getMilliByTime(oClockAgo);
    }

    public static long getOClockAgoMillis(int number, LocalDateTime now) {
        LocalDateTime hoursAgo = minu(now, number, ChronoUnit.HOURS);
        LocalDateTime oClockAgo = hoursAgo.withMinute(0).withSecond(0).withNano(0);
        return getMilliByTime(oClockAgo);
    }

    //獲取以當前時間為止,幾分鐘前的整分鐘 毫秒值
    public static long getMinuteStartAgoMillis(int number) {
        return getMilliByTime(getMinuteStartAgoTime(number));
    }

    public static LocalDateTime getMinuteStartAgoTime(int number) {
        LocalDateTime minutesAgo = minu(LocalDateTime.now(), number, ChronoUnit.MINUTES);
        return minutesAgo.withSecond(0).withNano(0);
    }

    //獲取時間集合。整點小時
    @Deprecated
    public static List<String> getTimeList(int beginNum) {
        List<String> list = Lists.newArrayList();
        LocalDateTime now = LocalDateTime.now();
        for (int i = beginNum; i >= 0; i--) {
            String hour = formatTime(now.minusHours(i), "HH");
            list.add(hour + ":00");
        }
        return list;
    }

    public static List<String> getTimeList(int number, ChronoUnit field, String format) {
        List<String> list = Lists.newArrayList();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime numberAgo = minu(now, number, field);
        int total = number;
        for (int i = 0; i < total; i++) {
            list.add(formatTime(plus(numberAgo, i, field), format));
        }

        return list;
    }

    public static List<String> getTimeList(int number, ChronoUnit field, String format, boolean containNow) {
        List<String> list = Lists.newArrayList();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime numberAgo = minu(now, number, field);
        int total = number;
        if (containNow) {
            total = number + 1;
        }
        for (int i = 0; i < total; i++) {
            list.add(formatTime(plus(numberAgo, i, field), format));
        }

        return list;
    }

    public static List<String> getTimeList(LocalDateTime beginTime, ChronoUnit field, String format) {
        List<String> list = Lists.newArrayList();
        LocalDateTime now = LocalDateTime.now();
        long diffTwoTime = betweenTwoTime(beginTime, now, field);
        long total = diffTwoTime;
        for (long i = 0; i < total; i++) {
            list.add(formatTime(plus(beginTime, i, field), format));
        }

        return list;
    }

    public static List<String> getTimeList(LocalDateTime beginTime, ChronoUnit field, String format, boolean containNow) {
        List<String> list = Lists.newArrayList();
        LocalDateTime now = LocalDateTime.now();
        long diffTwoTime = betweenTwoTime(beginTime, now, field);
        long total = diffTwoTime;
        if (containNow) {
            total = diffTwoTime + 1;
        }
        for (long i = 0; i < total; i++) {
            list.add(formatTime(plus(beginTime, i, field), format));
        }

        return list;
    }

    public static List<String> getTimeList(ChronoField field, int interval, ChronoUnit fieldUnit, int number, String format) {
        List<String> list = Lists.newArrayList();
        LocalDateTime now = LocalDateTime.now();
        int timeNow = now.get(field);
        int timeToWant = timeNow / interval * interval;
        LocalDateTime timeBegin = now.with(field, timeToWant).minus(number, fieldUnit);
        int total = number / interval;
        for (int i = 0; i < total; i++) {
            list.add(formatTime(plus(timeBegin, i * interval, fieldUnit), format));
        }

        return list;
    }

    public static List<String> getTimeList(ChronoField field, int interval, ChronoUnit fieldUnit, int number, String format, boolean containNow) {
        List<String> list = Lists.newArrayList();
        LocalDateTime now = LocalDateTime.now();
        int timeNow = now.get(field);
        int timeToWant = timeNow / interval * interval;
        LocalDateTime timeBegin = now.with(field, timeToWant).minus(number, fieldUnit);
        int total = number / interval;
        if (containNow) {
            for (int i = 1; i <= total; i++) {
                list.add(formatTime(plus(timeBegin, i * interval, fieldUnit), format));
            }
        } else {
            for (int i = 0; i < total; i++) {
                list.add(formatTime(plus(timeBegin, i * interval, fieldUnit), format));
            }
        }

        return list;
    }

    @Deprecated
    public static List<String> getMinuteList(int countOfHour, int interval, String format) {
        List<String> list = Lists.newArrayList();
        LocalDateTime now = LocalDateTime.now();
        int minuteNow = now.getMinute();
        int minuteToWant = minuteNow / interval * interval;
        LocalDateTime timeBegin = now.withMinute(minuteToWant).minusHours(countOfHour);
        int total = countOfHour * 60 / interval;
        for (int i = 1; i <= total; i++) {
            String time = formatTime(timeBegin.plusMinutes(i * interval), format);
            list.add(time);
        }
        return list;
    }

    public static List<String> getMinuteList(int count, int interval, String format, ChronoUnit field) {
        List<String> list = Lists.newArrayList();
        LocalDateTime now = LocalDateTime.now();
        int minuteNow = now.getMinute();
        int minuteToWant = minuteNow / interval * interval;
        LocalDateTime timeBegin = minu(now.withMinute(minuteToWant), count, ChronoUnit.MINUTES);
        int total = count / interval;
        for (int i = 1; i <= total; i++) {
            String time = formatTime(timeBegin.plusMinutes(i * interval), format);
            list.add(time);
        }
        return list;
    }

    //獲取一天的開始時間,2017,7,22 00:00
    public static LocalDateTime getDayStart(LocalDateTime time) {
        return time.withHour(0).withMinute(0).withSecond(0).withNano(0);
    }

    //獲取一天的結束時間,2017,7,22 23:59:59.999999999
    public static LocalDateTime getDayEnd(LocalDateTime time) {
        return time.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
    }

    //獲取當月第一天。 參數可以是LocalDate、LocalDateTime、LocalTime等。
    //傳不同的參數會得到不同的結果。
    public static Temporal getFirstDayOfMonth(Temporal date) {
        return date.with(TemporalAdjusters.firstDayOfMonth());
    }

    public static Temporal getCopyDateToWant(Temporal date, TemporalAdjuster adjuster) {
        return date.with(adjuster);
    }

    //獲取當月第一天的凌晨
    public static Long getFirstDayOfCurrentMonth() {
        LocalDate firstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        LocalTime weeHour = LocalTime.of(0, 0, 0);
        LocalDateTime firstDayOfCurrentMonth = LocalDateTime.of(firstDay, weeHour);
        return getMilliByTime(firstDayOfCurrentMonth);
    }

    //獲取當年第一天的凌晨
    public static LocalDateTime getDayStartThisYear() {
        LocalDateTime firstDay = (LocalDateTime) getCopyDateToWant(LocalDateTime.now(), TemporalAdjusters.firstDayOfYear());
        LocalDateTime dayStart = getDayStart(firstDay);
        return dayStart;
    }

    //獲取兩個日期的差  field參數為ChronoUnit.*
    public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
        Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
        if (field == ChronoUnit.YEARS) return period.getYears();
        if (field == ChronoUnit.MONTHS) return period.getYears() * 12 + period.getMonths();
        return field.between(startTime, endTime);
    }

    //日期加上一個數,根據field不同加不同值,field為ChronoUnit.*
    public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
        return time.plus(number, field);
    }

    //日期減去一個數,根據field不同減不同值,field參數為ChronoUnit.*
    public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field) {
        return time.minus(number, field);
    }

    //獲取指定時間的指定格式
    public static String formatTime(LocalDateTime time, String pattern) {
        return time.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static String formatDate(LocalDateTime time) {
        return time.format(DateTimeFormatter.ofPattern(YEAR_MONTH_DAY));
    }

    public static String formatDateTime(LocalDateTime time) {
        return time.format(DateTimeFormatter.ofPattern(DATE_TIME));
    }

    //獲取當前時間的指定格式
    public static String formatNow(String pattern) {
        return formatTime(LocalDateTime.now(), pattern);
    }

    //解析指定格式的時間
    public static LocalDateTime parseTime(String time, String pattern) {
        return LocalDateTime.parse(time, DateTimeFormatter.ofPattern(pattern));
    }

    //獲取指定日期的毫秒
    public static Long getMilliByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    //獲取指定日期的秒
    public static Long getSecondsByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    //Date轉換為LocalDateTime
    public static LocalDateTime convertDateToLDT(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    //LocalDateTime轉換為Date
    public static Date convertLDTToDate(LocalDateTime time) {
        return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
    }

    //判斷給定時間是否不在當前這個小時內
    public static boolean judgePassedTimeLine(int hourAgo) {
        LocalDateTime now = LocalDateTime.now();
        if (now.getHour() != hourAgo) {
            return true;
        }
        return false;
    }

}

總結

以上是生活随笔為你收集整理的LocalDateTimeUtils的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。