Date format handling in Spring MVC - spring

HI I have a problem in my current Spring +JPA project. My entity object and bean object for web page are same.
From web page using jquery i am reading date in (dd-mon-yyyy) format from screen and saving it to database. The field is of Date type in my bean class.
During update i am fetching values from database and displaying the same in web page. But this time the date format has chnaged to different fromat(yyyy/mm/dd) on screen.
So while saving again i am getting error, as the date format has been changed and i am unable to parse the value received form screen.
So is there any proper way to handle this situation.

I assume that you are storing the dates in database as a DATE or DATETIME type. If so, the value should be a Date object in Java. In order to output the date value as dd-mon-yyyy, you will need to use SimpleDateFormat to convert the date object into a string representation in your desired format:
new SimpleDateFormat("dd-MMM-yyyy").format(date);
An additional note: for an internal-only API, it doesn't matter what format you choose as long as you are consistent everywhere. But if you are creating something that could one day be exposed publicly, I would suggest that you send/receive date and time value in the ISO8601 format: yyyy-MM-ddTHH:mm:ssZ. This format is understood by everyone everywhere.
Update:
Based on your comment, I would say add a new method to your bean class:
private DateFormat dateformat = new SimpleDateFormat("dd-MMM-yyyy");
public String getFromDateString() {
return dateformat.format(fromDate);
}
And then in your JSP, call ${bean.fromDateString}.

Related

Why Date field is coming with Character T in JPA

Hi I am trying to retrieve and Save Date from Oracle Table using JPA . The column type is DATE in Oracle.
In JPA entity i configured as
#Column(name = "NOTE_DATE")
private Date noteDate;
In Json why i am getting the date with a T character . Eventhough its not saving that (T)character ins DB why its asking for date as
Successful Save
"noteDate": "2021-11-01T20:00:05"
Failure Save
"noteDate": "2021-11-01 20:00:05"
The 2021-11-01T20:00:05 date representation is generated by your JSON serializer which converts your Date to String and not by JPA. By default, Spring uses Jackson as JSON serializer which converts Date to its ISO 8601 representation.
For your error when you try to save your object using
"noteDate": "2021-11-01 20:00:05"
For me, it is a deserialization problem: Jackson can not convert your JSON to your Java object.

Can't remove hours and minutes from datatable

I'm trying to get release date field with y-m-d format.. Actually time format (for y-m-d) seems fine but also it gives h:m:s too, I changed time format at server side and removed h:m:s but datatable still shows them
What I get
2012-04-11 00:00:00
What I want
2012-04-11
Released_at field (Metronic theme - json datatable)
{
field: "released_at",
title: "Release Date",
type: "date",
format: "YYYY/MM/DD"
}
time format (I'm using laravel framework)
protected $dateFormat = "Y-m-d";
How do I fix this ?
Datatable accept column type same as mysql type.
You are trying to convert mysql dateTime column to Date in datatable, which is not possible from datatable.
In your code you have declared
format: "YYYY/MM/DD" // but actual type is dateTime as per mysql.So it will append time next to it.
If you are storing only date in mysql then you can change column type to Date, and your problem gets solved.
And if you want to convert string to date in laravel you can follow this post.
Your database column is of type dateTime which, by definition, includes both date and time information. If you want to remove the time at a database level then use the date type instead
$table->date("released_at")->nullable();
If instead of removing the time from the database, you just want to ignore it for certain parts of your application, you can leverage the fact than in Laravel all dates coming from your models are Carbon\Carbon instances, so you could do
$model->releasedAt->format('y-m-d'); // Returns '18-09-11'

Date timezone getting changed when using spring boot default jackson mapping

I have spring boot rest service which call another service xyz and receive date in format yyyy-MM-ddXXX from json. But time Zone of of date is getting changed in my service response. Suppose I am getting date in JSON from service xyz as "date": "2018-08-27-07:00" but my service response is returning date : "2018-08-27-04:00". Offset getting changed. Date field in my POJO is . I want to use the same offset I am getting from backend service and it can be any offset.
#JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-ddXXX")
private Calendar date;
The problem is that Calendar (and Date) use implicit time conversions to adjust it to your time zone. And almost always it is something that not expected.
To avoid this use java.time classes (such as OffsetDateTime or ZonedDateTime, or even LocalDateTime if you do not need to work wit time zones).
And small offtopic advice: try to use time format aligned to ISO8601 standard (like 2018-08-24T22:30:00)

ngx-mydatepicker is formatted as jsdate and doesn't format back to normal date angular 4

I am using ngx-mydatepicker in my angular 4 application with reactive forms. I am binding date field to input field from rest api which is in the format "OrderDate": "2018-07-03T15:58:20.183". This doesn't populate date in input field directly so we need to convert it to jsdate as suggested in this post githubissue as in the below code snippet
'OrderDate': [{jsdate: new Date(data.OrderDate)}, Validators.required],
Now the date appears in the input field as expected. Now the actual problem is while submitting the form the date is coming in this format as in the below screen shot which is not accepted by rest api. So I need to convert it back to the api accepted format, how do I do it and my form contains more than 40 fields where am posting the form directly to api, so if I need to format the date I should explicitly again edit the form data before sending it to api. Is there a way that the date will format directly without explicit formatting. If there is no way then how to format back to normal date format.
Thanks in advance
Try the format using toISOString,
new Date(field).toISOString()
Reference

400 - Bad Request while saving Date Only field in CRM using Web API

I am using Microsoft Dynamics 365 Web API approach to interacting with CRM data.
Here I want to update the field with type: DATE and TIME.
Date value I am passing in the request body is as below:
"packfirstday":"1-15-2018"
Except for above, I have also tried with DateTime and with different date formats.
e.g.
mm-dd-yyyy
m-dd-yyyy
mm/dd/yyyy
yyyy/mm/dd
yyyy-mm-dd
PS: I try to post without date field it is saving details successfully.
The problem is not with the code, the simple misunderstanding.
There are 2 components namely Behavior & Format. You have Format set as 'Date only' not the Behavior. Behavior decides the Database terms whereas Format is used for displaying datepicker controls in Form.
So when you set the field in web api with only date part - the CRM database expects time part also.
Either set behavior also as date only, so this will work:
"packfirstday":"2018-01-15" //YYYY-mm-dd format
Or change your code to pass time part also:
"packfirstday":"2018-01-15T11:10:00.000Z" //UTC offset
Since user local behavior still expects the time part.

Resources