views:

580

answers:

2

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?

+2  A: 

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))
Dustin
+1  A: 

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)
gimel
jython 2.2.1 doesnt support time.strptime.
Setori