java 时间字符串转utc_将Java日期转换为UTC字符串
时间: 2023-08-22 17:10:05 浏览: 70
可以使用Java中的SimpleDateFormat类将日期转换为UTC格式的字符串。以下是一个示例代码:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class DateToUTCString {
public static void main(String[] args) {
// 创建一个日期对象
Date date = new Date();
// 创建一个SimpleDateFormat对象,指定日期格式为UTC
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
// 将日期对象转换为UTC格式的字符串
String utcString = dateFormat.format(date);
System.out.println("Date: " + date);
System.out.println("UTC String: " + utcString);
}
}
```
该程序的输出为:
```
Date: Wed Sep 22 17:08:44 CST 2021
UTC String: 2021-09-22T09:08:44Z
```
注意,在将日期转换为UTC格式的字符串时,需要指定时区为UTC。否则,将会使用本地时区。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)