Date 和 LocalDate

1, Date转LocalDate:new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()

 

2、計算兩個時間間隔的天數:

        String time1 = "2023 01 12 00:05:00";
        String time2 = "2022 12 21 15:00:05";
        DateFormat dateFormat = new SimpleDateFormat("yyyy MM dd hh:mm:ss");
        Date beString = dateFormat.parse(time1);
        Date enString = dateFormat.parse(time2);
        long sub = Math.abs(beString.getTime() - enString.getTime());
        long longDay = TimeUnit.DAYS.convert(sub, TimeUnit.MILLISECONDS);

 3、Jackson 实现 LocalDate 转 String:

        ObjectMapper objectMapper = new ObjectMapper();

        objectMapper.registerModule(new JavaTimeModule());

        CustomerStatementDTO value = new CustomerStatementDTO();
        value.setPeriodEndDate(LocalDate.now());
        String str = objectMapper.writeValueAsString(value);

        CustomerStatementDTO a = objectMapper.readValue(str, CustomerStatementDTO.class);

 

4、 LocalDate 昨天 转成 String:

 String yesterday = LocalDate.now().plusDays(-1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))

 

 String 转  LocalDate :

   String yes = "2023-02-17";

   LocalDate localDate = LocalDate.parse(yes , DateTimeFormatter.ofPattern("yyyy-MM-dd"));

  

 

posted @ 2022-09-20 09:09  zhoudingzhao  阅读(42)  评论(0编辑  收藏  举报