Java Date、LocalDate、LocalDateTime互相转换,比较大小

一、相互转化

1、Date转LocalDate

      Date date = new Date();
      LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();


2、LocalDate转Date

        LocalDate localDate = LocalDate.now();       
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));


3、Date转LocalDateTime

      
       Date date = new Date();
       LocalDateTime now = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

4、LocalDateTime转Date

      LocalDateTime localDateTime = LocalDateTime.now();
      Date startDate = Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));

5、LocalDate 转 LocalDateTime

      LocalDate localDate = LocalDate.now();
      LocalDateTime localDateTime1 = localDate.atStartOfDay();
      LocalDateTime localDateTime2 = localDate.atTime(LocalTime.now());


6、LocalDateTime 转 LocalDate

      LocalDateTime localDateTime = LocalDateTime.now();
      LocalDate localDate = localDateTime.toLocalDate();

二、比较大小

  1、Date比较大小

        String beginTime = new String("2014-08-15 10:22:22");
        String endTime = new String("2014-09-02 11:22:22");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date bt = sdf.parse(beginTime);
        Date et = sdf.parse(endTime);

        if (bt.before(et)){
               //表示bt小于et
           } else {
              --反之
           }

      if (bt.after(et)){
               //表示bt大于et
           } else {
              --反之
           }

    2、LocalDate 比较大小

             LocalDate localDate1 = LocalDate.now();
             LocalDate localDate2 = LocalDate.now();
             localDate1.isBefore(localDate2);
             localDate1.isAfter(localDate2);

   
    3、LocalDateTime 比较大小

         LocalDateTime localDateTime1 = LocalDateTime.now();
          LocalDateTime localDateTime2 = LocalDateTime.now();                 
          localDateTime1.isBefore(localDateTime2);
          localDateTime1.isAfter(localDateTime2);
  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要进行datelocaldatelocaldatetime和string之间的相互转换,可以使用Java 8中的DateTimeFormatter和相关类来实现。 1. 将String转换LocalDateTime: 使用DateTimeFormatter的ofPattern方法指定日期时间的格式,然后使用LocalDateTime的parse方法将String转换LocalDateTime对象。例如: ```java String dateStr = "2020-07-20 15:54:41"; String pattern = "yyyy-MM-dd HH:mm:ss"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dateTimeFormatter); ``` 2. 将LocalDateTime转换为String: 使用DateTimeFormatter的ofPattern方法指定日期时间的格式,然后使用LocalDateTime的format方法将LocalDateTime对象转换为String。例如: ```java LocalDateTime localDateTime = LocalDateTime.now(); String pattern = "yyyy-MM-dd HH:mm:ss"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); String dateStr = localDateTime.format(dateTimeFormatter); ``` 3. 将String转换LocalDate: 使用DateTimeFormatter的ofPattern方法指定日期的格式,然后使用LocalDate的parse方法将String转换LocalDate对象。例如: ```java String dateStr = "2021-06-15"; String pattern = "yyyy-MM-dd"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); LocalDate localDate = LocalDate.parse(dateStr, dateTimeFormatter); ``` 4. 将LocalDate转换为String: 使用DateTimeFormatter的ofPattern方法指定日期的格式,然后使用LocalDate的format方法将LocalDate对象转换为String。例如: ```java LocalDate localDate = LocalDate.now(); String pattern = "yyyy-MM-dd"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); String dateStr = localDate.format(dateTimeFormatter); ``` 请注意,以上代码示例仅供参考,具体的实现方式可以根据实际需求进行调整。 #### 引用[.reference_title] - *1* [LocalDateLocalDateTime的各种转换形式](https://blog.csdn.net/zzti_erlie/article/details/100657394)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [LocalDate & LocalDateTime互相转换](https://blog.csdn.net/weixin_45103793/article/details/117929986)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值