java时间初始化工具类

本文介绍了一个Java时间操作工具类,提供了丰富的功能,如获取指定时间单位的开始和结束时间戳,判断两个时间是否处于同一日、周、月、年,以及生成时间范围内的时间戳列表。该工具类使用Joda-Time和Java 8时间API,适用于多种时间处理场景。
package com.vortex.hzz.util;

import org.joda.time.DateTime;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

/**
 * @author win7
 * @version 创建时间:2017/7/26 10:44
 * classType:
 */
@SuppressWarnings("unused")
public class TimeUtils {

    public static Long getStartTime(Long time, TimeEnum timeEnum) {
        if (time == null) {
            return null;
        }
        switch (timeEnum) {
            case SECOND:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(),
                        new DateTime(time).getDayOfMonth(), new DateTime(time).getHourOfDay(), new DateTime(time).getMinuteOfHour(),
                        new DateTime(time).getSecondOfMinute(), 0).getMillis();
            case MINUTE:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(),
                        new DateTime(time).getDayOfMonth(), new DateTime(time).getHourOfDay(), new DateTime(time).getMinuteOfHour(),
                        0, 0).getMillis();
            case HOUR:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(),
                        new DateTime(time).getDayOfMonth(), new DateTime(time).getHourOfDay(), 0, 0, 0).getMillis();
            case DAY:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(),
                        new DateTime(time).getDayOfMonth(), 0, 0, 0, 0).getMillis();
            case WEEK:
                return getStartTime(new DateTime(time).plusDays(-new DateTime(time).getDayOfWeek() + 1).getMillis(), TimeEnum.DAY);
            case MONTH:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(), 1, 0, 0, 0, 0).getMillis();
            case YEAR:
                return new DateTime(new DateTime(time).getYear(), 1, 1, 0, 0, 0, 0).getMillis();
            default:
                return null;
        }
    }

    public static Long getEndTime(Long time, TimeEnum timeEnum) {
        if (time == null) {
            return null;
        }
        switch (timeEnum) {
            case SECOND:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(),
                        new DateTime(time).getDayOfMonth(), new DateTime(time).getHourOfDay(), new DateTime(time).getMinuteOfHour(),
                        new DateTime(time).getSecondOfMinute(), 999).getMillis();
            case MINUTE:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(),
                        new DateTime(time).getDayOfMonth(), new DateTime(time).getHourOfDay(), new DateTime(time).getMinuteOfHour(),
                        59, 999).getMillis();
            case HOUR:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(),
                        new DateTime(time).getDayOfMonth(), new DateTime(time).getHourOfDay(), 59, 59, 999).getMillis();
            case DAY:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(),
                        new DateTime(time).getDayOfMonth(), 23, 59, 59, 999).getMillis();
            case WEEK:
                return getStartTime(new DateTime(time).plusWeeks(1).plusDays(-new DateTime(time).getDayOfWeek() + 1).getMillis(), TimeEnum.DAY) - 1;
            case MONTH:
                return getStartTime(new DateTime(time).plusMonths(1).getMillis(), timeEnum) - 1;
            case YEAR:
                return new DateTime(new DateTime(time).getYear(), 12, 31, 23, 59, 59, 999).getMillis();
            default:
                return null;
        }
    }

    public static Long getStartTime(Long time, TimeEnum timeEnum, Integer step) {
        if (time == null) {
            return null;
        }
        switch (timeEnum) {
            case SECOND:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(),
                        new DateTime(time).getDayOfMonth(), new DateTime(time).getHourOfDay(), new DateTime(time).getMinuteOfHour(),
                        new DateTime(time).getSecondOfMinute(), 0).plusSeconds(step).getMillis();
            case MINUTE:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(),
                        new DateTime(time).getDayOfMonth(), new DateTime(time).getHourOfDay(), new DateTime(time).getMinuteOfHour(),
                        0, 0).plusMinutes(step).getMillis();
            case HOUR:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(),
                        new DateTime(time).getDayOfMonth(), new DateTime(time).getHourOfDay(), 0, 0, 0).plusHours(step).getMillis();
            case DAY:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(),
                        new DateTime(time).getDayOfMonth(), 0, 0, 0, 0).plusDays(step).getMillis();
            case WEEK:
                return getStartTime(new DateTime(time).plusDays(-new DateTime(time).getDayOfWeek() + 1).plusWeeks(step).getMillis(), TimeEnum.DAY);
            case MONTH:
                return new DateTime(new DateTime(time).getYear(), new DateTime(time).getMonthOfYear(), 1, 0, 0, 0, 0).plusMonths(step).getMillis();
            case YEAR:
                return new DateTime(new DateTime(time).getYear(), 1, 1, 0, 0, 0, 0).plusYears(step).getMillis();
            default:
                return null;
        }
    }

    public static Boolean isSameDay(Long originalTime) {
        return isSameDay(originalTime, DateTime.now().getMillis());
    }

    public static Boolean isSameDay(Long originalTime, Long newTime) {
        if (originalTime == null || newTime == null) {
            return false;
        }
        return isSameYear(originalTime, newTime) && isSameMonth(originalTime, newTime)
                && new DateTime(originalTime).getDayOfMonth() == new DateTime(newTime).getDayOfMonth();

    }

    private static Boolean isSameWeek(Long originalTime) {
        return isSameMonth(originalTime, DateTime.now().getMillis());
    }

    private static Boolean isSameWeek(Long originalTime, Long newTime) {
        if (originalTime == null || newTime == null) {
            return false;
        }
        return isSameYear(originalTime, newTime)
                && new DateTime(originalTime).getWeekOfWeekyear() == new DateTime(newTime).getWeekOfWeekyear();
    }


    private static Boolean isSameMonth(Long originalTime) {
        return isSameMonth(originalTime, DateTime.now().getMillis());
    }

    public static Boolean isSameMonth(Long originalTime, Long newTime) {
        if (originalTime == null || newTime == null) {
            return false;
        }
        return isSameYear(originalTime, newTime) && new DateTime(originalTime).getMonthOfYear() == new DateTime(newTime).getMonthOfYear();
    }

    public static Boolean isSameYear(Long originalTime) {
        return isSameYear(originalTime, DateTime.now().getMillis());
    }

    private static Boolean isSameYear(Long originalTime, Long newTime) {
        if (originalTime == null || newTime == null) {
            return false;
        }
        return new DateTime(originalTime).getYear() == new DateTime(newTime).getYear();
    }


    public static List<Long> getTimes(Long startTime, Long endTime, TimeEnum timeEnum) {
        return getTimes(startTime, endTime, timeEnum, 1);
    }

    public static List<Long> getTimes(Long startTime, Long endTime, TimeEnum timeEnum, Integer step) {
        startTime = getStartTime(startTime, timeEnum);
        List<Long> times = new ArrayList<>();
        for (; startTime < endTime; ) {
            times.add(startTime);
            startTime = getStartTime(startTime, timeEnum, step);
        }
        return times;
    }

    public enum TimeEnum {
        /**
         *
         */
        SECOND(0),
        MINUTE(1),
        HOUR(2),
        DAY(3),
        WEEK(4),
        MONTH(5),
        YEAR(6);
        private int code;

        TimeEnum(int code) {
            this.code = code;
        }

        public static TimeEnum valueOfCode(int code) {
            for (TimeEnum timeEnum : TimeEnum.values()) {
                if (timeEnum.getCode() == code) {
                    return timeEnum;
                }
            }
            return null;
        }

        public int getCode() {
            return code;
        }
    }


    /**
     * LocalDateTime 转long
     *
     * @param localDateTime
     * @return
     */
    public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    /**
     * long LocalDateTime
     *
     * @return
     */
    public static LocalDateTime getTimestampOfDateTime(Long timeStamp) {
        Instant instant = Instant.ofEpochMilli(timeStamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值