Timestamp getting as long instead of date format - java

I have a one rest method from Spring3 which returns expected JSON response except date which is in timestamp.
In database date is stored as 2013-08-08 00:30:00. Before sending response to the application, the date I am getting as 2013-08-08 00:30:00. I have checked it in debug mode. But after complete execution of rest-service, I got the date in long format as 1375902000000.
I want to return same timestamp format instead of long format.
I don't want to convert long timestamp to again date format at client side.

By default it is serialized as long if you want to send it like 2013-08-08 00:30:00 you may try asString()

You could do the following to keep your desire date format:
Declare the bean property as String.
Format the date object according to your desire format, example yyyy-dd-MM hh:mm:ss

Related

Validate Date in the request body

I have a request body have date field and I want to validate the input of the date and return error code 400 BadRequest if the format is wrong.
If I use the JsonFormat then it throws error 500:
#JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
private Date dateAt;
So I changed the field to String and then parse and throw BadRequest format everywhere using the getDate()
I am not really satisfied with this approach. Is there any other elegant way to achieve this validation?
I think I will accept error 500 from JSON formatter and skip my custom exception.
Since Java 8, you should avoid using Date. Java 8 brought an entirely new date and time API.
According to your question, a suitable replacement for Date would be LocalDate, which is expected to be in the format yyyy-MM-dd.
Jackson has a module supporting the new date and time API and an exception will be thrown if the date is not in the correct format. When serializing, you want to make sure the WRITE_DATES_AS_TIMESTAMPS feature is disabled, so the values will be written according to the ISO 8601 format.

How to convert date in string with or without time into DATE

I have a date in string format like "2017-11-16" or "2017-11-16 12:59:11.243". I have to convert it to Date.
Below is my code:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
Date createdOn = formatter.parse(date);
It works fine when the date is "2017-11-16 12:59:11.243". But not able to format with "2017-11-16" date.
String date is coming from URL, so it could be either with time or without time. How can I convert it into Date in spite of the fact what type of date is coming from URL?
Actually, I have to find row from the database based on the date. In the database, the date is storing in the format like 2017-11-16 12:59:11.243. But If from URL 2017-11-16 is coming then it should find according to this date.
You can modify the pars based on the presence of time,
if(dateOrDataTime.contains(":")){
//Use parser with date-time. & fetch with equal
}else{
//Use parser for date only. & fetch using date(date_time)
}

how to send date through URL in ajax

Hi i have to append Date to a URL, maintaining its Datatype as Date and not as String
When in append Date to URL as "?date=" + $(target).val();
i fetch it through #RequestParameter in my Controller
But the problem here lies that i cant access it as Date but as String.
If i access it as Date it does not gives an error but changes the format of the Date from 2017-05-12 00:00:00 to something like Fri MAY 12 00:00:00 IST 2017.
So i need to send data as date through AJAX so when i access in my Controller i can maintain its format
As far as i know when i fetch the data as Date it gives me internal representation of Date because there is internal conversion of String to Date
Thus i need to know how i can send date through URL in AJAX
You can't pass types via GET URL parameters.
You need to convert the type on your java code.
For example:
new SimpleDateFormat( "MM/dd/yyyy H:m:s" ).parse( #RequestParam( "date" ) )
It's all in the javadoc for SimpleDateFormat

Jersey JSON Date formatting POJO Mapping

I have date stored in mysql db as DATETIME and the value is stored in db as
"2016-09-09 15:56:26"
I wanted to display in same format, I am using Jersey 1.19 with POJOMapping and I used Date format in pojo, but the value shown in API is as:
"createdDt":1473454586000
What is the right approach to display correct date format in GET API, should I use String?

Talend timestamp - Getting a zero value instead of timestamp

I am converting string to date using a tConvertType component in Talend.The data(source is in String) is getting loaded when I do the date pattern yyyy-mm-dd but when I try with timestamp yyyy-MM-dd HH:mm:ss then I am getting an error. The data from the source has the timestamp but then I am getting an error.
For Eg : I have the source as 2015-09-03 14:14:90 , since data is in string so used tconverttype and then for destination the data type is date. But if I use timestamp then I am getting an error of Unparseable date and if I change to yyyy-mm-dd then the data is coming as 2015-09-03 00:00:00 which is wrong
try
convert(timestamp,expression)
i had the same problem in the past and convert fixed that
Using the type date and date format "yyyy-MM-dd' 'HH:mm:ss" it works fine
I recommand you to use date format by pressing Ctrl+space and choose from the list.

Categories