Jackson: Deserialize date String `2022-05-18Z` error - jackson

When I try to deserialize date from this string format 2022-05-18Z in Jackson it throws this exception:
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "2022-05-18Z": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2022-05-18Z' could not be parsed, unparsed text found at index 10
All attempts to configure Date field from this string was unsuccessful, I did:
#JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate date;
#JsonFormat(pattern = "yyyy-MM-ddZ")
#JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime date;
#JsonFormat(pattern = "yyyy-MM-ddZ")
private Date date;
What is the right way to deserialize this string yyyy-MM-ddZ format to Date? I believe this "Z" at the end is the timezone, is there a way to represent it in this mask?
Thanks in advance.

Ok, I found the answer. After few more attempts, finally got the solution:
#JsonFormat(pattern = "yyyy-MM-dd")
private Date date;

Related

Convert string format to date format

How can I convert this string format
03.07.2019
to the following Date Format?
2019-07-03
This is how the String format I get from Soap Services looks like
Public Class ContractBill
Public StartDate As Date
End Class
Dim ctrBill As New ContractBill With {
.StartDate = Convert.ToDateTime(BillingData.START_DATE.ToString("yyyy-MM-dd"))
}
this is my attempt to convert the code to the desired format, but with this .StartDate I get the error:
System.InvalidCastException: "The object of type" System.String "cannot be converted to type" System.IFormatProvider "."
How can I get this Date format yyyy-MM-dd?
There is no format for date with ., so you have to first replace the . with - or / so you can parse your date into available formats.
string date = "03.07.2019";
DateTime value = Convert.ToDateTime(date.Replace(".", "-"));
VB.net
Dim date As String = "03.07.2019"
Dim value As DateTime = Convert.ToDateTime(date.Replace(".", "-"))
Use the DateTime.TryParseExact method since you know the format of the incoming date:
Dim enUS As New CultureInfo("en-US")
Dim inbound As String = "03.07.2019"
Dim outbound As DateTime
If DateTime.TryParseExact(inbound, "MM.dd.yyyy", enUS, DateTimeStyles.None, outbound) Then
Console.WriteLine("'{0}' is formated from: {1}", outbound.ToString("yyyy/dd/MM"), inbound)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", inbound)
End If
Fiddle: Live Demo

'System.InvalidCastException in Microsoft.VisualBasic.dll'

I have done a little bit of research on different posts and have come to the conclusion that my code as shown below:
Public Class Form1
Private Sub btnGetTime_Click(sender As Object, e As EventArgs) Handles btnGetTime.Click
getTime.ShowDialog()
lblTime.Text = DateAdd(DateInterval.Second, 0, CDate(lblTime.text))
End Sub
End Class
has an error at 'CDate(lblTime.text)'. Now I've not much of a clue what most of this code means. I suppose that's how VB works, do now learn later. The problem is that I can't convert string: lblTime.text to a date format using cDate? why not? I'm using cDate()?
The error reads:
System.InvalidCastException: 'Conversion from string "Label1" to type 'Date' is not valid.'
SOLUTION:
The problem was with a maskedtextbox, I had a string inside of it as a preview. The issue came from converting that string into a date.
It should be:
You are trying to convert a string to date. You can use Convert.ToDateTime function for that.
lblTime.Text = "2018-11-05"
Dim date as Date = Convert.ToDateTime(lblTime.Text)
date.ToString("yyyy-MM-dd"); //to convert back to string.
If your date has a custom format, take a look at DateTime.ParseExact so you can also specify the format.
You can read about it here.
If your date is in this format, 11/05/2018,
lblTime.Text = "11/05/2018"
Dim dt as Date = DateTime.ParseExact(lblTime.Text, "MM/dd/yyyy", Nothing)
date.ToString("yyyy-MM-dd"); //to convert back to string
Custom Date and Time Format Strings

Conversion from string "M" to type 'Integer' is not valid

I am having an issues with conversion of values that I am trying to reference via a field that I want to format from an ERP System. I am unable to convert all of my values because they are being pulled out as strings, no matter if variables are set to integer or string. What am I doing that would cause this error, should variables be defined a different way?
Public Class Class1
Inherits erp.Rule
Public Overrides Function Execute() As erp.RuleResult
Dim Result As New RuleResult
Try
Dim date_recieved As Date
Dim month As String
Dim period As String
Dim Year1 As String
Dim Year As String
date_recieved = Data.Fields.GetFieldByAlias("date_received").FieldValue
month = Format(date_recieved, "M").ToString
Year = Data.Fields.GetFieldByAlias("yearAR").FieldValue
period = Data.Fields.GetFieldByAlias("periodAR").FieldValue
If period = month Then
If Year = Year1 Then
Exit Function
Else
MessageBox.Show("Date received does not match year", "Invalid Input")
End If
Else
MessageBox.Show("Date received does not match period", "Invalid Input")
End If
Catch ex As Exception
Result.Message = ex.Message
End Try
Result.Success = True
Return Result
End Function
Format does not accept a string parameter, by you passing "M" it is trying to convert the datatype you supply to the datatype the function accepts and since a string does not implicitly cast to an integer an error occurs
To format a Date type to various formats of string you just use your date variable and its subsequent .ToString() method with your formatting rules as an argument of .ToString()
Here is a link to the msdn explaining all the possible formatting options: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

Validate the date is in correct format

I have one field which is in text format and is used for keeping release date .I have to check whether the date in field is in dd/mm/yyyy format or not.
Please suggest how to do that if the field is in string format.
I would use DateTime.TryParseExact, it will not throw an exeption but will return a bool that represents a valid conversion.
From above link:
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.
Example:
Dim yourdate As String = "31/12/1999"
Dim mynewDate As DateTime
Dim culture As New CultureInfo("") 'Uses invariant culture
If Not Date.TryParseExact(yourdate, "dd/MM/yyyy", culture, DateTimeStyles.None, mynewDate) Then
MsgBox("Oops")
Else
MsgBox(mynewDate.ToString())
End If
DateTime.ParseExact
trycatch the call since it will throw an exception if it not in the specified format.
format = "d"
dateString = "Sun 15 Jun 2008 8:30 AM -06"
Try
result = Date.ParseExact(dateString, format, provider)
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
Catch e As FormatException
Console.WriteLine("{0} is not in the correct format.", dateString)
End Try

Invalid Cast Exception. Not sure why

Can anyone see from the following function why I would be getting an "Invalid Cast Exception"? More specifically this is the error "Conversion from string "yyyyMMdd" to type 'Integer' is not valid."
I am trying to convert a DateTime value from the database to a String with the format "yyyyMMdd" so for example October 22, 1985 would be "19851022".
dbReader(fieldName).ToString("yyyyMMdd")
Here is the entire function ...
Private Function GetDBReaderDateValue(ByVal dbReader As IDataReader, ByVal fieldName As String) As String
If dbReader(fieldName) Is DBNull.Value Then
Return ""
Else
Return dbReader(fieldName).ToString("yyyyMMdd")
End If
End Function
If fieldName is not a DateTime, conversion will fail. Try to cast it to a Datetime first:
Dim dt As Date
If Date.TryParse(dbReader(fieldName).tostring, dt) Then
Return dt.ToString("yyyyMMdd")
Else
Throw New ArgumentException("GetDBReaderDateValue needs a Date-Column as parameter!")
End If
It seems like you're calling ToString on an Object... and there's no overload that takes a String parameter. You probably need to cast to a DateTime first.