Prasad Parab
Prasad Parab

Reputation: 496

Date value mismatch between REST Server and Postman

I am developing a REST server using spring framework.

I am returning a list of Model object(Employee which containing a Date field

So my Model class looks like:

Class Employee {
 private String empId;
 private Date joinDate;
}

My controller method looks like:

@GetMapping("/Employee")
    public List<EmployeeLeaves> searchEmployeeLeaveDetails(@RequestParam(value = "filter", required = false) String filter) throws Exception {

        List<Employee> empList = service.searchEmployee(filter);

        return empList;
    }

When I tried to call this api from postman in response it always shows joinDate is 1 day less than actual date.

I debugged this issue and found that in controller the date is value is correct

Below are the test/debug results

Value for joinDate(Column type is Date) in Database: '2018-10-27'
Value for joinDate(java.sql.timestamp) in Controller: 2018-10-27 00:00:00.0
Value retried in postman's JSON response : "2018-10-26T18:30:00.000+0000"

Anyone have idea why postman response shows date as 26 instead of 27? Any timezone related factor came in between?

Upvotes: 0

Views: 1576

Answers (1)

cassiomolin
cassiomolin

Reputation: 130947

You should avoid using Date, as it's a legacy class now. Java 8 introduced a new API for dates, times, instants and durations based on the ISO calendar system.

The closest equivalent to Date is Instant which represents a timestamp, a moment on the timeline in UTC. But you probably want to use OffsetDateTime instead, as it stores all date and time fields as well as the offset from UTC/Greenwich.

Quoting the OffsetDateTime class documentation:

It is intended that ZonedDateTime or Instant is used to model data in simpler applications. This class [OffsetDateTime] may be used when modeling date-time concepts in more detail, or when communicating to a database or in a network protocol.

Upvotes: 1

Related Questions