如何将时间戳值设置为GET服务的@RequestParam变量?

huangapple go评论78阅读模式
英文:

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.

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?

英文:

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(&quot;/getNumberOfHolidays&quot;)
public ResponseEntity&lt;Properties&gt; getNumberOfHolidays(@RequestParam Integer locationId, 
		@RequestParam(&quot;startDate&quot;) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date startDate,
		@RequestParam(&quot;endDate&quot;) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endDate){
	Integer noOfDays = 0;
	Properties prop = new Properties();
	try {
		noOfDays = service.getNumberOfHolidays(locationId, startDate, endDate);
		prop.setProperty(&quot;Number of Holidays&quot;, 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&amp;startDate=2020-08-01&amp;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&amp;startDate=1596220200000&amp;endDate=1596997800000

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

       @RequestParam(&quot;startDate&quot;) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
	   @RequestParam(&quot;endDate&quot;) @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?

答案1

得分: 2

1596220200000 不是一个日期,它是一个数字。

在这种情况下,它是一个可以解释为自纪元以来的毫秒数的数字,但它只是一个数字。要转换为日期,您需要执行以下操作。

public ResponseEntity&lt;Properties&gt; getNumberOfHolidays(@RequestParam Integer locationId, 
        @RequestParam(&quot;startDate&quot;) long startMilli,
        @RequestParam(&quot;endDate&quot;) long endMilli) {
    Instant startInstant = Instant.ofEpochMilli(startMilli);
    Instant endInstant = Instant.ofEpochMilli(endMilli);

在问题中有两个示例:

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

然而,15962202000002020-07-31T18:30:00Z15969978000002020-08-09T18:30:00Z

假设它的目的是生成与第一个示例相同的日期值,那么日期必须调整为印度时区。

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

这将产生值 2020-08-01T00:00+05:30[Asia/Kolkata]2020-08-10T00:00+05:30[Asia/Kolkata]

然后,您可以调用 toLocalDate() 来获得 2020-08-012020-08-10

英文:

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&lt;Properties&gt; getNumberOfHolidays(@RequestParam Integer locationId, 
        @RequestParam(&quot;startDate&quot;) long startMilli,
        @RequestParam(&quot;endDate&quot;) 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(&quot;Asia/Kolkata&quot;);
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.

答案2

得分: 0

当您指定:

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

您正在告诉解析器传入的数据应该是什么样子,这就是为什么您的第一个情况起作用的原因。在第二种情况中,您只提供了一个数字(可能是自纪元以来的毫秒数),这不是正确的 ISO 日期格式。如果您想从已设置的 LocalDateTime(您的第一个情况)中获取毫秒数,可以使用以下代码:

LocalDateTime ldt = // 这已经从请求参数中解析出来
ZonedDateTime zdt = ldt.atZone(ZoneId.of("America/Los_Angeles")); // 指定您的时区
long millis = zdt.toInstant().toEpochMilli();

如果您想直接将时间戳以毫秒或任何数字格式发送到您的 API,只需使用 @RequestParam("startDate") Long startdate。您也可以将自纪元以来的毫秒数转换为 LocalDateTime,但在线上有很多资源可以告诉您如何做到这一点。

英文:

When you specify:

@RequestParam(&quot;startDate&quot;) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
@RequestParam(&quot;endDate&quot;) @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(&quot;America/Los_Angeles&quot;)); // 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(&quot;startDate&quot;) Long startdate. You can convert ms since epoch to LocalDateTime as well but there are lot of resources how to do it online.

huangapple
  • 本文由 发表于 2020年7月22日 23:24:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63037752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定