Java 2023-05转202305
时间: 2023-12-02 16:02:11 浏览: 20
这是一个纯粹的日期格式转换问题,可以使用Java的SimpleDateFormat类将2023-05转换为202305,具体代码如下:
```
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMM");
String inputDateString = "2023-05";
String outputDateString = "";
try {
Date inputDate = inputFormat.parse(inputDateString);
outputDateString = outputFormat.format(inputDate);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(outputDateString);
```
输出结果为:202305
相关问题
Java程序 2023-05转202305
可以使用Java中的字符串操作函数来实现将2023-05转换成202305的功能,代码如下:
```java
String dateStr = "2023-05";
String newDateStr = dateStr.replace("-", "");
System.out.println(newDateStr);
```
运行结果为:
```
202305
```
这里使用了String类的replace()函数,将原字符串中的"-"替换为空字符串,得到新的字符串newDateStr。
java 20230512转成2023-05-12
可以使用Java中的SimpleDateFormat类来实现日期格式的转换。具体实现代码如下:
```java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws ParseException {
String dateString = "20230512";
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = inputFormat.parse(dateString);
String outputString = outputFormat.format(date);
System.out.println(outputString);
}
}
```
运行结果为:
```
2023-05-12
```
其中,SimpleDateFormat中的"yyyyMMdd"表示输入格式为年月日,"yyyy-MM-dd"表示输出格式为年-月-日。通过调用parse方法将输入字符串转换为Date类型,再通过format方法将Date类型转换为输出字符串。
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.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)