string类型怎么转成date类型
时间: 2023-10-17 17:35:28 浏览: 114
String与date的转换
要将String类型转换为Date类型,可以使用SimpleDateFormat类来实现。首先,需要定义一个SimpleDateFormat对象,并指定日期的格式。比如,如果String类型是"yyyy-MM-dd"格式的日期,可以使用以下代码进行转换:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
然后,使用SimpleDateFormat的parse()方法将String类型的日期转换为Date类型的日期。例如:
String dateString = "2023-03-17";
Date date = sdf.parse(dateString);
这样,就将String类型的日期转换为了Date类型的日期。请注意,parse()方法可能会抛出ParseException异常,因此需要进行异常处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [String类型和Date类型相互转换(java)](https://blog.csdn.net/weixin_43878639/article/details/129614055)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [如何将String转Date类型](https://blog.csdn.net/HelloWorl___d/article/details/107973692)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [String类型转localDate,date转localDate的实现代码](https://download.csdn.net/download/weixin_38742409/12721056)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文