如何将(Jython 的)日期时间类型转换为 java.util.Date?

发布于 2024-12-10 10:01:54 字数 70 浏览 5 评论 0原文

我是 Jython 新手,如何将(Jython 的)日期时间类型转换为 java.util.Date?

谢谢。

I am new to Jython, how do you convert a datetime type (of Jython) to java.util.Date?

thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南风几经秋 2024-12-17 10:01:55

请注意这一点:

java.util.Date.getTime() 给出自纪元以来的毫秒数,而 Jython(就像 CPython)给出自纪元以来的秒数。因此,您需要将 java.util.Date 给出的值除以 1000

来自 Jython 的 常见问题解答

Be aware of this:

java.util.Date.getTime() gives the milliseconds since the epoch while Jython (just like CPython) gives seconds since the epoch. So you need to divide the values given from java.util.Date by 1000

From Jython's FAQ

昔日梦未散 2024-12-17 10:01:55

这是我的解决方案:

from java.util import Calendar, GregorianCalendar

def datetimeToJavaDate(value):
    cal = GregorianCalendar(value.year, value.month - 1, value.day, 
        value.hour, value.minute, value.second)
    cal.set (Calendar.MILLISECOND, value.microsecond / 1000)
    return cal.getTime()

Here is my solution:

from java.util import Calendar, GregorianCalendar

def datetimeToJavaDate(value):
    cal = GregorianCalendar(value.year, value.month - 1, value.day, 
        value.hour, value.minute, value.second)
    cal.set (Calendar.MILLISECOND, value.microsecond / 1000)
    return cal.getTime()
原谅过去的我 2024-12-17 10:01:55

我做了一些混乱和试验...这两种方法是我使用的方法,用于转换为 java.sql.Timestamp ,然后在另一个方向上,使用 java.sql.Timestamp.time() (ms自 1970 年 1 月 1 日起)。

这里的“bst”代表英国夏令时间...我当时确实验证了它是否正确考虑了这些日期:即,tz 模块似乎足够聪明,能够检测到夏季期间的月份到 BST 并进行相应调整。

希望它能够与您可以使用的任何其他时区一起使用......

def convert_datetime_bst_to_javaSQLTimestamp( datetime_val ):
    if datetime_val is None: return None
    assert type( datetime_val ) is datetime.datetime
    tz_local = tz.tzlocal()
    gmt_datetime = datetime_val - tz_local.dst( datetime_val )
    delta = gmt_datetime - datetime.datetime( 1970, 1, 1 )
    seconds = int( delta.total_seconds() ) 
    ms_fraction = int( delta.microseconds / 1000 )
    ms = seconds * 1000 + ms_fraction
    return java.sql.Timestamp( ms )

def conv_ms_to_datetime_bst( ms ):
    assert type( ms ) is long, '# ms type %s' % ( type( ms ), )
    # NB potentially a problem with pre-1970 and post-2038 dates: datetime.datetime.fromtimestamp(ms/1000.0)
    gmt_datetime = datetime.datetime( 1970, 1, 1 ) + datetime.timedelta( 0, 0, 0, ms )
    tz_local = tz.tzlocal()
    return gmt_datetime + tz_local.dst( gmt_datetime )

I did a bit of messing around and experimenting with this... these two methods are the ones I use, for conversion to java.sql.Timestamp and then in the other direction, using java.sql.Timestamp.time() (ms since 1/1/1970).

"bst" here stands for British Summer Time... I did at the time verify that it correctly took such dates into account: i.e. it would appear that the tz module is sufficiently clever to be able to detect that months during the summer are subject to BST and adjust accordingly.

Hopefully it should work with any other time zones you can throw at it...

def convert_datetime_bst_to_javaSQLTimestamp( datetime_val ):
    if datetime_val is None: return None
    assert type( datetime_val ) is datetime.datetime
    tz_local = tz.tzlocal()
    gmt_datetime = datetime_val - tz_local.dst( datetime_val )
    delta = gmt_datetime - datetime.datetime( 1970, 1, 1 )
    seconds = int( delta.total_seconds() ) 
    ms_fraction = int( delta.microseconds / 1000 )
    ms = seconds * 1000 + ms_fraction
    return java.sql.Timestamp( ms )

def conv_ms_to_datetime_bst( ms ):
    assert type( ms ) is long, '# ms type %s' % ( type( ms ), )
    # NB potentially a problem with pre-1970 and post-2038 dates: datetime.datetime.fromtimestamp(ms/1000.0)
    gmt_datetime = datetime.datetime( 1970, 1, 1 ) + datetime.timedelta( 0, 0, 0, ms )
    tz_local = tz.tzlocal()
    return gmt_datetime + tz_local.dst( gmt_datetime )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文