yyyyMMddHHmmss转yyyy-MM-dd HH:MM:SS java
时间: 2024-04-24 20:25:33 浏览: 25
你可以使用Java中的SimpleDateFormat类来格式化日期和时间。以下是将yyyyMMddHHmmss格式转换为yyyy-MM-dd HH:mm:ss格式的示例代码:
```java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
String inputDate = "20220101120000";
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = inputFormat.parse(inputDate);
String outputDate = outputFormat.format(date);
System.out.println(outputDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
```
在上面的示例中,我们首先定义输入日期的格式为"yyyyMMddHHmmss",输出日期的格式为"yyyy-MM-dd HH:mm:ss"。然后,我们使用`parse()`方法将输入日期字符串解析为Date对象,并使用`format()`方法将Date对象格式化为输出日期字符串。最后,我们打印输出日期字符串"2022-01-01 12:00:00"。