String转data
时间: 2023-08-03 09:40:51 浏览: 127
在Java中,可以使用SimpleDateFormat类将字符串转换为日期类型。以下是将字符串转换为日期类型的示例代码:
```
String strDate = "2022-01-01";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(strDate);
```
在上面的示例中,首先定义了一个格式化日期的SimpleDateFormat对象,并指定了日期的格式为"yyyy-MM-dd"。然后,使用parse()方法将字符串转换为日期类型。
需要注意的是,SimpleDateFormat类在解析日期字符串时,会抛出ParseException异常,需要进行捕获或声明抛出。同时,也要确保字符串的格式与指定的格式化字符串相匹配,否则会解析失败。
阅读全文
相关推荐


















