Java时区更新为double值 [英] Java timezone update by double values
问题描述
我有一个表,该表具有国家时区范围
例如
I have a table that has country timezone range e.g.
国家/地区:加拿大
开始范围:-3.5
结束范围:-8.0
Country: Canada Start Range: -3.5 End Range: -8.0
我有一种方法可以从表
中拉回起始和结束范围值,我需要将系统时间(UTC)转换为国家时间范围。
如果我要处理整数,那就很好了,只需使用Calendar添加带有int值的常规方法,但我的方法返回双精度值
I have a method that pulls back the start and end range values from the table I need to convert the system time (UTC) to the country time range. If I was dealing with whole numbers it would be fine, and would just use the Calendar add regular methods that take int values but my method return doubles
I有:
private Calendar[] setCountryTime( double startRange double endRange) {
Calendar[] clientTimeRange = null;
Date today = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(today);
Calendar clientStartRange = getCurrentServerTime();
clientStartRange.add(Calendar.HOUR_OF_DAY, //what value here);
clientStartRange.add(Calendar.MINUTE, //what value here);
clientTimeRange[0] = clientStartRange;
Calendar clientEndRange = getCurrentServerTime();
clientEndRange.add(Calendar.HOUR_OF_DAY, //what value here);
clientEndRange.add(Calendar.MINUTE, //what value here);
clientTimeRange[1] = clientEndRange ;
return clientTimeRange;
}
推荐答案
java。时间
java.time
private static final long HOURS_TO_SECONDS = TimeUnit.HOURS.toSeconds(1);
private static OffsetDateTime[] setCountryTime(double startRange, double endRange) {
ZoneOffset startOffset = hoursToOffset(startRange);
ZoneOffset endOffset = hoursToOffset(endRange);
Instant now = Instant.now();
return new OffsetDateTime[] { now.atOffset(startOffset), now.atOffset(endOffset) };
}
private static ZoneOffset hoursToOffset(double offsetHours) {
long offsetSeconds = Math.round(offsetHours * HOURS_TO_SECONDS);
if (offsetSeconds < ZoneOffset.MIN.getTotalSeconds()
|| offsetSeconds > ZoneOffset.MAX.getTotalSeconds()) {
throw new IllegalArgumentException("Offset too large " + offsetHours);
}
return ZoneOffset.ofTotalSeconds((int) offsetSeconds);
}
让我们尝试一下:
OffsetDateTime[] canadaTimeRange = setCountryTime(-3.5, -8.0);
System.out.println(Arrays.toString(canadaTimeRange));
现在正在运行此打印:
[2018-05-23T10:06:47.489234-03:30,2018-05-23T05:36:47.489234-08:00]
[2018-05-23T10:06:47.489234-03:30, 2018-05-23T05:36:47.489234-08:00]
由于我们可以从几秒钟中实例化 ZoneOffset
,所以主要的窍门是将小时数转换为秒。如果我们只有整整几个小时, TimeUnit
枚举可以为您做到这一点。我们仍然可以将其转换因子用于乘法。
Since we can instantiate a ZoneOffset
from a number of seconds, the main trick is to convert your hours to seconds. The TimeUnit
enum could do that for you if we had only whole hours. We can still use its conversion factor for the multiplication. It makes our intention clearer and the code less error-prone.
Calendar
类早已过时且设计欠佳,这使我们的意图更清晰,代码更少出错。 。相反,我使用 java.time
(现代的Java日期和时间API)。与之合作真是太好了。如果确实需要日历
对象,例如对于您无法更改的旧版API,则可以进行转换。
The Calendar
class is long outdated and poorly designed. Instead I use java.time
, the modern Java date and time API. It is so much nicer to work with. If you do need Calendar
objects, for example for a legacy API that you cannot change, a conversion can be made.
EDIT 没什么大问题, java.time
在Java 6和7上运行良好。
EDIT No big problem, java.time
works nicely on Java 6 and 7.
- 在Java 8和更高版本以及较新的Android设备上(通过API)有人告诉我,第26级是内置的现代API。
- 在Java 6和7中,获得了ThreeTen Backport,即新类的Backport(JSR 310的ThreeTen;请参见
- 在(较旧的)Android上使用Android版的ThreeTen Backport。叫做ThreeTenABP。并确保使用子包从
org.threeten.bp
导入日期和时间类。
- In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
- Oracle教程:日期时间解释了如何使用
java.time
。 - Java规范请求(JSR)310 ,其中
java.time
首次被描述。 - ThreeTen-Backport 项目,它是
java.time
到Java 6和7(JSR-310的ThreeTen)的反向端口。 - ThreeTenABP ,ThreeTen Backport的Android版本
- 问题:如何
- Oracle tutorial: Date Time explaining how to use
java.time
. - Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen-Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
这篇关于Java时区更新为double值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!