找出发布的UTC时间戳与当前时间之间的差异(以分钟为单位)

时间:2021-09-06 21:31:13

What I'm trying to do: Check for the minutes until an event. (I'm in central time which is UTC -5 hours). The object I get is a JSON Element that looks like this when I take the string:

我正在尝试做的事情:检查事件发生前的分钟数。 (我在*时间,即UTC -5小时)。我得到的对象是一个JSON元素,当我接受字符串时,它看起来像这样:

/Date(1502964420000-0500)/

I should be able to:

我应该能够:

//take the departure time and subtract it from the current time. Divide by 60
    timeStamp = timeStamp.substring(6,16);

This gives me 1502964420 which I can use a time converter to get: Thursday, August 17, 2017 5:07:00 AM

这给了我1502964420,我可以使用时间转换器得到:2017年8月17日星期四上午5:07:00

Problem is.. How do I get the current time in the same format to subtract it? (or if there's a better way to do this I'd gladly take that advice as well).

问题是..如何以相同的格式获取当前时间来减去它? (或者,如果有更好的方法,我也很乐意接受这个建议)。

2 个解决方案

#1


1  

I would recommend looking at the datatype ZonedDateTime.

我建议查看数据类型ZonedDateTime。

With this you can easily perform calculasions and conversions like this:

有了这个,你可以轻松地执行这样的计算和转换:

ZonedDateTime startTime = ZonedDateTime.now();
Instant timestamp = startTime.toInstant(); // You can also convert to timestamp
ZonedDateTime endTime = startTime.plusSeconds(30);

Duration duration = Duration.between(startTime, endTime);

if(duration.isNegative()){
  // The end is before the start
}

long secondsBetween = duration.toMillis(); // duration between to seconds

Since you don't know about ZonedDateTime here is a quick overview how to convert string to ZonedDateTime:

由于您不了解ZonedDateTime,因此这里是一个快速概述如何将字符串转换为ZonedDateTime:

Note: The String has to be in the ISO8601 format!

注意:String必须是ISO8601格式!

String example = "2017-08-17T09:14+02:00";
OffsetDateTime offset = OffsetDateTime.parse(example);
ZonedDateTime result = offset.atZoneSameInstant( ZoneId.systemDefault() );

#2


0  

You can either use Date currentDate = new Date() and then currentDate.getTime() to get the current Unix time in milliseconds or use the Calendar-class: Calendar currentDate = Calendar.getInstance() and currentDate.getTime().getTime() to get the current Unix time in milliseconds.

您可以使用Date currentDate = new Date()然后使用currentDate.getTime()来获取当前Unix时间(以毫秒为单位)或使用Calendar-class:Calendar currentDate = Calendar.getInstance()和currentDate.getTime()。getTime( )以毫秒为单位获取当前的Unix时间。

You can do the same with the date parsed from the json and then calculate the difference between the two values. To get the difference in minutes, just divide it then by (60*1000)

您可以对从json解析的日期执行相同的操作,然后计算两个值之间的差异。要在几分钟内得到差异,只需将其除以(60 * 1000)

#1


1  

I would recommend looking at the datatype ZonedDateTime.

我建议查看数据类型ZonedDateTime。

With this you can easily perform calculasions and conversions like this:

有了这个,你可以轻松地执行这样的计算和转换:

ZonedDateTime startTime = ZonedDateTime.now();
Instant timestamp = startTime.toInstant(); // You can also convert to timestamp
ZonedDateTime endTime = startTime.plusSeconds(30);

Duration duration = Duration.between(startTime, endTime);

if(duration.isNegative()){
  // The end is before the start
}

long secondsBetween = duration.toMillis(); // duration between to seconds

Since you don't know about ZonedDateTime here is a quick overview how to convert string to ZonedDateTime:

由于您不了解ZonedDateTime,因此这里是一个快速概述如何将字符串转换为ZonedDateTime:

Note: The String has to be in the ISO8601 format!

注意:String必须是ISO8601格式!

String example = "2017-08-17T09:14+02:00";
OffsetDateTime offset = OffsetDateTime.parse(example);
ZonedDateTime result = offset.atZoneSameInstant( ZoneId.systemDefault() );

#2


0  

You can either use Date currentDate = new Date() and then currentDate.getTime() to get the current Unix time in milliseconds or use the Calendar-class: Calendar currentDate = Calendar.getInstance() and currentDate.getTime().getTime() to get the current Unix time in milliseconds.

您可以使用Date currentDate = new Date()然后使用currentDate.getTime()来获取当前Unix时间(以毫秒为单位)或使用Calendar-class:Calendar currentDate = Calendar.getInstance()和currentDate.getTime()。getTime( )以毫秒为单位获取当前的Unix时间。

You can do the same with the date parsed from the json and then calculate the difference between the two values. To get the difference in minutes, just divide it then by (60*1000)

您可以对从json解析的日期执行相同的操作,然后计算两个值之间的差异。要在几分钟内得到差异,只需将其除以(60 * 1000)