获取当前时间戳的几种方法,把时间转换成long
long time0 = System.currentTimeMillis();
long time1 = new Date().getTime();
long time2 = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
long time3 = Calendar.getInstance().getTimeInMillis();
使用LocalDateTime计算两个时间的差
https://www.cnblogs.com/maohuidong/p/11956787.html
LocalDateTime now = LocalDateTime.now();
LocalDateTime end = LocalDateTime.now();
Duration duration = Duration.between(now,end);
long days = duration.toDays(); //相差的天数
long hours = duration.toHours();//相差的小时数
long minutes = duration.toMinutes();//相差的分钟数
long millis = duration.toMillis();//相差毫秒数
long nanos = duration.toNanos();//相差的纳秒数
计算LocalDateTime相差天数
https://blog.csdn.net/qq_31263039/article/details/94597415
//只能计算相同月之间相隔的天数
int daysNum = Period.between(O.getStartTime().toLocalDate(), O.getEndTime().toLocalDate()).getDays();
优化后使用toEpochDay()方法为:
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
LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
DateTimeFormatter time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String todayStart=time.format(today_start);
String todayEnd = time.format(today_end);
LocalDateTime 获取本月第一天开始时间00.00 ~本月最后一天结束时间23.59
LocalDateTime now = LocalDateTime.now();
LocalDateTime beginDateTime = now.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
LocalDateTime endDateTime = now.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
DateTimeFormatter time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String startMoth = time.format(beginDateTime);
String endMoth = time.format(endDateTime);
LocalDate的减少指定的天数、年数等
LocalDate localDate = LocalDate.now(); // 2019-01-31
//计算去年
LocalDate previousYear = localDate.minus( 1, ChronoUnit.YEARS ); // 2018-01-31
//减30天
LocalDate previousDay = localDate.minus( 30, ChronoUnit.DAYS ); //2019-01-01
//减少6天
LocalDate startTime = localDate.minusDays( 6 ) ; 2019-01-25
LocalDate计算上周日的日期
LocalDate localDate = LocalDate.now();//当前日期
int week = localDate.getDayOfWeek().getValue(); //计算今天是星期几
LocalDate endTime = localDate.minusDays( week ); //计算上周日的日期