LocalDate 、LocalTime、LocalDateTime基础用法

       LocalDate localDate = LocalDate.now();
        System.out.println("获取当前日期: "+localDate);
        LocalTime  localTime  = LocalTime.now();
        System.out.println("获取当前时分秒:"+localTime);
        LocalDateTime  localDateTime=LocalDateTime.now();

        System.out.println("获取当前年月日时分秒:"+localDateTime);

        System.out.println("获取当前年份:"+localDate.getYear());

        System.out.println("获取当前月份:"+localDate.getMonth() +"  "+ localDate.getMonth().getValue());

        System.out.println("获取指定日期所在月份的第八天:"+localDate.withDayOfMonth(8));

        System.out.println("格式化当前年月日:"+localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-hh")));

        System.out.println("格式化当前年月日时分秒:"+localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        System.out.println("字符串转年月日:"+LocalDate.parse("2023-06-08",DateTimeFormatter.ofPattern("yyyy-MM-dd")));

        System.out.println("字符串转年月日时分秒:"+LocalDateTime.parse("2023-06-08 10:23:45",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        System.out.println("组合时间:"+LocalDateTime.of(LocalDate.of(2023,3,9),LocalTime.of(12,30,35)));

        System.out.println("获取当前月份的第2个周日的日期:"+localDate.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.SUNDAY)));

        System.out.println("指定日期所在月份的第一天: "+ localDate.with(TemporalAdjusters.firstDayOfMonth()));

        System.out.println("指定日期所在月份的最后一天: "+ localDate.with(TemporalAdjusters.lastDayOfMonth()));

        System.out.println("指定日期所在月份的下一个月第一天: "+ localDate.with(TemporalAdjusters.firstDayOfNextMonth()));

        System.out.println("指定日期所在年份的第一天: "+localDate.with(TemporalAdjusters.firstDayOfYear()));

        System.out.println("指定日期所在年份的最后一天: "+localDate.with(TemporalAdjusters.lastDayOfYear()));

        System.out.println("当前时间加2个小时"+localTime.plus(2, ChronoUnit.HOURS));

        System.out.println("当前时间加3天"+localDateTime.plus(3, ChronoUnit.DAYS));

        System.out.println("比较两个日期大小:"+LocalDate.of(2023,6,10).isAfter(LocalDate.of(2023,5,10)));

        System.out.println("比较两个日期大小:"+LocalTime.of(9,12,30).isBefore(LocalTime.of(10,5,10)));

你可能感兴趣的:(java,servlet,服务器)