Tech_sharma
Tech_sharma

Reputation: 117

How to set timestamp value to a @RequestParam variable of get service?

I have a @GetMapping mapped controller method with 3 Request Parameters: id, startDate, and endDate.

I want it to accept timestamps for both date parameters, but only get it working using ISO formatted strings.

My Method looks like below:

@GetMapping("/getNumberOfHolidays")
public ResponseEntity<Properties> getNumberOfHolidays(@RequestParam Integer locationId, 
        @RequestParam("startDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date startDate,
        @RequestParam("endDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endDate){
    Integer noOfDays = 0;
    Properties prop = new Properties();
    try {
        noOfDays = service.getNumberOfHolidays(locationId, startDate, endDate);
        prop.setProperty("Number of Holidays", noOfDays.toString());
    } catch(Exception e) {
        //return
    }
     //return
}

When I invoke this method with startDate = 2020-08-01 and endDate = 2020-08-10 (both in YYYY-mm-DD), it's working as expected and properly converts the strings from the url.

Example:

   http://localhost:8080/TrackContract/getNumberOfHolidays?locationId=2&startDate=2020-08-01&endDate=2020-08-10

But when I call the method with timestamps like startDate = 1596220200000 and endDate = 1596997800000 it's not working(giving 400 Bad Request in postman)

Example:

   http://localhost:8080/TrackContract/getNumberOfHolidays?locationId=2&startDate=1596220200000&endDate=1596997800000

I tried to set the timestamp value to request param like below:

       @RequestParam("startDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
       @RequestParam("endDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate

But that didn't work. Can someone help me here how can I set timestamp value to the RequestParam startDate and endDate?

Upvotes: 1

Views: 4093

Answers (2)

Andreas
Andreas

Reputation: 159270

1596220200000 is not a date, it's a number.

In this case, it's a number that can be interpreted as the number of milliseconds since epoch, but it is just a number. To convert to a date, you have to do it.

public ResponseEntity<Properties> getNumberOfHolidays(@RequestParam Integer locationId, 
        @RequestParam("startDate") long startMilli,
        @RequestParam("endDate") long endMilli) {
    Instant startInstant = Instant.ofEpochMilli(startMilli);
    Instant endInstant = Instant.ofEpochMilli(endMilli);

In the question, there are two examples:

startDate = 2020-08-01 and endDate = 2020-08-10
startDate = 1596220200000 and endDate = 1596997800000

However, 1596220200000 is 2020-07-31T18:30:00Z
and 1596997800000 is 2020-08-09T18:30:00Z

Assuming it was meant to produce the same date values are the first example, the dates must be adjusted to the India time zone.

ZoneId zone = ZoneId.of("Asia/Kolkata");
ZonedDateTime startDateTime = startInstant.atZone(zone);
ZonedDateTime endDateTime = endInstant.atZone(zone);

That will produce values 2020-08-01T00:00+05:30[Asia/Kolkata] and 2020-08-10T00:00+05:30[Asia/Kolkata].

You can then call toLocalDate() to get 2020-08-01 and 2020-08-10.

Upvotes: 2

Norbert Dopjera
Norbert Dopjera

Reputation: 751

When you specify:

@RequestParam("startDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
@RequestParam("endDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate

You are telling to parser how incoming data should look like, that's why your first case is working. In you second case you only provide number (probably ms since epoch) and that is not correct ISO date format. If you want to obtain ms. from already set LocalDateTime (your first case) you can use following code:

LocalDateTime ldt = // this is already parsed from request param
ZonedDateTime zdt = ldt.atZone(ZoneId.of("America/Los_Angeles")); // specify your time zone
long millis = zdt.toInstant().toEpochMilli();

If you want to send timestamp's in ms or any number format directly to your API then just use @RequestParam("startDate") Long startdate. You can convert ms since epoch to LocalDateTime as well but there are lot of resources how to do it online.

Upvotes: 0

Related Questions