首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Java中将日期类型角转换为LocalDate

如何在Java中将日期类型角转换为LocalDate
EN

Stack Overflow用户
提问于 2019-08-06 08:22:45
回答 1查看 6.2K关注 0票数 1

我在角7中有一个前端部分,在Java中有一个带有Spring框架的后端部分。我想在后端发布一个日期对象。后端有一个Local对象。我不需要LocalDateTime对象

我的约会服务是角质的。我需要保留日期类型,而不是使用字符串。

代码语言:javascript
复制
addDate(): Observable<Date> {
    let now = new Date();
    return this.http
      .post<Date>('/api/date', now)
      .pipe(
        tap(response => {
          return response;
        }),
        catchError(error => this.notificationService.handleError(error))
      );
  }

我的后端服务:

代码语言:javascript
复制
    @PostMapping
    public LocalDate addIrregularity(@RequestBody LocalDate date, HttpServletRequest request) {
        log.info(date);
        return date;
    }

我有个错误:

2009-08-06 08:21:02.185警告1444年- nio-8080-exec-4 .w.s.m.s.DefaultHandlerExceptionResolver :已解析的.w.s.m.s.DefaultHandlerExceptionResolver JSON解析错误:无法从字符串“2019-08-06T00:00:00.000+0000”反序列化java.time.LocalDate类型的值: java.time.LocalDate:(java.time.format.DateTimeParseException)文本'2019-08-06T00:00:00.000+0000‘无法解析,在索引23处找到未解析文本;嵌套异常是com.fasterxml.jackson.databind.exc.InvalidFormatException:不能从字符串“2019-0806T00:00:00.000+0000”反序列化java.time.LocalDate类型的值:无法反序列化java.time.LocalDate:(java.time.format.DateTimeParseException)文本‘2019-0806T00:00:00.000+0000’,索引23中未解析的文本

EN

回答 1

Stack Overflow用户

发布于 2021-03-03 16:11:08

如何将LocalDate和LocalDateTime从角度发送到弹簧

更好的解决办法:

app.module.ts

代码语言:javascript
复制
import {DatePipe} from '@angular/common';
.
.
.
providers: [DatePipe]

app.service.ts

代码语言:javascript
复制
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
    }); 
  }

弹簧部分

代码语言:javascript
复制
@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) {) {
...
}

如果您只想转换LocalDate,可以离开datePipe,只需从角发送新的Date().toISOString()。但是,由于发问者特别要求LocalDate,所以需要某种格式才能得到这种日期->“yyyy”。只有有了这个春天,才能发挥出它的魔力。

顺便说一句。如果您只需要时间(HH:mm:ss.SSSXXX),即DateTimeFormat.ISO.TIME,也是可能的。

完成!

不是很好的解决方案,更不舒服的->转换为String (没有测试,因为我对Spring解决方案很满意)

app.service.ts

代码语言:javascript
复制
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
    }); 
  }

春天:

代码语言:javascript
复制
@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...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57371807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档