JDK1.8 LocalDateTime时间操作类使用

使用Period.between()方法来获取,相差天数、相差月数的时候,发现只能计算同月的天数、同年的月数,不能计算隔月的天数以及隔年的月数!!!相当的坑

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startTime=LocalDateTime.now(ZoneOffset.of("+8")); //2019-08-05T15:12:22.733
        String format = df.format(startTime);    //2019-08-05 15:12:22
        LocalDateTime endTime = startTime.plusDays(7);  //加7天 2019-08-12T15:14:05.974
        LocalDateTime time1 = startTime.minusDays(7);   //减7天 2019-07-29T15:15:04.313
        //时间字符串转localDateTime对象2018-03-28T12:12:12
        LocalDateTime dateTest = LocalDateTime.parse("2019-06-14 12:12:12", df);
        //相差多少天 7天  前后相差多少天 前面距离后面多少天 此方法使用前提同年同月之间比较,有巨坑不建议用
        int daysNum = Period.between(dateTest.toLocalDate(), endTime.toLocalDate()).getDays();
        //相差多少月 0月  前后相差多少月 前面距离后面多少月 此方法使用前提同年之间比较,有巨坑不建议用
        int monthNum = Period.between(dateTest.toLocalDate(), endTime.toLocalDate()).getMonths();

        //一周从周日开始
        WeekFields weekFields = WeekFields.of(DayOfWeek.SUNDAY, 7);
        LocalDate today = LocalDate.now();
        // 取本月第1天:
        LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); //2019-08-01
        // 取本月第2天: 传几取本月第几天
        LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); //2019-08-02
        // 取本月最后一天,再也不用计算是28,29,30还是31:
        LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); //2019-08-31
        // 取下一天:
        LocalDate firstDayOf2015 = today.plusDays(1); // 2019-08-06
        // 取2019年8月第一个周三,  2019-08-07
        LocalDate firstMondayOf2019 = LocalDate.parse("2019-08-01")
                            .with(TemporalAdjusters.firstInMonth(DayOfWeek.WEDNESDAY));
        // 取2019年8月第二个周三,  2019-08-14
        LocalDate firstMondayOf2014 = LocalDate.parse("2019-08-01")
                .with(TemporalAdjusters.dayOfWeekInMonth(2,DayOfWeek.WEDNESDAY));
        //获取秒数  1564991556
        Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        //获取毫秒数   1564991556739
        Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        //毫秒转LocalDateTime 方式一
        LocalDateTime dateToLcal1=LocalDateTime.ofEpochSecond(milliSecond /1000,0,ZoneOffset.ofHours(8));
        //毫秒转LocalDateTime  方式二
         Instant instant = Instant.ofEpochMilli(timeStamp);
        LocalDateTime dateToLcal2=LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        //LocaldateTime 转date 类型   ////Mon Aug 05 15:57:13 CST 2019
        Date date = Date.from(startTime.toInstant(ZoneOffset.of("+8")));
        //date转LocaldateTime  类型   //2019-08-05T16:01:32.412
        LocalDateTime dateToLcal = date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        LocalDate localDate=LocalDate.now(ZoneOffset.of("+8"));  //2019-08-05 东8区
        LocalTime now = LocalTime.now(); // 15:12:22.240
        //去掉毫秒值
        LocalTime localTime=(LocalTime.now(ZoneOffset.of("+8")).withNano(0));  //15:12:22
        LocalTime zero = LocalTime.of(18, 59, 1); // 18:59:01
        LocalTime mid = LocalTime.parse("12:00:00"); // 12:00 当秒为00时打印为省略掉

 

你可能感兴趣的:(java)