LocalDateTime与String日期互相转换


DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time = LocalDateTime.now();
String localTime = df.format(time);
LocalDateTime ldt = LocalDateTime.parse("2017-09-28 17:07:05",df);
System.out.println("LocalDateTime转成String类型的时间:"+localTime);
System.out.println("String类型的时间转成LocalDateTime:"+ldt);

 

控制台结果:

 LocalDateTime转成String类型的时间:2017-09-30 10:40:06
 String类型的时间转成LocalDateTime:2017-09-28T17:07:05

               

  • 13
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要进行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 ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值