LocalDateTime与String日期互相转换

一、LocalDateTime与String日期互相转换

public class Main {
    public static void main(String[] args) {
        //指定日期格式
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //获取当前日期
        LocalDateTime now = LocalDateTime.now();
        //LocalDateTime转String
        String date = format.format(now);
        //String转LocalDatetime
        LocalDateTime localDate = LocalDateTime.parse(date, format);
        System.out.println("localDate is " + localDate);
    }
}

输出结果

localDate is 2022-10-31T10:28:09

Process finished with exit code 0

二、获取系统当前日期上个月的第一天

public class Main {
    public static void main(String[] args) {
        //创建当前日期
        LocalDateTime localDateTime = LocalDateTime.now();
        //获取当前日期的0时
        LocalDateTime nowDate = LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth(), 00, 00, 00);
        //获取当前日期的第一天
        LocalDateTime firstDayOfMonth = nowDate.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println("nowDate is " + nowDate);
        System.out.println("firstDayOfMonth is " + firstDayOfMonth);
    }
}

输出结果

nowDate is 2022-10-31T00:00
firstDayOfMonth is 2022-10-01T00:00

Process finished with exit code 0

三、获取系统当前日期上个月的最后一天

public class Main {
    public static void main(String[] args) {
        //获取当前日期
        LocalDateTime nowDate = LocalDateTime.now();
        //当前日期的上一个月,月份减一
        LocalDateTime localDateTime = nowDate.minusMonths(1);
        LocalDateTime lastDayOfMonth = localDateTime.with(TemporalAdjusters.lastDayOfMonth());
        LocalDateTime result = LocalDateTime.of(lastDayOfMonth.getYear(), lastDayOfMonth.getMonthValue(), lastDayOfMonth.getDayOfMonth(), 23, 59, 59);
        System.out.println("nowDate is " + nowDate);
        System.out.println("result is " + result);
    }
}

输出结果

nowDate is 2022-10-31T10:45:59.467
result is 2022-09-30T23:59:59

Process finished with exit code 0
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要进行date、localdate、localdatetimestring之间的相互转换,可以使用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* [LocalDate,LocalDateTime的各种转换形式](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、付费专栏及课程。

余额充值