jython date conversion

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?


Asked by: Daryl250 | Posted: 05-10-2021






Answer 1

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))

Answered by: Brianna443 | Posted: 06-11-2021



Answer 2

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)

Answered by: Lily375 | Posted: 06-11-2021



Similar questions

python - Conversion SQL to Jython with list and dict

Trying to convert this script in Jython (which doesn't have pandas) It uses the tables Work, Budget, Grade, Office. Now they are no longer tables, Work and budget are a List Grade and Office are Dictionaries


python - long <-> str binary conversion

Is there any lib that convert very long numbers to string just copying the data? These one-liners are too slow: def xlong(s): return sum([ord(c) &lt;&lt; e*8 for e,c in enumerate(s)]) def xstr(x): return chr(x&amp;255) + xstr(x &gt;&gt; 8) if x else '' print xlong('abcd'*1024) % 666 print xstr(13**666)


python - Django and units conversion

I need to store some values in the database, distance, weight etc. In my model, I have field that contains quantity of something and IntegerField with choices option, that determines what this quantity means (length, time duration etc). Should I create a model for units and physical quantity or should I use IntegerField that contains the type of unit?


python - pytz utc conversion

What is the right way to convert a naive time and a tzinfo into an UTC time? Say I have: d = datetime(2009, 8, 31, 22, 30, 30) tz = timezone('US/Pacific') First way, pytz inspired: d_tz = tz.normalize(tz.localize(d)) utc = pytz.timezone('UTC') d_utc = d_tz.astimezone(utc) Second way, from


Python Type Conversion

Whats the best way to convert int's, long's, double's to strings and vice versa in python. I am looping through a list and passing longs to a dict that should be turned into a unicode string. I do for n in l: {'my_key':n[0],'my_other_key':n[1]} Why are some of the most obvious things so complicated?


python - png24 to png8 conversion

I want to convert the input 24 bit PNG image to 8 bit, I have tried using Imagemagick and Python PIL, neither works. for instance: at Imagemagick I try convert console command as such: convert -depth 8 png24image.png png8image.png And here is the way I tried with python: import Image def convert_8bit(src, dest): """ convert_8bit: String, String -&gt;...


python - numpy arrays type conversion in C

I would like to convert the numpy double array to numpy float array in C(Swig). I am trying to use PyObject *object = PyArray_FROM_OT(input,NPY_FLOAT) or PyObject *object = PyArray_FROMANY(input,NPY_FLOAT,0,0,NPY_DEFAULT) or PyObject *object = PyArray_FromObject(input,NPY_FLOAT,0,0) or PyObject *object = P...


regex - python string conversion for eval

I have list like: ['name','country_id', 'price','rate','discount', 'qty'] and a string expression like exp = 'qty * price - discount + 100' I want to convert this expression into exp = 'obj.qty * obj.price - obj.discount + 100' as I wanna eval this expression like eval(exp or False, dict(obj=my_obj)) m...


python - string conversion

I’ve got a long string object which has been formatted like this myString = “[name = john, family = candy, age = 72],[ name = jeff, family = Thomson, age = 24]” of course the string is longer than this. Also i have 3 lists with related names: Names = [] Families = [] Ages = [] I want to read that string character by character and take the data and a...


Python to C# conversion

In python i'm creating .an file using commandStr="dpan.exe -np -Lwork_%s.lib -Owork_%s.lib %s %s.an" %( option1, option2, Sourcefile, Destination file) os.system( commandStr ) This will create the .an file (Destination file) from Sourcefile. Now i'm converting this line of code from Python to C# So how do i do this in C#. How to run the commandStr


python - RGB to HSV conversion using PIL

I'm trying to automate the enhancement of some images that are to be transfered to a digital frame. I have code in place that resizes, adds a date/time to the least-significant (least details) corner of the image and pastes together pairs of portrait images to avoid displaying a single portrait in the frame's 41:20 low resolution screen. I've implemented a brightness-stretching filter for those pictures where the l...


python - long <-> str binary conversion

Is there any lib that convert very long numbers to string just copying the data? These one-liners are too slow: def xlong(s): return sum([ord(c) &lt;&lt; e*8 for e,c in enumerate(s)]) def xstr(x): return chr(x&amp;255) + xstr(x &gt;&gt; 8) if x else '' print xlong('abcd'*1024) % 666 print xstr(13**666)


UTF-8 latin-1 conversion issues, python django

ok so my issue is i have the string '\222\222\223\225' which is stored as latin-1 in the db. What I get from django (by printing it) is the following string, 'ââââ¢' which I assume is the UTF conversion of it. Now I need to pass the string into a function that does this operation: strdecryptedPassword + chr(ord(c) - 3 - intCounter - 30) I get this error: chr() ...


python - Django and units conversion

I need to store some values in the database, distance, weight etc. In my model, I have field that contains quantity of something and IntegerField with choices option, that determines what this quantity means (length, time duration etc). Should I create a model for units and physical quantity or should I use IntegerField that contains the type of unit?


python - need help - bit-field conversion

I want to convert strings to bit-fields.Also,convert them to binary and then use. Need help with this..help me ..


Ruby to python one-liner conversion

I have a little one-liner in my Rails app that returns a range of copyright dates with an optional parameter, e.g.: def copyright_dates(start_year = Date.today().year) [start_year, Date.today().year].sort.uniq.join(" - ") end I'm moving the app over to Django, and while I love it, I miss a bit of the conciseness. The same method in Python looks like: def copyright_dates...


python - pytz utc conversion

What is the right way to convert a naive time and a tzinfo into an UTC time? Say I have: d = datetime(2009, 8, 31, 22, 30, 30) tz = timezone('US/Pacific') First way, pytz inspired: d_tz = tz.normalize(tz.localize(d)) utc = pytz.timezone('UTC') d_utc = d_tz.astimezone(utc) Second way, from


casting - Automatic String to Number conversion in Python

I am trying to compare two lists of string in python. Some of the strings are numbers however I don't want to use it as number, only for string comparison. I read the string from a file and put them on a list like this: def main(): inputFileName = 'BateCarteira.csv' inputFile = open(inputFileName, "r") bankNumbers = [] for line in inputFile: values = line[0:len(line)-1].spl...


java - media conversion library

I am building a mobile website were users can upload/download videos, and I need a library that can convert the media files from mpeg, 3gp, mov depending on what the user wants to download. Do you happen to know a a library that can do this?


python source code conversion to uml diagram with Sparx Systems Enterprise Architect

Please let me know how to create a uml diagram along with its equivalent documentation for the source code(.py format) using enterprise architecture 7.5 Please help me find the solution, I have read the solution for the question on this website related to my topic but in vain


python - Emulate floating point string conversion behaviour of Linux on Windows

I've encountered an annoying problem in outputting a floating point number. When I format 11.545 with a precision of 2 decimal points on Windows it outputs "11.55", as I would expect. However, when I do the same on Linux the output is "11.54"! I originally encountered the problem in Python, but further investigation showed that the difference is in the underlying C runtime library. (The architecture is x86-x64 in b...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top