英文:
Java Time API get offset from sql Timestamp given the Chronology
问题
以下是您要的翻译内容:
我有以下代码,它使用Joda Time库从sql Timestamp对象获取偏移量。
public static long getOffset(Chronology chronology_, Timestamp timeStamp_) {
long offset = 0;
if (chronology_ != null) {
offset = chronology_.getZone().getOffset(new DateTime(timeStamp_).getMillis());
}
return offset;
}
如何使用Java 8 API实现相同的功能。我不确定是否还需要使用Chronology。
英文:
I have the following code which gets offset from sql Timestamp object using the Joda Time Library.
public static long getOffset(Chronology chronology_, Timestamp timeStamp_)
{
long offset = 0;
if (chronology_ != null)
{
offset = chronology_.getZone().getOffset(new DateTime(timeStamp_).getMillis());
}
return offset;
}
How can achieve the same using Java 8 API. I am not sure if chronology is required any more.
答案1
得分: 1
虽然 Joda-Time 的年代概念和 java.time (JSR-310) 的年代概念相似,但在你的情况下存在一个重要的区别:Joda-Time 的 Chronology
可能(可选地)具有时区,而 java.time.chrono.Chronology
则不行。因此,你需要以某种其他方式提供用于操作的时区,而不是通过年代。
顺便说一下,我可能会建议你提供时间点的方式与 java.sql.Timestamp
不同。所以一个选项是:
public static long getOffset(ZoneId zone, Instant when)
{
long offset = 0;
if (zone != null)
{
int offsetSeconds = zone.getRules()
.getOffset(when)
.getTotalSeconds();
offset = TimeUnit.SECONDS.toMillis(offsetSeconds);
}
return offset;
}
如果你的调用者从无法更改的旧版 API 中获得了老式的 Timestamp
,他们应该进行转换。所以调用上述方法的一种方式可能是:
long offsetMillis = getOffset(
ZoneId.of("Africa/Khartoum"), theirTimesatmp.toInstant());
System.out.println(offsetMillis);
使用当前时间附近的时间戳的示例输出:
> 7200000
Timestamp
类的设计较差,在已经设计不佳的 java.util.Date
类的基础上进行了真正的修改,因此我们不应该使用它。如果我们无法避免获取一个时间戳,我们应该立即将其转换为 Instant
或 LocalDateTime
,然后从那里执行进一步的工作。
进一步的一个良好改进是,如果你的方法返回从 getOffset()
返回的 ZoneOffset
对象,而不是一个可能让调用者不确定是秒、毫秒还是其他单位的数字。
如果你坚持提供一个接受 Timestamp
的便利方法,当然可以添加一个对过去友好的包装器。例如:
/** @deprecated use {@link #getOffset(ZoneId, Instant)} instead */
public static long getOffset(ZoneId zone, Timestamp timeStampParam)
{
return getOffset(zone, timeStampParam.toInstant());
}
链接: 从 Joda-Time 转换为 java.time Stephen Colebourne 的博客上。
英文:
While the Joda-Time concept of a chronology and the java.time (JSR-310) concept of a chronology are similar, there is a difference that matters in your situation: The Joda-Time Chronology
may (optionally) have a time zone. The java.time.chrono.Chronology
cannot. So you need to provide the time zone to use for the operation in some other way than through a chronology.
Now we’re at it, I might suggest that you also provide the point in time in some other way than through a java.sql.Timestamp
. So one option would be:
public static long getOffset(ZoneId zone, Instant when)
{
long offset = 0;
if (zone != null)
{
int offsetSeconds = zone.getRules()
.getOffset(when)
.getTotalSeconds();
offset = TimeUnit.SECONDS.toMillis(offsetSeconds);
}
return offset;
}
If your caller has got an old-fashioned Timestamp
from a legacy API that they cannot change, they should convert. So one way of calling the above method would be:
long offsetMillis = getOffset(
ZoneId.of("Africa/Khartoum"), theirTimesatmp.toInstant());
System.out.println(offsetMillis);
An example output using a timestamp from around now:
> 7200000
The Timestamp
class is poorly designed, a true hack on top of the already poorly designed java.util.Date
class, so we should not use it. If we can’t avoid getting one, we should convert it to either Instant
or LocalDateTime
immediately and perform or further work from there.
A further nice improvement will be if your method returns the ZoneOffset
object returned from getOffset()
rather than a number that may leave a caller wondering whether it’s seconds, milliseconds or some other unit.
If you do insist on providing a convenience method that accepts a Timestamp
, you may of course add a wrapper that is friendly to the past. For example:
/** @deprecated use {@link #getOffset(ZoneId, Instant)} instead */
public static long getOffset(ZoneId zone, Timestamp timeStampParam)
{
return getOffset(zone, timeStampParam.toInstant());
}
Link: Converting from Joda-Time to java.time on Stephen Colebourne’s blog
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论