How do you parse basic (short/compact) ISO:8601 string with Joda? - timezone-offset

I have a time string, which looks like this: 20170822T194135+00. This is called basic ISO:8601 format, if I understood correctly.
When I try to parse it using ZonedDateTime, it throws exception, complaining that it can't parse it.
SO, how do I convert this string to a valid Joda datetime object?
Do I need to build manual "format" to parse it (this would be silly, considering it's a standard format)?
Desperately, I've actually tried to implement custom format:
const time = ZonedDateTime.parse(timeString, DateTimeFormatter.ofPattern(`yyyyMMdd'T'HHmmssZ`));
But, it throws error on column 15. Looks like it fails to parse the timezone. Is my implementation correct? How do I make it work?

I could do it using the pattern x as described in the docs (this pattern accepts offsets like +00).
Then I parsed directly to a ZonedDateTime:
const formatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmssx");
const time = ZonedDateTime.parse("20170822T194135+00", formatter);
The resulting time variable has the value equivalent to 2017-08-22T19:41:35Z.
The built-in formatters (such as ISO_LOCAL_DATE_TIME) can't parse this format, so the only way seems to be creating a formatter.

Related

Flask Restplus : Sending DateTime as a parameter [duplicate]

Let's say that I have following parser inside my get method:
from flask.ext.restful import reqparse
parser = reqparse.RequestParser()
parser.add_argument('when', type=datetime, help='Input wasn\'t valid!')
And then I want to test the said get method with curl...
curl --data "when=[WHAT SHOULD I WRITE HERE?]" localhost:5000/myGet
So the question is, how I should call the get method? I've tried numerous different formats, tried to read rfc228 standard, etc. but I can't figure out the right format.
Kinda late, but I've just been in the same problem, trying to parse a datetime with RequestParser, and sadly the docs are not so helpful for this scenario, so after seeing and testing RequestParser and Argument code, I think I found the problem:
When you use type=datetime in the add_argument method, under the hood it just calls datetime with the arg, like this: datetime(arg), so if your param is a string like this: 2016-07-12T23:13:3, the error will be an integer is required.
In my case, I wanted to parse a string with this format %Y-%m-%dT%H:%M:%S into a datetime object, so I thought to use something like type=datetime.strptime but as you know this method needs a format parameter, so I finally used this workaround:
parser.add_argument('date', type=lambda x: datetime.strptime(x,'%Y-%m-%dT%H:%M:%S'))
As you can see in this way you can use whatever format datetime you want. Also you can use partial functool instead of lambda to get the same result or a named function.
This workaround is in the docs.
Just an update on Flask-Restful (0.3.5): it is possible to use the own library date and datetime functionality parsing, if ISO 8601 or RFC 822 suffices:
from flask_restful import inputs
parser.add_argument('date', type=inputs.datetime_from_iso8601)
So the request would be,
curl --data "date=2012-01-01T23:30:00+02:00" localhost:5000/myGet
From the docs

using DateFormat to produce a "2016-12-28T17:43:47.345Z"

I have been looking at the above question and have most of it correct.
I am going to get a datetime in Zulu, and then will want to output that format.
My first go is just as simple as:
DateFormat format = new DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
My issue I am having is the T and Z. Obviously T is used to separate the date from the time and the Z is representative of Zulu time.
That being said the users will be entering a datetime in Zulu, so it wont need to be converted from Local to Zulu, so i was not sure if 'Z' is an acceptable result. I was not sure if there is a different want to handle this, or if my result was the best answer.
Try this package, Jiffy.
String isoFomart = Jiffy().format(); // This will return ISO format from now
You can also add your DateTime object
String isoFomart = Jiffy(DateTime.now()).format(); // This will also return ISO format from now
Hope this helped
The DateTime object has a method called: toIso8601String which is used to return an ISO formatted string. The 'Z' will be added if isUTC is true, otherwise the result will not have the Z in it.
Make sure that the DateTime object itself is correctly set to UTC as if you look in the constructor for the class will tell you a lot of the defaults are local with the exception of the DateTime.utc() static function.
In that concept, you dont really need a DateFormat use to define an iso string.

How to convert date format 'DDMONYYYY:HH....' style date in Hive

I am inserting dates that look like:
'19APR2014:08:42:32.123456'
I am interpreting their format as
'DDMONYYYY:HH24:MI:SS.FFFFFF'
Though I have not seen any times after 12:59:59 I am assuming a 24-hour clock. Hive does not seem to understand what I want to do:
HiveException: Error evaluating unix_timestamp(date_string,'DDMONYYYY:HH24:MI:SS.FFFFFF')
Any ideas what I am doing wrong or what might be wrong with my format string?
Have you tried ddMMMyyyy:HH:mm:ss.SSS? According to Hive manual a pattern string in function unix_timestamp(string date, string pattern) should comply to Java's SimpleDateFormat(see manual and javadocs).

Get a DateTime with an specific pattern with nscala-time

I am trying to get this pattern 'dd-MM-yyyy' with a variable of type DateTime
#{DateTimeFormat.forPattern("dd-MM-YYYY").parseDateTime(user.birthday.toString)}
But I am getting this error
java.lang.IllegalArgumentException: Invalid format: "2015-12-10T00:00:00.000Z" is malformed at "15-12-10T00:00:00.000Z"
Is there a way to do this with nscala-time?
makes a difference if I am using UTC?
UPDATE
For the moment I am casting to Date and doing this
#{Dates.format(user.birthday.toDate, "dd-MM-YYYY")}
But maybe is a better way without casting
thank you
So, if I understood your question correctly, you are trying to achieve the following:
Parse date from a string using a date format
Print/Display the date in another format.
Try the below:
#{Dates.format(DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parseDateTime(user.birthday.toString), "dd-MM-YYYY")}

Why do I need to parse dates in Grails?

I am in the unfortunate position that I need to use a composite id in a Grails app where I work with legacy data. This means I have to override some actions in the controller, but as I did this I was struck by the fact that I could not use use a date argument directly as a parameter to a dynamic method.
Instead of just doing MyLegacyObj.findBySystemIdAndLogDate(params.systemId, params.logDate), I first needed to parse the date string before giving it to the dynamic method. To further complicate matters I had no idea what format the date string had (until I added lots of log.debug() string to the output). So now I have a bit of code looking like this
def formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy")
MyLegacyObj.findBySystemIdAndLogDate(params.systemId, formatter.parse(params.logDate));
This feels unoptimal, no to say dangerous (what if the date format changes with the locale?)? What would be a recommended way of doing this, and do I really need to parse dates at all?
Date is a pretty complex object and params are just Strings, so Date is submitted in parts. It is "magically" assembled from the parts when assigning x.properties = params.
Command object will do the work for you, if you add a Date field to it.
It has nothing to do with methods' dynamic or static invocation. Your GSP that renders Date editor might interfere too.