yyyyMMdd 转换为 yyyy-MM-dd java语言
时间: 2023-12-12 21:04:53 浏览: 39
可以使用Java的SimpleDateFormat类来实现日期格式化。
代码示例如下:
```java
String dateStr = "20220518";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf1.parse(dateStr);
String result = sdf2.format(date);
System.out.println(result);
} catch (ParseException e) {
e.printStackTrace();
}
```
输出结果为:2022-05-18。
相关问题
yyyymmdd转换为 yyyy-mm-dd
要将yyyyMMdd转换为yyyy-MM-dd,可以使用Java的SimpleDateFormat类来实现。可以使用以下代码:
```java
String inputDate = "20211231";
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
String outputDate = outputFormat.format(inputFormat.parse(inputDate));
```
在这个代码中,我们首先定义了输入日期的格式为"yyyyMMdd",然后定义了输出日期的格式为"yyyy-MM-dd"。接下来,我们使用SimpleDateFormat的parse()方法将输入日期字符串解析为Date对象,并使用format()方法将解析后的Date对象格式化为输出日期字符串。最后,我们将输出日期字符串存储在outputDate变量中。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Java一行代码将yyyyMMdd转换为yyyy-MM-dd,yyyy-MM-dd转换为yyyyMMdd](https://blog.csdn.net/qq_38112624/article/details/128283702)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [【HQL实用】时间格式YYYYMMDD转换成YYYY-MM-DD](https://blog.csdn.net/u013631103/article/details/126983750)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
java yyyymmdd转化为yyyy-mm-dd date
你可以使用Java中的SimpleDateFormat类来实现日期格式的转换。以下是将"yyyymmdd"格式转换为"yyyy-mm-dd"格式的示例代码:
```java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatConverter {
public static void main(String[] args) {
String inputDate = "20211231";
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = inputFormat.parse(inputDate);
String outputDate = outputFormat.format(date);
System.out.println(outputDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
```
在这个示例中,我们首先定义了输入日期的格式为"yyyyMMdd",输出日期的格式为"yyyy-MM-dd"。然后,使用`parse()`方法将输入日期字符串解析为Date对象。最后,使用`format()`方法将Date对象格式化为输出日期字符串"yyyy-MM-dd"。运行上述代码,将输出转换后的日期"2021-12-31"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)