gmt java date_关于日期:以Java获取GMT时间

在Java中,我想获取格林尼治标准时间的当前时间。

我尝试了各种选择,例如:

Date date = new Date();

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

date1 = calendar.getTime();

但是日期总是在我的本地时区解释。

我在做什么错,如何将Java Date转换为GMT?

System.currentTimeMillis(),或仅是新的Date()。 它的格林尼治标准时间已经。

仅供参考,现在麻烦的旧日期时间类(例如java.util.Date,java.util.Calendar和java.text.SimpleDateFormat)已成为旧版,由Java 8和更高版本中内置的java.time类取代。 请参见Oracle教程。

奇怪的是,您在获取日期方面在后端做了正确的事情,但是没有任何迹象表明您没有花费GMT时间并根据计算机的当前语言环境对其进行格式化。

final Date currentTime = new Date();

final SimpleDateFormat sdf =

new SimpleDateFormat("EEE, MMM d, yyyy hh:mm:ss a z");

// Give it to me in GMT time.

sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

System.out.println("GMT time:" + sdf.format(currentTime));

关键是使用您自己的DateFormat,而不是系统提供的日期格式。这样,您可以将DateFormat的时区设置为所需的时间,而不是将其设置为语言环境的时区。

我希望以毫秒为单位的格林尼治标准时间日期有效吗?

long millis = System.currentTimeMillis();将返回自1 January 1970, 00:00:00.000 UTC起经过的毫秒数。 UTC时间是大多数人说" GMT时间"时真正想要的。 java.util.Date具有getTime()方法,该方法对任意Date执行相同的操作。根据您的需要,您可能需要使用java.util.Calendar来构造Date对象,如果是这样的话,则Calendars getTime()方法将返回Date,然后您可以通过getTime()来获取毫秒偏移量。

我不知道为什么没人这样做:

Calendar time = Calendar.getInstance();

time.add(Calendar.MILLISECOND, -time.getTimeZone().getOffset(time.getTimeInMillis()));

Date date = time.getTime();

更新:

由于Java 8,9,10及更高版本,因此应该有Java支持的更好的替代方案。感谢您的评论@humanity

我尝试了几种方法来实现此目的,但只有您的解决方案能完美地工作

这种方法的一个问题是时间序列化时它不会更改时区。

仅供参考,现在麻烦的旧日期时间类(例如java.util.Date,java.util.Calendar和java.text.SimpleDateFormat)已经成为旧版,被Java 8和更高版本中内置的java.time类取代。请参见Oracle教程。

根据我的经验,Java中捆绑的Calendar和Date类可能产生不必要的效果。

如果您不介意升级到Java 8,请考虑使用ZonedDateTime

像这样:

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

TL;博士

Instant.now()

java.time

Damilola的答案在建议您使用Java 8和更高版本中内置的java.time框架方面是正确的。但是那个Answer使用ZonedDateTime类,如果您只想使用UTC而不是任何特定的时区,那就太过分了。

麻烦的旧日期时间类现在已被遗留,由java.time类取代。

Instant

Instant类表示UTC时间轴上的时刻,其分辨率为纳秒(最多十进制的九(9)位数字)。

简单的代码:

Instant instant = Instant.now() ;

instant.toString(): 2016-11-29T23:18:14.604Z

您可以将Instant视为构建块,可以在其中添加时区(ZoneID)以获得ZonedDateTime。

ZoneId z = ZoneId.of("America/Montreal" );

ZonedDateTime zdt = instant.atZone( z );

关于java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧式旧式日期时间类,例如java.util.Date,Calendar和SimpleDateFormat。

现在处于维护模式的Joda-Time项目建议迁移到java.time。

要了解更多信息,请参见Oracle教程。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310。

在哪里获取java.time类?

Java SE 8和SE 9及更高版本

内置。

标准Java API的一部分,具有捆绑的实现。

Java 9添加了一些次要功能和修复。

Java SE 6和SE 7

java.time的许多功能在ThreeTen-Backport中都被反向移植到Java 6和7。

Android的

ThreeTenABP项目专门针对Android改编了ThreeTen-Backport(如上所述)。

请参阅如何使用…。

ThreeTen-Extra项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。您可能会在这里找到一些有用的类,例如Interval,YearWeek,YearQuarter等。

