DateUtil.java 4.28 KB
package com.yoho.rfid.util;

import org.slf4j.helpers.MessageFormatter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Objects;

/**
 * Created by sailing on 2017/10/16.
 */
public class DateUtil {
    // 基础(通用)的FORMAT类型,
    public static final String NORMAL_DAY_FORMAT = "yyyy-MM-dd";

    public static final String DAY_FORMAT = "yyyyMMdd";

    // 完整格式的时间
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * 操作一个时间里的分钟/小时/天数
     * @param date
     * @param operator
     * @param timeLong
     * @param timeUnit
     * @return
     */
    public static Date calDateTime(Date date,Operator operator,long timeLong, TimeUnit timeUnit){
        if (Objects.isNull(date)){
            return date;
        }
        if(Objects.isNull(operator)){
            return date;
        }
        if (timeLong == 0L){//as no any operator
            return date;
        }

        if(Objects.isNull(timeUnit)){
            return date;
        }
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant,zoneId);
        switch (operator){
            case plus:
                switch(timeUnit){
                    case minute:
                        localDateTime = localDateTime.plusMinutes(timeLong);
                        break;
                    case hour:
                        localDateTime = localDateTime.plusHours(timeLong);
                        break;
                    case day:
                        localDateTime = localDateTime.plusDays(timeLong);
                        break;
                }
                break;
            case minus:
                switch(timeUnit){
                    case minute:
                        localDateTime = localDateTime.minusMinutes(timeLong);
                        break;
                    case hour:
                        localDateTime = localDateTime.minusHours(timeLong);
                        break;
                    case day:
                        localDateTime = localDateTime.minusDays(timeLong);
                        break;
                }
                break;
        }
        return Date.from(localDateTime.atZone(zoneId).toInstant());
    }

    /**
     * 根据时间获取到秒值
     * @param date
     * @return
     */
    public static int getDateTimeSecond(Date date){
        long longTime = date.getTime();
        return (int) (longTime / 1000);
    }

    public static int getPart(Date date, TimeUnit timeUnit){
        int result = 0;
        if (Objects.isNull(date)){
            return result;
        }

        if(Objects.isNull(timeUnit)){
            return result;
        }
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant,zoneId);
        switch(timeUnit){
            case minute:
                result = localDateTime.getMinute();
                break;
            case hour:
                result = localDateTime.getHour();
                break;
            case day:
                result = localDateTime.getDayOfMonth();
                break;
        }
        return result;
    }


    public enum Operator{
        plus,minus;
    }

    public enum TimeUnit{
        minute,hour,day
    }


    public static void main(String[] args) {
        try{
        int currentHour = DateUtil.getPart(new Date(), TimeUnit.hour);
        String specailDatetime = "2018-10-10 00:00:00";
        DateFormat format = new SimpleDateFormat(DateUtil.DATE_TIME_FORMAT);
        Date parseDate = format.parse(specailDatetime);
            int parseDateHour = DateUtil.getPart(parseDate, TimeUnit.hour);
        System.out.println(MessageFormatter
                .arrayFormat("currentHour {}, parseDateHour {}", new Object[]{currentHour, parseDateHour})
                .getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}