Jython 日期转换

发布于 2024-07-09 10:52:56 字数 123 浏览 4 评论 0原文

给定如下字符串,我需要使用 jython 将

1 Dec 2008 06:43:00 +0100

转换为

MM/DD/YYYY HH:MM:SSAM

最好的方法是什么?

Given a string as below, I need to convert:

1 Dec 2008 06:43:00 +0100

to

MM/DD/YYYY HH:MM:SSAM

using jython what is the best way to do this?

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

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

发布评论

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

评论(2

埖埖迣鎅 2024-07-16 10:52:56

我手边没有 jython,但我希望这样的东西能够工作:

import java
sdf = java.text.SimpleDateFormat

fmt_in = sdf('d MMM yyyy HH:mm:ss Z')
fmt_out = sdf('MM/dd/yyyy HH:mm:ssaa')

fmt_out.format(fmt_in.parse(time_str))

I don't have jython handy, but I'd expect something like this to work:

import java
sdf = java.text.SimpleDateFormat

fmt_in = sdf('d MMM yyyy HH:mm:ss Z')
fmt_out = sdf('MM/dd/yyyy HH:mm:ssaa')

fmt_out.format(fmt_in.parse(time_str))
稀香 2024-07-16 10:52:56

Jython 2.5b0(测试版)具有 时间模块,包括

strptime(字符串[,格式])

根据格式解析表示时间的字符串。 返回值是 gmtime() 或 localtime() 返回的 struct_time。

(Jython2.2.1 中缺少 strptime)。

转换格式的 python 版本如下所示(不确定区域组件):

import time
mytime = time.strptime("1 Dec 2008 06:43:00 +0100", "%d %b %Y %H:%M:%S %Z")
new_time_string = time.strftime("%m/%d/%Y %I:%M:%S%p", mytime)

Jython 2.5b0 (beta) has an implementation of the time module that includes

strptime(string[, format]).

Parse a string representing a time according to a format. The return value is a struct_time as returned by gmtime() or localtime().

(strptime is missing in Jython2.2.1).

A python version of the conversion formats will look like (not sure of the zone component):

import time
mytime = time.strptime("1 Dec 2008 06:43:00 +0100", "%d %b %Y %H:%M:%S %Z")
new_time_string = time.strftime("%m/%d/%Y %I:%M:%S%p", mytime)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文