java 取utc标准时间
时间: 2023-09-07 22:02:03 浏览: 110
在Java中取得UTC标准时间可以使用Java的日期时间库。以下是可以使用的方法:
1. 使用Instant类:
```
Instant instant = Instant.now();
System.out.println(instant.toString());
```
这将输出当前的UTC时间戳。
2. 使用ZonedDateTime类:
```
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneOffset.UTC);
System.out.println(zonedDateTime.toString());
```
这将输出当前的UTC时间和时区信息。
3. 使用SimpleDateFormat类:
```
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String utcTime = sdf.format(new Date());
System.out.println(utcTime);
```
这将输出当前的UTC时间字符串。
无论使用哪种方法,都可以获取到当前的UTC标准时间。请注意,在使用日期时间库之前,需要确保Java版本兼容,并导入相关的类和包。
相关问题
Java里utc时间转时间戳
以下是Java中将UTC时间转换为时间戳的方法:
```java
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String date = "2019-09-12T02:43:08.091Z";
long timestamp = simpleDateFormat.parse(date).getTime();
System.out.println(timestamp); // 输出:1568256188091
```
以上代码中,我们首先定义了一个UTC时间的格式化字符串,然后创建了一个SimpleDateFormat对象,并将其时区设置为UTC。接着,我们将UTC时间字符串解析为Date对象,并调用getTime()方法获取时间戳。
java获取UTC当前时间
您好!感谢您的提问。关于您的问题,您可以使用以下代码获取当前UTC时间:
```
import java.time.LocalDateTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
LocalDateTime currentTime = LocalDateTime.now(ZoneOffset.UTC);
System.out.println(currentTime);
}
}
```
这样就能够获取当前的UTC时间了。希望能够帮到您!
相关推荐
![](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)