我有一个带有 Java 后端的 Angular 前端,我想使用 PrimeNG 的 DateInput 来接收和保存一个日期。我得到了以下代码:
<p-calendar [(ngModel)]="enddate" dateFormat="dd.mm.yy" class="medium-field"></p-calendar>
在我的组件中:
enddate: Date;
通过 REST 发送此值时,我有以下代码(包括检查):
createPSP(project_num: string, financialItem: FinancialItem) {
this.logger.info("Startdate is " + financialItem.startDate);
this.logger.info("Enddate is " + financialItem.endDate);
let order_num = financialItem.orderNumber;
let psp_num = financialItem.pspNumber;
return this.http.post(`${httpBaseUrl}/project/${project_num}/order/${order_num}/addFinancialItem/${psp_num}`, financialItem).pipe();
}
输出(这是正确的):
然后我将它保存在我的后端中的- 变量中LocalDate(在 MySQL 中转换为date-type)。现在发生的事情是我插入31.12.2018并得到(在我的 Converter 在后端)30.12.2018和 MySQL 29.12.2018。为什么会这样?
编辑:当我更改为时LocalDate,LocalDateTime我只是进入30.12.2018MySQL 而不是29.12.2018显然仍然是错误的。
更多代码:
我这样定义了我的 MySQL 列(在我的实体中):
@Entity
class FinancialItemDto {
//...
@Column(name = "ENDDATE", nullable = false)
private LocalDate endDate;
}
在控制器中:
public ResponseEntity addFinancialItem(@PathVariable String project_num, @PathVariable String order_num,
@PathVariable String psp_num, @RequestBody FinancialItemDto financialItemDto) {
try {
this.financialItemService.saveItemGivenProjectAndOrderNumber(financialItemDto, order_num);
} catch (NoSuchEntityException e) {
this.logger.error(e.getMessage());
return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getUserMessage());
}
return ResponseEntity.ok(HttpStatus.OK);
}
在服务中:
@Transactional
@Override
public void saveItemGivenProjectAndOrderNumber(FinancialItemDto financialItemDto, String orderNumber)
throws NoSuchEntityException {
OrderEntity order = this.orderRepository.findByOrderNumber(orderNumber).orElseThrow(
() -> new NoSuchEntityException("Order with number " + orderNumber + " could not be found.",
"Der Abruf wurde nicht gefunden."));
OrdertypesEntity ordertype = this.ordertypesRepository.findByShorthand(financialItemDto.getOrderType())
.orElseThrow(() -> new NoSuchEntityException("Ordertype " + financialItemDto.getOrderType()
+ " for creating FI to order " + orderNumber + " could not be found.",
"Der Abruftyp wurde nicht gefunden."));
FinancialItemEntity financialItemEntity = FinancialItemConverter.dtoToEntity(financialItemDto, ordertype,
order);
this.entityManager.persist(financialItemEntity);
}
TS 端的 Dto 定义日期如下:
export class FinancialItem {
endDate: Date;
//...
}
我的转换器只是传递:
public static FinancialItemEntity dtoToEntity(FinancialItemDto financialItemDto, OrdertypesEntity ordertype, OrderEntity order) {
FinancialItemEntity financialItemEntity = new FinancialItemEntity( (...), financialItemDto.getEndDate(), (...));
LoggerFactory.getLogger(FinancialItemConverter.class).info("Got Date Value: " + financialItemDto.getEndDate()); //gives: Got Date Value: 2018-12-30 instead of value 31.12.2018
return financialItemEntity;
}
更新:
REST 服务“松散”一天的一种解决方法是以长格式保存日期,然后传递它并将其转换回来。可悲的是,当调用repository.save()-function 时,记录器会在我插入2018-12-01日期值时调用2018-12-01,但 MySQL 说它2018-11-30在我的数据库中。我无法弄清楚那里发生了什么:
this.logger.info("Startdate is " + financialItemDto.getStartDate()); //2018-12-01
this.logger.info("Startdate is " + financialItemEntity.getStartDate()); //2018-12-01
this.financialItemRepository.save(financialItemEntity);
this.logger.info("Startdate (received) is " + this.financialItemRepository.getFinancialItemByPSPNumber(financialItemDto.getPspNumber()).get().getStartDate()); //2018-12-01
