我在做一个拍卖项目。我想从用户那里抽出一天、一周或12个小时的时间,然后检查它的结果。但是,我如何将用户输入的时间与当前时间进行比较,并发现时间已经过期,我不知道应该在react或spring中这样做。
假设开始时间为:04/06/2022-00:00:00结束时间为:04/06/2022-12:00:00当开始时间和结束时间相等时,我希望拍卖无效。
拍卖类将是这样的。
@Data
@Entity
public class Auction {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@NotNull
private String name;
@NotNull
private String title;
@NotNull
private String price;
@NotNull
private Date startDate;
@NotNull
private Date endDate;
@NotNull
private Category category;
@NotNull
private String sellNowPrice;
private User seller;
private User buyer;
}
您应该始终在后端(Spring)验证这种类型的数据,否则其他访问您的API的人可以很容易地绕过这种数据验证,如果他们只是使用HTTP请求而不使用您的前端。因此,您应该在后端验证这些数据。
现在实现:
Date currentDate = new Date();
long diffInMillies = Math.abs(currentDate.getTime() - endDate.getTime());
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
如果diff
现在是负数,则拍卖尚未达到结束日期。但如果diff
是正数,则拍卖在diff
-days