-1

I am writing an application in which I have a Rest-API which is getAll() method and I take date parameter also in this. But when I hit a GET request in POSTMAN, I see date coming as 1519561301000 instead of '2018-02-25 20:10:29' . Actual date is there in Mysql DB.

Is it a known thing or I am going in wrong direction?

In Response creationdate is like this:

"creationtimestamp": 1519569629000,

Any help would be appreciated..

I am using SPRING_BOOT MysqlDB and POSTMAN to check the responses.

Deepak Jain
  • 305
  • 1
  • 3
  • 19
  • Search Stack Overflow before posting. This has been addressed multiple time already. – Basil Bourque Feb 25 '18 at 16:50
  • More duplicates/similar: [this](https://stackoverflow.com/q/10865455/642706) & [this](https://stackoverflow.com/q/38567128/642706) & [this](https://stackoverflow.com/q/18121082/642706) & [this](https://stackoverflow.com/q/46298350/642706) & [this](https://stackoverflow.com/q/14316375/642706) and many others. – Basil Bourque Feb 25 '18 at 20:37
  • This is not same question. This is related to some json problem – Deepak Jain Feb 28 '18 at 10:20

3 Answers3

1

What you're getting is likely (as the response property's name implies this) the Unix timestamp version of the date. This is defined as the number of seconds since the Epoch (1970-01-01 00:00:00 UTC). https://en.m.wikipedia.org/wiki/Unix_time

You should be able to easily convert this to any time format and timezone of your preference with Java's date related APIs.

TwoD
  • 305
  • 2
  • 12
  • Hi If I print the timestamp in spring boot I am getting the same timestamp as saved in DB but when I see the response in Postman It gives me in Epoch standard. Getting response in postman is same what I display on the UI from my angular 2 code. Hence On my UI (which is made from angular2) displays the time stamp in Epoch standard. Thats where I am failing to get the real timestamp – Deepak Jain Feb 25 '18 at 15:50
  • I will give a try to add some code for conversion in the angular side – Deepak Jain Feb 25 '18 at 15:53
1

I think this solve your issue

import java.sql.Timestamp;    
import java.util.Date;    
import java.text.SimpleDateFormat;
public class TimestampToDateExample1 {    
       public static void main(String args[]){
            Timestamp ts = new Timestamp(Long.parseLong("1519569629000"));  
            Date date = new Date(ts.getTime()); 
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            String strDate = formatter.format(date);  
            System.out.println(strDate);                          
        }    
}

if you need to format date in javascript

let d = new Date(1519569629000 * 1000);
   let v =  ('0' + d.getDate()).slice(-2) + '-' + ('0' + (d.getMonth() + 1)).slice(-2) + '-' + d.getFullYear() + ' ' + d.getHours() + ':' + d.getMinutes();
console.log(v);

may be its help you

  • Hi If I print the timestamp in spring boot I am getting the same timestamp as saved in DB but when I see the response in Postman It gives me in Epoch standard. Getting response in postman is same what I display on the UI from my angular 2 code. Hence On my UI (which is made from angular2) displays the time stamp in Epoch standard. Thats where I am failing to get the real timestamp. – Deepak Jain Feb 25 '18 at 15:50
  • I will give a try to add some code for conversion in the angular side. – Deepak Jain Feb 25 '18 at 15:53
  • please find the updated answer may help you – Sooraj Dev Varrier Feb 25 '18 at 16:43
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 25 '18 at 20:30
  • I was able to solve by adding @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "IST") annotation. – Deepak Jain Feb 28 '18 at 10:18
1

You can use "JsonFormat" in your return object, try this:

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date creationtimestamp;

See more about date format in:

Docs Oracle - SimpleDateFormat

And Read more about "JsonFormat":

Jackson Annotations - JsonFormat

Bruno Carletti
  • 273
  • 1
  • 3
  • 18
  • I went through the Jackson Annotations - JsonFormat page, It was indeed helpful. I was able to resolve the issue. – Deepak Jain Feb 25 '18 at 16:16