Strict Date Format Using Jackson and Kotlin - kotlin

I'm using Jackson, Kotlin and Quarkus and my object mapper registers Java Time Module, Kotlin Module and Serialize feature write dates as timestamps has been disabled
I have a data class that represents my request body, it contains an LocalDate field called creationDate by default the format that I get is yyyy-mm-dd.
As the date that come in here is getting used to do a network call, I want to change the format to MM/dd/yyyy to facilitate that
What is the best design that needs to be followed
1.) Get Date in MM/dd/YYYY format and send it ahead ? If so then how can I define a default format for my local date
Or
2.) Get default Local Date format from Client and later parse it to MM/dd/yyyy
The main thing that I am having trouble is define a default format for my date field
data class( val createDate: LocalDate, val accountId: Int) // this is my data class for input request

ISO 8601
The default format that you get, yyyy-mm-dd, is ISO 8601 and is the recommended format for data interchange.
How come you need a different format for your network call, I don’t know. The very best thing would be if you could have that network call changed to accept ISO 8601 format too. Under no circumstances should you let this custom format pollute the JSON that you pass in your request. So second best is to get default date format from client and only format it to MM/dd/yyyy for the network call where this format is required.
Link: ISO 8601 - Wikipedia

Related

java.util.Date to kotlinx.datetime.LocalDateTime

I have a value of type java.util.Date which was obtained from a legacy third-party API. Is there a direct way of converting it to kotlinx.datetime.LocalDateTime? I know how to do it only in a roundabout way, such as serializing to String and deserializing, or by converting to java.time.LocalDateTime first and then to the wanted type.
i Think the best thing you can do is to convert it to a string and than convert into a kotlinx.datetime.LocalDateTime
The problem is that not all java types can be converted directly into a kotlin one especially not with the old DataType java.util.Date.
It is also posible with the new DataType java.time.LocalDateTime.

Parse date-time field defined in Swagger file

I have an API written in Swagger 2.0 that says an entity has a property called when of type date-time:
properties:
when:
type: string
format: date-time
I don't know how to parse the string. How should I expect the date-time format to looks like? I cannot find this in the Swagger 2.0 documentation
As per the Open API 2.0 spec, the date-time should be defined by RFC3339.
For example:
2016-03-22T21:03:41
1985-04-12T23:20:50.52Z
1990-12-31T15:59:60-08:00
I don't know how to parse the string.
This would depend on the language you're using. In JavaScript, Date.parse(dateString) can easily parse the string. Or in Java, you can refer to Converting ISO 8601-compliant String to java.util.Date to know how to parse the date string.

scalaquery how to create datetime

I foundout that scalaquery uses java.sql.date as the date object. But it drops time when I create java.sql.date.
Is there any way that I can use to create mysql datetime field in scalaquery?
There is no java.util.Date support in ScalaQuery. However, it's possible to enhance ScalaQuery with your own type mappers. For java.util.Date such a wrapper could look like this
implicit val JavaUtilDateTypeMapper = MappedTypeMapper.base[java.util.Date, Long] (_.getTime, new java.util.Date(_))
converting the java.util.Date into a simple Long. It depends on your database what's the best target, in my case a Long works perfectly.
Instead of using java.sql.Date, try using java.sql.Timestamp, when declaring the column.
It looks like that yields a better mapping for a column with both date and time elements.

XmlSerializer. Deserializion of empty Date and Time elements

I am trying to deserialize an Xml file content into a specific object. Everything runs fine but when I include empty tag of type date or time in my xml(having xsd that describes my xml structure) the deserialization fails. What can I do to make deserialization working when empty date tags are appear?
Thanks
Pan
A DateTime cannot be empty - instead try using the DateTime.Min value.

SQL Compact DateTime Weirdness

I'm having a weird issue concerning Microsoft SQL Compact Edition. In a Windows.Forms application, I try to modify a database which was created by the same (.Net 2.0) application. The database gets sent to a Windows Mobile Phone later, but this is not important. What's important is that on the normal regional settings of my computer, which is English (USA), inserting DateTime values in the database happens without any problems. However, when I switch the locale to Dutch (Netherlands), I get the following error:
"There was an error in a part of the date format. [ Expression (if known) = ]"
I tracked it down to the way DateTime looks in NL. However, I do not use DateTime.ToString(). I add it to the SQL insert/update statements purely as "column = " + DateTime. This works perfectly in "en-US" but when I switch to Dutch, it blows up.
The way I fixed this is by creating an extension method for the DateTime datatype like so:
/// <summary>
/// Transforms a DateTime from various cultures into what SQL compact expects to get.
/// </summary>
/// <param name="original">DateTime to process.</param>
/// <returns>A SQL-friendly string.</returns>
public static string _ToSQLDateTimeString (this DateTime? original)
{ //No provided Date? Bye.
if (original == null) return null;
IFormatProvider usDate = new CultureInfo("en-US");
return ((DateTime)original).ToString(usDate);
}
But I would like somebody to maybe confirm / improve my solution. Maybe I missed something?
But "column = " + DateTime implicitly calls ToString() on the DateTime.
You should use System.Data.SqlClient.SqlCommand and add the dates (and all other parameters) with command.Parameters.Add(...).
This will fix all localization issues with dates, floats, etc. And protect your application from SQL injection attacks.
If you want to push the DATETIME into the database a string, you're best of using the 2010-08-25T07:26:05 format - however, better would be to create a command object with parameters of the right types and then set the DATETIME parameter to the DateTime object rather than stringifying it.
SQL Server won't convert abritrary date string formats - it'll use the format dictated by whichever locale it's running under (default is US).