如何在Java中将日期类型转换为LocalDate
我在Angular 7中有一个前端部分,在带有Spring Boot框架的Java中有一个后端部分。我想在后端发布一个日期对象。在后端,我有一个本地日期对象。我不需要LocalDateTime对象 我在英国的约会服务。我需要保留日期类型,而不是使用字符串如何在Java中将日期类型转换为LocalDate,java,json,angular,spring-boot,date,Java,Json,Angular,Spring Boot,Date,我在Angular 7中有一个前端部分,在带有Spring Boot框架的Java中有一个后端部分。我想在后端发布一个日期对象。在后端,我有一个本地日期对象。我不需要LocalDateTime对象 我在英国的约会服务。我需要保留日期类型,而不是使用字符串 addDate():可观察{ 让我们现在=新日期(); 返回此文件。http .post('/api/date',现在) .烟斗( 点击(响应=>{ 返回响应; }), catchError(error=>this.notificationSe
addDate():可观察{
让我们现在=新日期();
返回此文件。http
.post('/api/date',现在)
.烟斗(
点击(响应=>{
返回响应;
}),
catchError(error=>this.notificationService.handleError(error))
);
}
我的后端服务:
@PostMapping
public LocalDate addireregularity(@RequestBody LocalDate date,HttpServletRequest){
日志信息(日期);
返回日期;
}
我有一个错误:
2019-08-06 08:21:02.185警告1444---[nio-8080-exec-4]
.w.s.m.s.DefaultHandlerExceptionResolver:已解决
[org.springframework.http.converter.httpMessageNodeTableException:
JSON分析错误:无法反序列化类型的值
java.time.LocalDate
来自字符串“2019-08-06T00:00:00.000+0000”:
未能反序列化java.time.LocalDate:
(java.time.format.DateTimeParseException)文本
无法分析“2019-08-06T00:00:00.000+0000”,未分析文本
在索引23处找到;嵌套异常为
com.fasterxml.jackson.databind.exc.InvalidFormatException:无法
从字符串反序列化类型java.time.LocalDate
的值
“2019-08-06T00:00:00.000+0000”:反序列化失败
java.time.LocalDate:(java.time.format.DateTimeParseException)文本
无法分析“2019-08-06T00:00:00.000+0000”,未分析文本
在索引23中找到
如何将LocalDate和LocalDateTime从Angular发送到Spring 更好的解决方案: 应用程序模块.ts
import {DatePipe} from '@angular/common';
.
.
.
providers: [DatePipe]
import { DatePipe } from '@angular/common';
constructor( private datePipe: DatePipe) {}
public sendStuff(): Observable<any>{
let params = new HttpParams().set("date", this.datePipe.transform(new Date(),"yyyy-MM-dd"))
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}
public sendStuff(): Observable<any>{
let params = new HttpParams().set("datetime",new Date().toLocaleString())
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}
app.service.ts
import {DatePipe} from '@angular/common';
.
.
.
providers: [DatePipe]
import { DatePipe } from '@angular/common';
constructor( private datePipe: DatePipe) {}
public sendStuff(): Observable<any>{
let params = new HttpParams().set("date", this.datePipe.transform(new Date(),"yyyy-MM-dd"))
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}
public sendStuff(): Observable<any>{
let params = new HttpParams().set("datetime",new Date().toLocaleString())
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}
从'@angular/common'导入{DatePipe};
构造函数(私有datePipe:datePipe){}
public sendStuff():可观察{
设params=new-HttpParams().set(“date”,this.datePipe.transform(new-date(),“yyyy-MM-dd”))
.set(“datetime”,new Date().toISOString());
返回此.http.post(“urlPath../values/show”,“Body”{
标题:新的HttpHeaders({
“接受”:“应用程序/json”
}),
params
});
}
弹簧部分
@PostMapping(path="values/show")
public Map<String, Object> showValues(@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
@RequestParam("datetime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime datetime) {) {
...
}
@PostMapping(path=“values/show”)
公共地图显示值(@RequestParam(“date”)@DateTimeFormat(iso=DateTimeFormat.iso.date)LocalDate日期,
@RequestParam(“datetime”)@DateTimeFormat(iso=DateTimeFormat.iso.DATE\u TIME)LocalDateTime datetime{){
...
}
如果你只想转换LocalDate,你可以不使用datePipe,只需从Angular发送新的Date().toISOString()。但由于提问者特别要求LocalDate,你需要某种格式来获取此类日期->“yyyy-MM-dd”。只有今年春天才能发挥其神奇作用
顺便说一句,如果您只需要DateTimeFormat.ISO.time的时间(HH:mm:ss.SSSXXX),也可以这样做
完成!
不是很好的解决方案,更不舒服->转换为字符串
(未测试,因为我对Spring解决方案很满意)
app.service.ts
import {DatePipe} from '@angular/common';
.
.
.
providers: [DatePipe]
import { DatePipe } from '@angular/common';
constructor( private datePipe: DatePipe) {}
public sendStuff(): Observable<any>{
let params = new HttpParams().set("date", this.datePipe.transform(new Date(),"yyyy-MM-dd"))
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}
public sendStuff(): Observable<any>{
let params = new HttpParams().set("datetime",new Date().toLocaleString())
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}
public sendStuff():可观察{
设params=new-HttpParams().set(“datetime”,new-Date().toLocaleString())
.set(“datetime”,new Date().toISOString());
返回此.http.post(“urlPath../values/show”,“Body”{
标题:新的HttpHeaders({
“接受”:“应用程序/json”
}),
params
});
}
春天:
@PostMapping(path="values/show")
public Map<String, Object> showValues( @RequestParam String date, @RequestParam String datetime) {
...
// code to transform String to Date copied somewhere
//Create a DateTimeFormatter with your required format:
DateTimeFormatter dateTimeFormat = new DateTimeFormatter(DateTimeFormatter.BASIC_ISO_DATE);
//Next parse the date from the @RequestParam, specifying the TO type as a TemporalQuery:
LocalDateTime date = dateTimeFormat.parse(datetime, LocalDateTime::from);
// some more code to transform LocalDate...
}
@PostMapping(path=“values/show”)
公共映射显示值(@RequestParam String date,@RequestParam String datetime){
...
//将字符串转换为某处复制的日期的代码
//使用所需格式创建DateTimeFormatter:
DateTimeFormatter DateTimeFormatt=新的DateTimeFormatter(DateTimeFormatter.BASIC_ISO_DATE);
//接下来分析@RequestParam中的日期,将TO类型指定为临时查询:
LocalDateTime日期=dateTimeFormat.parse(datetime,LocalDateTime::from);
//更多用于转换LocalDate的代码。。。
}
如何将LocalDate和LocalDateTime从Angular发送到Spring
更好的解决方案:
应用程序模块.ts
import {DatePipe} from '@angular/common';
.
.
.
providers: [DatePipe]
import { DatePipe } from '@angular/common';
constructor( private datePipe: DatePipe) {}
public sendStuff(): Observable<any>{
let params = new HttpParams().set("date", this.datePipe.transform(new Date(),"yyyy-MM-dd"))
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}
public sendStuff(): Observable<any>{
let params = new HttpParams().set("datetime",new Date().toLocaleString())
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}
app.service.ts
import {DatePipe} from '@angular/common';
.
.
.
providers: [DatePipe]
import { DatePipe } from '@angular/common';
constructor( private datePipe: DatePipe) {}
public sendStuff(): Observable<any>{
let params = new HttpParams().set("date", this.datePipe.transform(new Date(),"yyyy-MM-dd"))
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}
public sendStuff(): Observable<any>{
let params = new HttpParams().set("datetime",new Date().toLocaleString())
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}
从'@angular/common'导入{DatePipe};
构造函数(私有datePipe:datePipe){}
public sendStuff():可观察{
设params=new-HttpParams().set(“date”,this.datePipe.transform(new-date(),“yyyy-MM-dd”))
.set(“datetime”,new Date().toISOString());
返回此.http.post(“urlPath../values/show”,“Body”{
标题:新的HttpHeaders({
“接受”:“应用程序/json”
}),
params
});
}
弹簧部分
@PostMapping(path="values/show")
public Map<String, Object> showValues(@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
@RequestParam("datetime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime datetime) {) {
...
}
@PostMapping(path=“values/show”)
公共地图显示值(@RequestParam(“date”)@DateTimeFormat(iso=DateTimeFormat.iso.date)LocalDate日期,
@RequestParam(“datetime”)@DateTimeFormat(iso=DateTimeFormat.iso.DATE\u TIME)LocalDateTime datetime{){
...
}
如果你只想转换LocalDate,你可以不使用datePipe,只需从Angular发送新的Date().toISOString()。但由于提问者特别要求LocalDate,你需要某种格式来获取此类日期->“yyyy-MM-dd”。只有今年春天才能发挥其神奇作用
顺便说一句,如果您只需要DateTimeFormat.ISO.time的时间(HH:mm:ss.SSSXXX),也可以这样做
完成!
不是很好的解决方案,更不舒服->转换为字符串
(未测试,因为我对Spring解决方案很满意)
app.service.ts
import {DatePipe} from '@angular/common';
.
.
.
providers: [DatePipe]
import { DatePipe } from '@angular/common';
constructor( private datePipe: DatePipe) {}
public sendStuff(): Observable<any>{
let params = new HttpParams().set("date", this.datePipe.transform(new Date(),"yyyy-MM-dd"))
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}
public sendStuff(): Observable<any>{
let params = new HttpParams().set("datetime",new Date().toLocaleString())
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}
public sendStuff():可观察{
设params=new-HttpParams().set(“datetime”,new-Date().toLocaleString())
.set(“datetime”,new Date().toISOString());
返回此.http.post(“urlPath../values/show”,“Body”{
他