Java中关于Date,LocalDate,LocalDateTime总结

Date,LocalDate,LocalDateTime总结

        // LocalDate 主要关心的是日期 不关注具体的时间
        // LocalTime 只包括时间
        // LocalDateTime 包括日期和时间

        LocalDate today = LocalDate.now();
        // 该日期的年份
        int year = today.getYear();
        // 该日期的月份
        int value = today.getMonthValue();
        // 该日期是当前周的第几天
        int dayOfWeek = today.getDayOfWeek().getValue();
        // 该日期是当前月的第几天
        int dayOfMonth = today.getDayOfMonth();
        // 该日期是当前年的第几天
        int dayOfYear = today.getDayOfYear();


        // 修改该日期的年份
        LocalDate localDate = today.withYear(2020);
        // 修改该日期的月份
        LocalDate localDate2 = today.withMonth(7);
        // 修改该日期的当月天数
        LocalDate localDate1 = today.withDayOfMonth(20);
        // 修改该日期当前年份的天数
        LocalDate localDate3 = today.withDayOfYear(1);


        // 判断是否是闰年
        boolean leapYear = today.isLeapYear();
        // 当前年份天数
        int lengthOfYear = today.lengthOfYear();
        // 当前月份有几天
        int lengthOfMonth = today.lengthOfMonth();

LocalDate 相关方法
LocalDate 相关方法

  • LocalDate 与 String 之间的转换
        // LocalDate 转 String
        LocalDate localDateToString = LocalDate.now();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateStr = localDateToString.format(fmt);

        //String 转 LocalDate
        String str = "2019-03-03";
        //指定转换格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //进行转换
        LocalDate date = LocalDate.parse(str, dateTimeFormatter);

  • Date 转 LocalDate
        // 方式一
        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date1);
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate parse = LocalDate.parse(format, dateTimeFormatter1);

        // 方式二
        Date date11 = new Date();
        Instant instant = date11.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        LocalDate localDate11 = instant.atZone(zoneId).toLocalDate();
  • Instant 转 Date
        // Instant 转 Date
        Instant now = Instant.now();
        Date from = Date.from(now);
        // Date 转 Instant
        Date date2 = new Date();
        Instant instant1 = date2.toInstant();
  • LocalDateTime

        // LocalDateTime
        LocalDateTime time = LocalDateTime.now();
        //字符串表示
        String string = time.toString();
        //获取时间(LocalTime)
        LocalTime localTime = time.toLocalTime();
        //获取日期(LocalDate)
        LocalDate localDate4 = time.toLocalDate();
        //获取当前时间月份的第几天
        int dayOfMonth1 = time.getDayOfMonth();
        //获取当前周的第几天
        DayOfWeek dayOfWeek1 = time.getDayOfWeek();
        //获取当前时间在该年属于第几天
        int dayOfYear1 = time.getDayOfYear();
        int hour = time.getHour();
        int minute = time.getMinute();
        int monthValue = time.getMonthValue();
        Month month = time.getMonth();

        //格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        System.out.println(time.format(formatter));
        //构造时间
        LocalDateTime startTime = LocalDateTime.of(2018, 1, 1, 20, 31, 20);
        LocalDateTime endTime = LocalDateTime.of(2018, 1, 3, 20, 31, 20);
        //比较时间
        boolean after = time.isAfter(startTime);
        boolean before = time.isBefore(endTime);

        //时间运算,相加相减
        //加2年
        System.out.println(time.plusYears(2));
        //加两天
        System.out.println(time.plusDays(2));
        //减两年
        System.out.println(time.minusYears(2));
        //减两天
        System.out.println(time.minusDays(2));

        //获取毫秒数(使用Instant)
        System.out.println(time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
        //获取秒数(使用Instant)
        System.out.println(time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());
  • localDateTime date互相转换
        // localDateTime 转date
        LocalDateTime now1 = LocalDateTime.now();
        ZoneId zone = ZoneId.systemDefault();
        Instant instant2 = now1.atZone(zone).toInstant();
        Date from1 = Date.from(instant2);

        // date 转 localDateTime
        Date datess = new Date();
        Instant instant3 = datess.toInstant();
        ZoneId zoneId1 = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant3, zoneId1);

参考文章:
Java 8 中 Date与LocalDateTime、LocalDate、LocalTime互转
LocalDate使用
LocalDate基本方法
w3cschool 这个网站总结了一些常见问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值