Deserializing a date with two possible formats with Jackson - java

I am deserializing some JSON which contains date fields from a web service. Almost all of the time the date comes in like this:
2017-01-18T07:20:00Z
But on vary rare occasions that date comes in like this:
2017-01-18T07:20:42.9295582Z
The second time it seems to not pick up the Z at the end. I'm just going by the logs at the moment but was just wondering if anyone had any ideas.

Related

Can I store a date in h2 database using long data type for date field in entity object?

I am new to spring boot, working on a project, and recently an experienced developer just told me that when creating an entity table, that has a field that should deal with dates the best approach is to use long, something like this:
private Long deliveryDate;
The thing is that I have never seen anything being done like this before, I do not understand how this should exactly work, and I couldn't find an answer anywhere I searched for it, so do you think that some of you can help me?
The fellow developer is probably referring to an instant, a certain point on the timeline at which the thing (probably order) is delivered.
The commonly used notation of an instant is the Unix Timestamp. It's the number of seconds since Jan 1, 1970, at midnight UTC. This number does not depend on timezone.
I would use Instant instead of Long, as this is the type that represents such a, well, instant. I do not know for sure whether it is automagically wired by Spring Boot, but you may have to add the JSR 310 module if you encounter problems. Here is more information.

Best strategy to persist ISO8601

I have been reading different articles on the said question yet i am unable to figure out what should be the best strategy to store the date in db.
I will be receiving the ISO8601 date via path-param in a rest call. What I have decided
Use Joda-Time to parse the date.
Extract UTC-0 time out of the date and the hours offset
Store UTC-0 in DateTime datatype in mysql db and store offset in varchar(5).
When I have to search something based on the date (an exposed rest api). I will use the search criteria (input date) extract the UTC-0 time and hours offset and compare the two columns in the db i.e. where table.dateInUTC0 = :inputDateInUTC0 AND table.hoursOffset = :inputHoursOffset
I am not sure about step 4. Am i doing i right ?
I am not sure about step 4. Am i doing i right ?
Really, it depends on what you are trying to do.
If you want the search to only match if the searcher is using the same timezone as the original data, then you are doing it right.
If you don't want that, you are doing it wrong.
Ask yourself this: If you enter the same date / time in your local time zone and UTC (or some other time zone), do they mean the same thing for clients of your server? Should they?

JacksonJaxbJsonProvider date format

We are using CXF in combination with Jackson (JacksonJaxbJsonProvider) to marshall domain objects into JSON. Everything is working good, with the exception that I cannot get dates to format the way I want them to. In short, what I want, is to output dates as seconds since epoch (also called unix time). This is partly doable with SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, but this gives me milliseconds, not seconds. As my dates don't have such high precision (and never will), I am wasting 4 bytes for every timestamp.
To my knowledge, the only way I can control the date format is by using setDateFormat() on ObjectMapper. This function accepts a DateFormat. However, it does not seem like a DateFormat can output seconds since epoch, only milliseconds.
Are there any other ways of doing this?

Java Date Time discrepancy

While writing integration test I was expecting a hardcoded date in the response.
Basically I hardcoded expected date value '2020-11-10T00:00:00.000-05:00'
and ran new GregorianCalendar(2020, 10, 10).getTime()
When I put and assert and run it locally it passes, however when the same code was pushed to bamboo build server the actualValue it generated was'2020-11-10T00:00:00.000Z' and so my test failed.
Why the same calendar.getTime generating two different times, is it because the server machine is configured to be on GMT?
Can I do something to have them the same time or any other workaround?
NOTE: Making it string or comparing dates without time is not an option here, as I am using Spring MockWebServiceServer, where in I must hardcode the responseXML and specify date, something like this in Enum.
REQUESTAUTOMATESETTLEMENTWORKCASE("<aut:AutomateSettlementWorkcaseRequest xmlns:aut=\"http://www.abcd.com/abcd/workflow/services/workcase/model/AutomateSettlementWorkcase_1_0_0\">" +
" <aut:customerAccountId>5049903033584324</aut:customerAccountId>\n" +
" <aut:settlementDate>2020-11-10T00:00:00.000-05:00</aut:settlementDate>\n" +
" </aut:AutomateSettlementWorkcaseRequest>"),
The "-5" part in your input data is throwing it off... ideally you should specify the time zone when constructing the calendar, and then set it to 5am UTC (for example). Basically, 2020-11-10T00:00:00.000-05:00 is the same instant as 2020-11-10T05:00:00.000Z.
Now, what we don't know is whether it's important to you that you preserve the offset from UTC. If it is, you need to set an appropriate time zone in the calendar - one which has the same rules as whatever's generating your input data. If it isn't, I'd use UTC and set the time appropriately.
I would personally recommend using Joda Time instead of Calendar and Date though - it's a much better date and time API. No 0-based months, for starters :)

How to extract portions of joda Date time effectively?

I have a Joda Date Time object that contains date and time fields and I wish to extract the date and time portions into 2 different sql date and time objects (java.sql.). I thought converting the DateTime to milliseconds and storing it in the respective sql objects would do the trick however it produces calculation errors further upstream in my app logic as one is === to the other.
Best way I've found to do this so far is what I've described in my comment on the question and then convert back to sql time using:
new java.sql.Date(locDate.toDateTime(LocalTime.MIDNIGHT).getMillis()));
new java.sql.Time(locTime.toDateTimeToday().getMillis()) );
Couldn't find anything in the DateTime object API to extract the respective parts.

Categories