Java得到UTC时间 [英] Java get UTC time

查看:188
本文介绍了Java得到UTC时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在UTC时区获得时间。所以我写了代码:

  import java.time.ZoneOffset; 
import java.time.ZonedDateTime;
import java.util.Date;

public class RegularSandbox {

public static void main(String [] args){

ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC) ;

System.out.println(DATETIME =+ Date.from(utc.toInstant()));

}
}

问题是输出显示我在PST(我的当地时区)的时间。我需要它输出UTC的时间,所以我可以将它存储在我的数据库中。

解决方案

  System.out.println(DATETIME =+ utc.format(DateTimeFormatter.ofPattern(yyyy-MM-dd hh:mm:ss))); 


I want to get the time in UTC time zone. So I wrote the code:

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Date;

public class RegularSandbox {

    public static void main(String[] args) {

        ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);

        System.out.println("DATETIME = " + Date.from(utc.toInstant()));

    }
}

The problem is the output shows me the time in PST (my local timezone). I need it to output the time in UTC so I can store it inside of my databases.

解决方案

System.out.println("DATETIME = " + utc.format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")));

这篇关于Java得到UTC时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