在尝试了许多方法之后,我发现,要在GMT上以毫秒为单位,您需要创建两个单独的SimpleDateFormat对象,一个用于在GMT中格式化,而另一个用于解析。

这是代码:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

format.setTimeZone(TimeZone.getTimeZone("UTC"));

Date date = new Date();

SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date dateTime= dateParser.parse(format.format(date));

long gmtMilliSeconds = dateTime.getTime();

这很好。 :)

要在格林尼治标准时间获得毫秒的时间,您需要做的是

long millis = System.currentTimeMillis();

你也可以

long millis = new Date().getTime();

long millis =

Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTimeInMillis();

但是这些都是拨打相同电话的低效率方式。

这个答案是不正确的。尝试从长毫秒打印新的Date(millis)= Calendar.getInstance(TimeZone.getTimeZone(" GMT"))。getTimeInMillis();它仍然给出当前时区时间。

@JavaEnthusiast ideone.com/seLqnz给出了格林尼治标准时间,而不管时区如何。

所有这些都给了我当地的时区,而不是格林尼治标准时间。

@ Forseth11这是不正确的。除非您的系统时钟设置不正确,否则它们都是GMT。

@PeterLawrey我的坏。我使用本地时区中的日期格式来显示时间。抱歉。

你不能

首先,您要问不可能的事情。没有老式的Date对象,因为不能具有时区或GMT偏移量。

But the date is always is interpreted in my local time zone.

我想您已经打印了Date或做了其他隐式调用它的方法。我相信这是在您的时区中唯一解释Date的时间。更准确地说,是在JVM的当前默认时区中。另一方面,这是不可避免的。 Date.toString()确实以这种方式运行,它获取了JVM的时区设置,并将其用于呈现要返回的字符串。

你可以用java.time

不过,您不应使用Date。该课程的设计不佳,幸运的是已经过时了。还有java.time,它是代替它的现代Java日期和时间API,它具有一两个用于日期和时间的类,它们与GMT或UTC有偏移。我现在认为GMT和UTC是同义词,严格来说,它们不是。

OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC);

System.out.println("Time now in UTC (GMT) is" + now);

当我刚运行此代码段时,输出为:

Time now in UTC (GMT) is 2019-06-17T11:51:38.246188Z

输出的尾随Z表示UTC。

链接

Oracle教程:Date Time说明如何使用java.time。

在timeanddate.com上,格林尼治标准时间与世界标准时间之间的差异。

在Java 8之后,您可能希望获取如下所示的格式化时间:

DateTimeFormatter.ofPattern("HH:mm:ss.SSS").format(LocalTime.now(ZoneId.of("GMT")));

以下有用的Utils方法可通过DST节省在GMT中管理时间:

public static Date convertToGmt(Date date) {

TimeZone tz = TimeZone.getDefault();

Date ret = new Date(date.getTime() - tz.getRawOffset());

// if we are now in DST, back off by the delta.  Note that we are checking the GMT date, this is the KEY.

if (tz.inDaylightTime(ret)) {

Date dstDate = new Date(ret.getTime() - tz.getDSTSavings());

// check to make sure we have not crossed back into standard time

// this happens when we are on the cusp of DST (7pm the day before the change for PDT)

if (tz.inDaylightTime(dstDate)) {

ret = dstDate;

}

}

return ret;

}

public static Date convertFromGmt(Date date) {

TimeZone tz = TimeZone.getDefault();

Date ret = new Date(date.getTime() + tz.getRawOffset());

// if we are now in DST, back off by the delta.  Note that we are checking the GMT date, this is the KEY.

if (tz.inDaylightTime(ret)) {

Date dstDate = new Date(ret.getTime() + tz.getDSTSavings());

// check to make sure we have not crossed back into standard time

// this happens when we are on the cusp of DST (7pm the day before the change for PDT)

if (tz.inDaylightTime(dstDate)) {

ret = dstDate;

}

}

return ret;

}

java.util.Date已经在UTC中。这段代码对我来说毫无意义。

这非常简单直接。

Date date = new Date();

TimeZone.setDefault(TimeZone.getTimeZone("GMT"));

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

date = cal.getTime();

现在日期将包含当前格林尼治标准时间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值