我有一个方法可以返回在特定日期内进行的交易。问题是我的项目要求我应该以一种也接受时间的格式提供开始和结束日期,但这不是我想要的,因为客户会发现输入时间也不友好。当我将信息保存到数据库中时,我没有及时保存它。我只保存了日期,这就是我在数据库中看到的,但我不知道为什么系统只有在我提供这种格式的东西时才能工作 “结束日期”:“2023-03-30T00:20:27.177Z”, “开始日期”:“2023-03-30T00:20:27.177Z” 但我需要的是这种格式的东西 "endDate": "yyyy-MM-dd", “开始日期”:“yyyy-MM-dd”
@Entity
@NoArgsConstructor
@Data
public class TransactionType {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Temporal(TemporalType.DATE)
private Date transactionDate;
private double amount;
private double currentBalance;
private String transactionType;
private String description;
private String depositorOrWithDrawalName;
@ManyToOne
@JoinColumn(name = "account_id")
private BankAccount bankAccount;
}
@Repository
public interface TransactionTypeRepository extends JpaRepository<TransactionType,Long> {
List<TransactionType> findByBankAccount(BankAccount bankAccount);
@Query("select t from TransactionType t where t.bankAccount = :bankAccount and t.transactionDate >= :startDate AND t.transactionDate <= :endDate")
List<TransactionType> findByBankAccountAndTransactionDate(BankAccount bankAccount, @Param("startDate") Date startDate,@Param("endDate") Date endDate);
@Query("select t from TransactionType t where t.transactionDate >= :startDate and t.transactionDate <= :endDate")
List<TransactionType> getTransactionStatement(@Param("startDate") Date startDate, @Param("endDate") Date endDate);
}
@Service
public class TransactionTypeService {
@Autowired
private TransactionTypeRepository transactionTypeRepository;
@Autowired
private BankAccountService bankAccountService;
public List<BankAccountStatement> getAccountStatement(BankAccountStatementDto bankAccountStatementDto) throws ParseException {
List<TransactionType> transactionTypeList = transactionTypeRepository.getTransactionStatement(bankAccountStatementDto.getStartDate(),bankAccountStatementDto.getEndDate());
List<BankAccountStatement> bankAccountStatementList = new ArrayList<>();
for (TransactionType transactionType: transactionTypeList){
BankAccountStatement bankAccountStatement = new BankAccountStatement();
bankAccountStatement.setAmount(transactionType.getAmount());
bankAccountStatement.setCurrentBalance(transactionType.getCurrentBalance());
bankAccountStatement.setDepositorOrWithDrawalName(transactionType.getDepositorOrWithDrawalName());
bankAccountStatement.setDescription(transactionType.getDescription());
bankAccountStatement.setTransactionDate(transactionType.getTransactionDate().toString());
bankAccountStatement.setTransactionType(transactionType.getTransactionType());
bankAccountStatement.setId(transactionType.getId());
bankAccountStatementList.add(bankAccountStatement);
}
return bankAccountStatementList;
}
@NoArgsConstructor
public class BankAccountStatementDto {
@Temporal(TemporalType.DATE)
@DateTimeFormat(style = "yyyy-MM-dd")
@JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "yyyy-MM-dd")
private Date startDate;
@Temporal(TemporalType.DATE)
@DateTimeFormat(style = "yyyy-MM-dd")
@JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "yyyy-MM-dd")
private Date endDate;
public Date getStartDate() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String newDate = dateFormat.format(startDate);
startDate = dateFormat.parse(newDate);
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String newDate = dateFormat.format(endDate);
endDate = dateFormat.parse(newDate);
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
}
What I meant is if I used LocalDate instead of Date, will I be able to do this query?
@Query("select t from TransactionType t where t.transactionDate >= :startDate and t.transactionDate <= :endDate")
List<TransactionType> getTransactionStatement(@Param("startDate") Date startDate, @Param("endDate") Date endDate