Convert JRuby DateTime object to Java.sql.Timestamp - jruby

I am using JRuby Version 1.7.11. I have a requirement of converting a date string in JRuby to a java.sql.Timestamp. I am a newbie to JRuby so need some help and how to get this done. Tried like below and I am stuck.
require 'date'
datePart = DateTime.parse('2014-04-30')
puts datePart
I am not sure how I convert this to a java.sql.Timestamp object.
Any thoughts?

[A little old, so might have been solved by now - please let us know your findings.]
Your DateTime call gives you a DateTime object.
The java.sql.Timestamp object can be constructed from a explicit year, month, day etc details or via milliseconds argument.
So, you could either do
java.sql.Timestamp.new(datePart.year-1900, datePart.month-1,... etc)
or
# convert to Ruby Time, so that the Java conversion gives you a Java Date object
java.sql.Timestamp.new(datePart.to_time.to_java.time)
There might be a way to get from the DateTime object directly to the milliseconds value, but I don't see it.
Cheers.

Related

Azure Logic Apps - Convert JSON Epoch Timestamp to DateTime String

I am working on an Azure Logic App that is triggered via an HTTP call and returns a JSON response. Internally, the logic app retrieves JSON data from a web API and then converts the response JSON data to a format that is acceptable to the calling client of the logic app.
The problem I'm having is that the web API is returning dates in the format "/Date(1616371200000)/" and I need the date format to look like "2021-03-32T19:00:00Z". I don't see any built-in logic app function that can work with or convert the Epoch timestamp as-is (unless I'm missing something).
To clarify...
Source Data:
{
"some_silly_date": "/Date(1616371200000)/"
}
Desired Data:
{
"some_silly_date": "2021-03-32T19:00:00Z"
}
The following solution would theoretically work if the source date wasn't wrapped with "/Date(...)/":
"#addToTime('1970-01-01T00:00:00Z', 1616371200000, 'Second')"
Parsing that text off the timestamp before converting it would lead to a really ugly expression. Is there a better way to convert the timestamp that I'm not aware of?
Note that using the Liquid JSON-to-JSON templates is not an option. I was using that and found this action apparently has to JIT compile before use which is causing my logic app to time-out when being called after a long period of inactivity.
Can you get the value "/Date(1616371200000)/" from the JSON into a variable? If so, a little string manipulation would do the trick:
int(replace(replace(variables('data_in'),'/Date(',''),')/',''))
Then use the variable in the addToTime function.
Result:
The following expression seems to be working and returns a timestamp in UTC. Note that the substring() function is only using a length of 10 instead of 13. I'm intentionally trimming-off the milliseconds from the Epoch timestamp because the addToTime() function only handles seconds.
{
"some_silly_date": "#addToTime('1970-01-01T00:00:00Z', int(substring(item()?['some_silly_date'],6,10)), 'Second')"
}
As an added bonus, if the timestamp value is null in the source JSON, do the following:
{
"some_silly_date": "#if(equals(item()?['some_silly_date'], null), null, addToTime('1970-01-01T00:00:00Z', int(substring(item()?['some_silly_date'],6,10)), 'Second'))"
}
Quite possibly the ugliest code I've ever written.

Is it possible to write date as DateTime format in JSON using Python without converting it to string

I want to know if there is a way to write the date as DateTime format in JSON.
I have followed so many links on the internet but everywhere date is converted to string(str) in order to write it on JSON file.
I used the below code:
import json
fileName='json_output.json'
def writeToJSONFile(data):
with open(fileName, 'a+') as fp:
json.dump(data, fp, indent=4, default=str)
then calling it as :
from datetime import datetime
date_value="09-23-2019"
date_time = datetime.strptime(date_value,'%m-%d-%Y')
date_dict={"eventDate":date_time}
writeToJSONFile(date_dict)
The above code is able to write date into the JSON file but in the string format.
I have already went through link:
How to overcome "datetime.datetime not JSON serializable"?
JSON datetime between Python and JavaScript
I just want to know if it is possible or not at all possible to store date as datetime format?
Simple answer is No, JSON does not have a native datetime representation, so it will appear as a string; however, if you use a standard that is agreed upon, the receiving application can parse the variable into a datetime object, if they choose to. If you do not know what format they may agree with, I would recommend just making it a standard ISO 8601(Combined date and time in UTC), that way it can be parsed by the receiver, and retain the correct value regardless of time zone.

in Java 8, how to serialize/deserialize a date time information with time zone

I would like to deserilize a json string with datetime information such as 2016-07-22T11:20:48.430-07:00 to a date time object using Jackson, currently, I am using joda Datetime, it works fine, I was able to convert 2016-07-22T11:20:48.430-07:00 to Datetime (UTC).
However, I want to use java 8 date time object, any recommendation? localDateTime do not have time zone information, and zoneDateTime seems not be able to deal with format like: 2016-07-22T11:20:48.430-07:00
OffsetDateTime
Your input strings have an offset-from-UTC not a time zone. A time zone is an offset plus a set of rules for handling anomalies such as Daylight Saving Time (DST).
For date-time values with an offset, use the aptly named OffsetDateTime class.
OffsetDateTime odt = OffsetDateTime.parse( "2016-07-22T11:20:48.430-07:00" );
You can apply a full time zone if desired.
ZoneId zoneId = ZoneId.of( "Europe/Paris" );
ZonedDateTime zdt = odt.atZone( zoneId );
For UTC value, extract an Instant.
Instant instant = odt.toInstant();
Adapter library
I don't use Jackson myself but I know you can use various adapter classes to handle java.time classes. See the first comment on the Question for one. See this Answer on a similar Question for another.
Hopefully Jackson will eventually be modernized to handle these types directly.

Restkit JSON error parsing dates in range 1969/12/07 to 1970/01/25 GMT

Running Restkit 0.23 and iOS7.1.2 and iOS8gms when dates in range 1969/12/08 to 1970/01/25 GMT are encountered the parsing to JSON fails with an error: [datasource.table.values.property]: Parsing date N was not recognized as a date format... where N is a number of seconds.
I am unable to determine the JSON parser that is in use.
Restkit documentation refers to either YAJL or SBJSON being pluggable.
If someone can advise how to identify the parser in use and to change to another parser that would be much appreciated. Otherwise if anyone can advise an alternative resolution that would also be helpful.
This was a problem with the backend which is using FlexJSON and is a known feature of FlexJSON:
(http://sourceforge.net/p/flexjson/discussion/686321/thread/029d17be/).
Trying to use JSONDeserializer but Flexjson is having issues parsing the Unix TimeStamps into Date objects, only if the date is in the range of Dec. 7th, 1969 to Jan. 25th, 1970. All other dates outside of that range are parsed fine. The error that it's giving is: [JSONException: : Parsing date 21600000 was not recognized as a date format]
So no problem at all with the iOS native JSON parser which is what RestKit is using, RestKit is simply faithfully passing back the server error messages.

Jackson 2.0 Mapper returns wrong Date format

i use a postgresql database and i save there a normal "timestamp without timezone" value.
this looks like this in my database:
2014-05-09 16:04:01.889
now i have created a pojo with a JsonFormat annoation to format my timestamp:
#JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd.MM.yyyy,HH:mm")
private Timestamp date;
but this returns me:
date": "09.05.2014,14:04"
The hours are wrong, it should be 16 and not 14.. what do i wrong ?
The difference in hours definitely comes from the difference in the time zones. According to the Jackson Date/Time FAQ, Jackson uses the GMT timezone by default. I suggest you to follow the wiki page and this stackoverflow answer to fix the handling of the date/time in your application.