-1

I'm trying to pass a java util date to my json response without succes.

If i System.out.println the dates i get "1970-01-01 01:00:00.263" correct format.

    System.out.println("from date: " + fromDate);
    System.out.println("to date: " + toDate);

    // from date: 1970-01-01 01:00:00.022
    // to date: 1970-01-01 01:00:00.263

    // in my json i get 
    // from date: 22 
    // to date: 263

if i pass my date to the json return i get " 263 " only (the last part of the timestamp) ?

how can i format the date so i get the whole date (YY-MM-DD, hour-min-sec) instead of just the last part of the timestamp ?

the model object

public class testSomething {

    boolean status;
    String msg;
    Date fromDate;
    Date toDate;

    public testSomething(boolean status, String msg, Date fromDate, Date toDate) {
    this.status = status;
    this.msg = msg;
    this.fromDate = fromDate;
    this.toDate = toDate;
    }

the return value

    Date fromDate = dates.get(0);
    Date toDate = (Date) dates.get(dates.size() - 1);

    return new testSomething(true, "msg here", fromDate, toDate);
Anders Pedersen
  • 2,255
  • 4
  • 25
  • 49

4 Answers4

0

The default conversion is to send the milliseconds since the Epoch (date.getMillis()). There are two reasons for this:

  1. Readability is not a concern for computers.
  2. This time format is independent of the time zone. That means you can send the date to another computer and it will display the same point in time.

Depending on your JSON framework, there are different ways to install data converters. Jackson has several third-party datatype modules for this purpose and you can install your own in your ObjectMapper.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

Something like this will do ...

long timestamp = fromDate.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY/MM/dd HH-mm-ss");
System.out.println(dateFormat.format(timestamp));
jonhid
  • 2,075
  • 1
  • 11
  • 18
0

fixed it by adding spring.jackson.date-format=(yyyy-MM-dd HH:mm:ss) to my application.properties file

Anders Pedersen
  • 2,255
  • 4
  • 25
  • 49
  • 1
    Note that this code will start to fail in unexpected ways when the JSON is exchanged between computers in different time zones. – Aaron Digulla Jan 25 '17 at 12:38
  • @AaronDigulla whats the proper solution then ? – Anders Pedersen Jan 25 '17 at 12:42
  • You either have to include milliseconds and time zone (`...HH:mm:ss.SSSZ`) or leave the date format alone (= transfer using milliseconds). Why do you want a special date format? – Aaron Digulla Jan 25 '17 at 12:50
  • @AaronDigulla i load a bunch of data from db, and i want to keep a record of when the sample are from (oldest and newest row gotten from the db select) (as ex: yyyy-mm-dd hh:mm:ss) – Anders Pedersen Jan 25 '17 at 12:54
  • Just to look at it? Or will a computer eventually have to process this data? – Aaron Digulla Jan 25 '17 at 12:59
  • @AaronDigulla i dont want to rule out that it's going to be processed at some point - but at the moment it's just to look at / add to log file if i add SSSZ i get (example format) " 1970-01-01, 00:00:00.284+0000 " – Anders Pedersen Jan 25 '17 at 13:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133986/discussion-between-anders-pedersen-and-aaron-digulla). – Anders Pedersen Jan 25 '17 at 13:03
  • I suggest to log in an archive format (hard to read for humans) and use a tool to convert the log data into human readable form. That way, you're not losing information (like the time zone; 9:00 am isn't the same everywhere on earth) and you can extend the tool to do more complex data mining on the go. – Aaron Digulla Jan 25 '17 at 13:04
0

In Java Spring I wanted to send the data & time to a object through a PUT which uses java.sql.Date in the object. I was using json to send it and this is the format I used

public class MyEntityClass {
    private String username;
    private Date dateTime;
}

And the Json Format I used is,

{
    "username": "selena31",
    "dateTime":"2017-01-23T12:34:56"
}

And it worked for me :)

nullPainter
  • 2,676
  • 3
  • 22
  • 42
Nohim Ys
  • 51
  • 7