[英]Independent time in java application
I would like to set arbitrary time in application. 我想在应用程序中设置任意时间。 Time is downloaded from server in milliseconds format- it should be independent from locale
and other system preferences. 时间是从服务器以毫秒格式下载的,该时间应与locale
和其他系统偏好locale
无关。 But application reacquire thread safety solution, and object like standard not thread safety Calendar
object. 但是应用程序需要获取线程安全解决方案,而标准对象不是线程安全Calendar
对象。
Whats is the best way? 最好的方法是什么?
Today I use: 今天我用:
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.setTimeInMillis(serverTime);
But is not good way for me beacuse, is't thread safe. 但是因为我不是一个好方法,所以不是线程安全的。
tl;dr Program have to contain own internal clock fetching time from external server. tl; dr程序必须包含自己的从外部服务器获取内部时钟的时间。 Clock must be thread-safe. 时钟必须是线程安全的。
The time in milliseconds that a Java application uses is Java应用程序使用的时间(以毫秒为单位)为
the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT. 自标准基准时间(即“纪元”)以来的指定毫秒数,即格林尼治标准时间1970年1月1日。
This number is based on the GMT time zone. 此数字基于GMT时区。 If you need to print it in another time zone you can use any formatting class you want, say SimpleDateFormat
. 如果您需要在其他时区进行打印,则可以使用所需的任何格式设置类,例如SimpleDateFormat
。 If you need to make the variable that holds it thread safe, just synchronize on it, possibly by wrapping it in a class. 如果需要使保存它的变量成为线程安全的,则只需对其进行同步(可能将其包装在类中)即可。
public class TimeInMillis {
private volatile long time;
public void setTime(long time) {this.time = time;}
public time getTime() {return time;}
}
Whenever you need to display it, just get the TimeInMillis
object, get the time
and create a Calendar
object with it. 每当您需要显示它时,只需获取TimeInMillis
对象,获取time
并使用它创建一个Calendar
对象。 Then use a formatting class to print the time in the format, locale, timezone, you require. 然后使用格式化类以所需的格式,区域设置,时区打印时间。
Time is downloaded from server in milliseconds format- it should be independent from locale and other system preferences 从服务器以毫秒格式下载时间-时间应与语言环境和其他系统偏好设置无关
That isn't "time". 那不是“时间”。 That is a timestamp, meaning a particular time value reported by a particular piece of software at a particular point in time. 这是一个时间戳,表示特定软件在特定时间点报告的特定时间值。
Now, if you are trying to say that, in future communications with this server, you want to translate time as reported by the device into the timebase as known by the server, that makes at least a bit of sense. 现在,如果您要说的是,在将来与该服务器通信时,您希望将设备报告的时间转换为服务器已知的时基, 这至少有一点道理。 In that case, you compute the delta between the device time when you receive the timestamp and the time value in the timestamp itself. 在这种情况下,您将计算收到时间戳的设备时间与时间戳本身中的时间值之间的差异。 Then, you apply that delta to future times you report back to the server. 然后,您将该增量应用于将来的时间,然后再向服务器报告。
Program have to contain own internal clock fetching time from external server 程序必须包含自己的内部时钟从外部服务器获取时间
That makes no sense whatsoever. 那毫无意义。
In this universe, based on our current knowledge of physics, time is continuous and linear. 在这个宇宙中,根据我们目前的物理学知识,时间是连续且线性的。 Time does not change only when you are "fetching time from an external server". 仅在“从外部服务器获取时间”时,时间不会改变。 Again, what you are "fetching... from an external server" is a timestamp, a statement of what the clock on the server thought the time was at the time you made the request. 同样,“从外部服务器获取...”是一个时间戳,表示服务器上的时钟认为该时间是发出请求时的时间。 You can use that timestamp for comparison purposes with other timestamps from that server, and you can use that timestamp to compare to the device's current time to determine the difference. 您可以将该时间戳记与该服务器上的其他时间戳记进行比较,也可以使用该时间戳记与设备的当前时间进行比较以确定差异。
However, you cannot create hardware in Java code, and so you cannot create "internal clock" in Java code. 但是,您无法使用Java代码创建硬件,因此无法使用Java代码创建“内部时钟”。 The only clock is the device's clock. 唯一的时钟是设备的时钟。 As noted earlier, you can translate the device's clock's time to the timebase of the server by applying the aforementioned difference, but the passage of time is still being marked by the device's own clock. 正如前面提到的,你可以通过应用上述差异设备的时钟的时间转换为服务器的时基,但时间的流逝仍受到设备自身的时钟标记。
Since the difference is going to be an int
or long
, you can use AtomicInteger
or AtomicLong
if you are concerned about multiple threads working with the value at once. 由于差异将是int
或long
,因此如果您担心多个线程同时使用该值,则可以使用AtomicInteger
或AtomicLong
。
I would just the the time in milli-second with GMT (BTW computers don't support UTC as such) 我只是GMT的毫秒级时间(BTW计算机不支持UTC这样的时间)
long serverTime = System.currentTimeMillis(); // millis since 1/1/1970 GMT.
To get/set this in a thread safe manner you can make it volatile
为了以线程安全的方式获取/设置它,您可以使其volatile
BTW Calender is pretty slow even on a PC. 即使在PC上,BTW Calender也相当慢。 I would avoid using it on a phone. 我会避免在手机上使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.