LocalDate、LocalDateTime和LocalTime的使用合集

LocalDate

		//获取当前日期
        LocalDate now = LocalDate.now();

        //获取指定日期
        LocalDate localDate = LocalDate.of(2024, 1, 1);

        //Date转LocalDate
        Date date = new Date();
        Instant instant = date.toInstant();
        LocalDate dateToLocalDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();

        //LocalDate转Date
        Date localDateToDate = Date.from(now.atStartOfDay(ZoneId.systemDefault()).toInstant());

        //String转LocalDate
        LocalDate stringToLocalDate = LocalDate.parse("2023-05-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        //LocalDate转String
        String localDateToString = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        //获取月份的第一天
        LocalDate monthFirstDay1 = LocalDate.now().withDayOfMonth(1);
        LocalDate monthFirstDay2 = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        //获取月份的最后一天
        LocalDate monthEndDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());

        //获取当前日期的前几天
        //前一天
        LocalDate beforeDate = LocalDate.now().plusDays(-1);
        //前一周
        LocalDate beforeWeekDate = LocalDate.now().plusWeeks(-1);
        //前一个月
        LocalDate beforeMonthDate = LocalDate.now().plusMonths(-1);
        //前一年
        LocalDate beforeYearDate = LocalDate.now().plusYears(-1);

        //获取当前日期的后几天
        //后一天
        LocalDate afterDate = LocalDate.now().plusDays(1);
        //后一周
        LocalDate afterWeekDate = LocalDate.now().plusWeeks(1);
        //后一个月
        LocalDate afterMonthDate = LocalDate.now().plusMonths(1);
        //后一年
        LocalDate afterYearDate = LocalDate.now().plusYears(1);


        //获取日期的年份
        int year = now.getYear();
        //获取日期的月份
        int month = now.getMonthValue();
        //获取日期是当月的第几天 -- 返回1
        int dayOfMonth = LocalDate.of(2023, 2, 1).getDayOfMonth();
        //获取周几 -- 周三 返回3
        int dayOfWeek = LocalDate.of(2023, 2, 1).getDayOfWeek().getValue();
        //获取日期是当年的第几天 -- 返回32
        int dayOfYear = LocalDate.of(2023, 2, 1).getDayOfYear();

        //日期比较
        LocalDate date1 = LocalDate.of(2023, 10, 1);
        LocalDate date2 = LocalDate.of(2023, 10, 2);
        //date1是否在date2日期之前
        boolean isBefore = date1.isBefore(date2);
        //date1是否在date2日期之后
        boolean isAfter = date1.isAfter(date2);
        //date1是否与date2日期相等
        boolean isEqual = date1.isEqual(date2);

        //两个日期相差多长时间
        LocalDate betweenDate1 = LocalDate.of(2023, 8, 1);
        LocalDate betweenDate2 = LocalDate.of(2023, 10, 2);
        Period period = Period.between(betweenDate1, betweenDate2);
        //相差年数
        int btwYears = period.getYears();
        //相差年数
        int btwMonths = period.getMonths();
        //相差年数
        int btwDays = period.getDays();

        //是否是闰年
        boolean isLeapYear = LocalDate.now().isLeapYear();

LocalDateTime

		//获取当前日期时间
        LocalDateTime nowTime = LocalDateTime.now();
        //获取指定日期时间
        LocalDateTime localDateTime = LocalDateTime.of(2024, 1, 1, 10, 8, 10);

        //DateTime转LocalDateTime
        DateTime dateTime = new DateTime();
        Instant instantTime = dateTime.toInstant();
        LocalDateTime dateTimeToLocalDateTime = instantTime.atZone(ZoneId.systemDefault()).toLocalDateTime();

        //LocalDateTime转DateTime
        Date localDateTimeToDate = Date.from(nowTime.atZone(ZoneId.systemDefault()).toInstant());

        //String转LocalDateTime
        LocalDateTime stringToLocalDateTime = LocalDateTime.parse("2023-05-01 10:08:20", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        //LocalDateTime转String
        String localDateTimeToString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

LocalTime

		//获取当前时间
        LocalTime localNowTime = LocalTime.now();
        //获取指定时间
        LocalTime localTime = LocalTime.of(10, 8, 10);

        //String转LocalTime
        LocalTime stringToLocalTime1 = LocalTime.parse("10:08:20");
        LocalTime stringToLocalTime2 = LocalTime.parse("10-08-20", DateTimeFormatter.ofPattern("HH-mm-ss"));

        //LocalTime转String
        String localTimeToString = LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));

对应LocalDateTime,LocalTime与LocalDate的用法一致,就不在列举

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值