码迷,mamicode.com
首页 > 其他好文 > 详细

日期工具类 DateTools

时间:2018-05-21 14:48:51      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:start   efault   pre   mes   put   时间段   tar   time   public   

为了跟其他日期工具类进行区分起名字DateTools

public class DateTools {

    /** The DAT e_ forma t1. */
    public static String DATE_FORMAT1 = "yyyy-MM-dd HH:mm:ss";

/**
     * 获取两个时间段的交集
     * @param s1
     * @param e1
     * @param s2
     * @param e2
     * @return
     */
    public static Map<String, LocalDateTime> getOverlapDuration(LocalDateTime s1,LocalDateTime e1,LocalDateTime s2,LocalDateTime e2){
        Map<String, LocalDateTime> localDateTimeMap = new HashMap<>();
        //是否有交集
        long s1InMillis = Timestamp.valueOf(s1).getTime();
        long e1InMillis = Timestamp.valueOf(e1).getTime();
        long s2InMillis = Timestamp.valueOf(s2).getTime();
        long e2InMillis = Timestamp.valueOf(e2).getTime();

        long overlapStartTime = 0;
        long overlapEndTime = 0;
        if((s1InMillis>=s2InMillis&&s1InMillis<=e2InMillis)
                ||(e1InMillis>=s2InMillis&&e1InMillis<=e2InMillis)){
            overlapStartTime = s1InMillis>=s2InMillis?s1InMillis:s2InMillis;
            overlapEndTime = e1InMillis<=e2InMillis?e1InMillis:e2InMillis;
        }
        LocalDateTime start = LocalDateTime.ofInstant(Instant.ofEpochMilli(overlapStartTime), ZoneId.systemDefault());
        LocalDateTime end = LocalDateTime.ofInstant(Instant.ofEpochMilli(overlapEndTime), ZoneId.systemDefault());
        localDateTimeMap.put("start", start);
        localDateTimeMap.put("end" , end);
        return localDateTimeMap;
    }

}

 

日期工具类 DateTools

标签:start   efault   pre   mes   put   时间段   tar   time   public   

原文地址:https://www.cnblogs.com/mengjianzhou/p/9066397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!