20230616182653怎么用java转成时间
时间: 2023-09-02 11:15:08 浏览: 41
在Java中,你可以使用`SimpleDateFormat`类来将字符串表示的日期转换为`Date`对象,然后再使用`DateFormat`类将其格式化为所需的时间格式。以下是一个示例代码:
```java
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeConversion {
public static void main(String[] args) {
String dateString = "20230616182653";
DateFormat inputFormat = new SimpleDateFormat("yyyyMMddHHmmss");
DateFormat outputFormat = new SimpleDateFormat("HH:mm:ss 'on' MMMM dd, yyyy");
try {
Date date = inputFormat.parse(dateString);
String formattedDate = outputFormat.format(date);
System.out.println(formattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
```
这段代码将会输出转换后的时间格式:`18:26:53 on June 16, 2023`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)