First elaborate on the differences between the common types of time.
Date format: year, month, day, hour, minute and second
The date format is: month and year
Date format: hours, minutes and seconds
.TimestampDate format: year, month, day, hour, minute, second, nanosecond (milliseconds)
The front-end passes time-type parameters to the back-end, there are generally two means of passing parameters, GET and POST.
When GET passes a parameter, the front-end passes in a string string, and the back-end uses thestring class (computing)After receiving the type connection, you need to do the relevant processing. The processing code is as follows.
String str="2021-5-21"; //assume str is the time type parameter passed in the previous paragraph
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parse = simpleDateFormat.parse(str);
String format = simpleDateFormat.format(parse);
System.out.println(parse);
System.out.println(format);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
When Post passes a parameter, the front-end passes over an object, the time parameter is just an attribute of the object, the object case is as follows.
public class Student {
public String getName() {
return name;
}
public Date getDate() {
return date;
}
private String name;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") // Add this annotation above the time attribute
private Date date;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
Backend Receive Request Code
@RestController
@RequestMapping("/test")
public class TestTime {
@GetMapping("/time1")
public void service1(String time){
System.out.println(time);
}
@PostMapping("/time2")
public void service2(@RequestBody Student student){
System.out.println(student);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Simulates a browser request:
### GET request
GET{{baseUrl}}//test/time2?time=2021-5-8
### POST request
POST{{baseUrl}}//test/time2
Content-Type: application/json
{
"name": "Gu.",
"date": "2020-05-08 17:08:10"
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
Note: baseurl is my ip address, there are many tools to simulate the request, the above is just a tool, readers can use thepostmanetc. tools to simulate the request.
To summarize: get requests are strings that need to be processed
post request is passed to the object, through the @requestbody, and in the field add jsonformat, will be automatically resolved to the date type, do not need to do additional processing.