美文网首页
(转)Java 8 获取某天最大(23:59:59)最小(00:

(转)Java 8 获取某天最大(23:59:59)最小(00:

作者: Luomeng | 来源:发表于2017-12-17 22:59 被阅读0次
    
    import java.text.SimpleDateFormat;
    import java.time.Instant;
    import java.time.LocalDateTime;
    import java.time.LocalTime;
    import java.time.ZoneId;
    import java.util.Date;
    
    public class GetMaxMinTime {
        public static void main(String[] args) {
            Date date = new Date();
            
            System.out.println("今天开始时间:" + getStartOfDay(date));
            System.out.println("今天结束时间:" + getEndOfDay(date));
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            
            System.out.println(sdf.format(getStartOfDay(date)));
            System.out.println(sdf.format(getEndOfDay(date)));
            
        }
        
        // 获得某天最大时间 2017-10-15 23:59:59
        public static Date getEndOfDay(Date date) {
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
            LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
            return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
        }
        
        // 获得某天最小时间 2017-10-15 00:00:00
        public static Date getStartOfDay(Date date) {
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
            LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
            return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        }
    
    }
    

    原文链接:http://blog.csdn.net/gisboygogogo/article/details/78239925

    相关文章

      网友评论

          本文标题:(转)Java 8 获取某天最大(23:59:59)最小(00:

          本文链接:https://www.haomeiwen.com/subject/jpzuwxtx.html