#JsonFormat Date field to localtimezone - spring

I have a field called expiredDate. The value of this field in the DB is: 2019-11-06 22:48:04. I am using SpringBoot and I have defined the field in POJO as:
#JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MMM-dd HH:mm:ss")
#JsonProperty("expiration_date")
Date expirationDate;
When I retrieve the date I get the following: 2019-Nov-07 04:48:04. My requirement is to get it in the following format: 2019-11-06T22:48:04.000Z.
I have given the timezone='CST' but I dont want to mention the timezone. I want my program to identify the timezone and show the result.
If I dont give the #JsonFormat annotation, I get the result in long.
What am I missing here?

Related

Select Datetime as Date in criteria query

I'm using JPA CriteriaBuilder to select attributes from the database. I have a specific use case that involves Conversion of datetime to date. The Projection segment of the code is as follows.
CriteriaQuery<ResponseDTO> query = cb.createQuery(ResponseDTO.class);
query.multiselect(root.get(MasterPackagesV1_.masterPackageName),
packageJoin.get(PackagesV1_.packageId),
packageJoin.get(PackagesV1_.packageName),
packageJoin.get(PackagesV1_.startSellingDate),
packageJoin.get(PackagesV1_.endSellingDate),
packageJoin.get(PackagesV1_.createdDate),
packageJoin.get(PackagesV1_.modifiedDate),
packagePaymentJoin.get(PackagePaymentPlanV1_.mrp),
packageSubscriptionJoin.get(PackageSubscriptionsV1_.packageSubscriptionId)
);
All of the date attributes are stored as datetime datatype in the MySQL database. I'm want the datetime to be formatted to date. i.e Instead of retrieving the Dates as LocalDateTime I want it to be converted to LocalDate during Instantiation.
I have made changes in the ResponseDTO class accordingly to support LocalDate. Thanks in Advance.

Why can't I bind dates in query string with GET parameters?

I am using ASP.NET Core 3.1. I am trying to bind dates in a query string to a PageModel using GET. Here is my query string:
?id=15+startDate=1900-01-01+endDate=1900-01-01
Here is the signature of my OnGet:
public void OnGet(int id, DateTime startDate, DateTime endDate)
The values are always 0, 1/1/0001 00:00:00, 1/1/0001 00:00:00 respectively.
If I remove the dates and just pass the id, it works fine. Why do the dates in the query string break everything including the id? I have also tried changing the data types from DateTime to string and it still breaks with the string parameters always being null.
The separator for parameters in a query string is &, not + as your example has. Corrected:
?id=15&startDate=1900-01-01&endDate=1900-01-01
An arguably better approach with datetime parameters is to accept them as strings, then use DateTime.TryParse to parse with control over expected format. That way you'll always have the opportunity to inspect the string value provided and decide if you want to convert it to a DateTime, and how.

How can you make LocalDate to return Date in a specific format?

How can you make LocalDate to return Date in a specific format ?
Ex . LocalDate ld = LocalDate.now();
The above statement will return date like 2018-11-24 but i want it to return like 24-11-2018 .
** Dont say use formatter because formatter will not return date , it will return String which is of no use for me .
Your question is contradictory, for example you want "24-11-2018" but also want "date" instead of "String" and overlook the fact that "24-11-2018" is a date in string form. A date will never have only one accepted format. So yes, for representing a date as string, users should apply any customized formatter.
But I suspect that you want to change the behaviour of the LocalDate-method toString() which produces a string in the ISO-format "2018-11-24". Well, you cannot because the class LocalDate is final so overriding this method is impossible, and there is also no configuration hook to change the behaviour because this would be in conflict with the immutability of the class.

Extract only date part in a datetimein LINQ

We have some data in a DataTable and we are using the query like this to get what we need.
IEnumerable<Results> subResult = from query in datatable.AsEnumerable()
select new Results
{
Name = query.Field<string>("Name"),
Date = query.Field<DateTime?>("Date")
}
This above query returns what i need but date in full format (Ex: m/dd/yyyy hh:min:sec am/pm) but we need only date part of it(only mm/dd/yyyy need to be pulled in). When looked in the properties of this, couldn't find an implicit way to get it, please help me in getting this result. Thanks.
The DateTime class has all the properties you just mentioned. You should try to fix your display with the ToString("anyformatyouwant") formats.
What about this?
Date = query.Field<DateTime?>("Date").Date
This will give you just the date part:
IEnumerable<Results> sub-result= from query in datatable.AsEnumerable()
where new Results
{
Name = query.Field<string>("Name"),
Date = query.Field<DateTime?>("Date").Date
}
However if you just want to display the date somewhere (meaning you're not using this to do date grouping, etc. then I would just specify the display format in your UI layer.
AFAIK there is no Date Class in .net, if you want pure date you should write a Class for that, otherwise you can pull out the part as a string
DateTime.Now.Date.ToString("yyyy/MM/dd")
gives you the current date in string type, but if you get the .Date part it gives you a DateTime object which still has a time part showing AM 12:00:00:0 blah blah

Query formatted value using LINQ

I have to query a Date column with formatted values. Consider a scenario, same date with different timing "05/12/2012 02:00:00" and "05/12/2012 12:00:00". I have to query this collection such that resultant should have only date and not time "05/12/2012"(dd/MM/yyyy).
datasource.AsQueryable().Select("Date").Cast<object>().Distinct().ToList<object>();
I have used the above to get collection and i am not able to get the formatted collection.
Thanks.
Why not just juse the Date property of DateTime?
datasource.Select(d => d.Date.Date).Distinct()
I can't figure out what you're trying to do with the Cast.

Resources