获取当前时间戳的几种方法,把时间转换成long

    1. long time0 = System.currentTimeMillis();
    2. long time1 = new Date().getTime();
    3. long time2 = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
    4. long time3 = Calendar.getInstance().getTimeInMillis();

    使用LocalDateTime计算两个时间的差
    https://www.cnblogs.com/maohuidong/p/11956787.html

    1. LocalDateTime now = LocalDateTime.now();
    2. LocalDateTime end = LocalDateTime.now();
    3. Duration duration = Duration.between(now,end);
    4. long days = duration.toDays(); //相差的天数
    5. long hours = duration.toHours();//相差的小时数
    6. long minutes = duration.toMinutes();//相差的分钟数
    7. long millis = duration.toMillis();//相差毫秒数
    8. long nanos = duration.toNanos();//相差的纳秒数

    计算LocalDateTime相差天数
    https://blog.csdn.net/qq_31263039/article/details/94597415

    1. //只能计算相同月之间相隔的天数
    2. int daysNum = Period.between(O.getStartTime().toLocalDate(), O.getEndTime().toLocalDate()).getDays();

    优化后使用toEpochDay()方法为:

    1. int daysNum=(int)(o.getEndTime().toLocalDate().toEpochDay() - o.getStartTime().toLocalDate().toEpochDay());

    LocalDateTime获取当日00:00、结束时间23.59与当月第一天00.00,月末最后一天23.59
    https://blog.csdn.net/weixin_48174603/article/details/108781167

    LocalDateTime 获取当天开始时间00.00 ~当天结束时间23.59

    1. LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
    2. LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
    3. DateTimeFormatter time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    4. String todayStart=time.format(today_start);
    5. String todayEnd = time.format(today_end);

    LocalDateTime 获取本月第一天开始时间00.00 ~本月最后一天结束时间23.59

    1. LocalDateTime now = LocalDateTime.now();
    2. LocalDateTime beginDateTime = now.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
    3. LocalDateTime endDateTime = now.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
    4. DateTimeFormatter time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    5. String startMoth = time.format(beginDateTime);
    6. String endMoth = time.format(endDateTime);

    LocalDate的减少指定的天数、年数等

    1. LocalDate localDate = LocalDate.now(); // 2019-01-31
    2. //计算去年
    3. LocalDate previousYear = localDate.minus( 1, ChronoUnit.YEARS ); // 2018-01-31
    4. //减30天
    5. LocalDate previousDay = localDate.minus( 30, ChronoUnit.DAYS ); //2019-01-01
    6. //减少6天
    7. LocalDate startTime = localDate.minusDays( 6 ) ; 2019-01-25

    LocalDate计算上周日的日期

    1. LocalDate localDate = LocalDate.now();//当前日期
    2. int week = localDate.getDayOfWeek().getValue(); //计算今天是星期几
    3. LocalDate endTime = localDate.minusDays( week ); //计算上周日的日期