我想实现此代码来检查用户是否订阅了服务:
public String calculateSubscription(String email) {
Optional<Subscription> subscriptionsByUserEmail = subscriptionService.findSubscriptionsByUserEmail(email);
if (subscriptionsByUserEmail.isPresent()) {
Subscription subscription = subscriptionsByUserEmail.get();
LocalDateTime startAt = subscription.getStartAt();
LocalDateTime endAt = subscription.getEndAt();
LocalDateTime now = LocalDateTime.now();
if(/* do here comparison */){
return "subscribed";
}
}
return "unsubscribed";
}
在数据库中,我存储 2 个字段:
@Column(name = "start_at")
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime startAt;
@Column(name = "end_at")
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime endAt;
我需要检查now
变量是否在时间间隔startAt
和endAt
.
这个检查如何在 if 语句中实现?
编辑:
@Converter(autoApply = true)
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
return Optional.ofNullable(localDateTime)
.map(Timestamp::valueOf)
.orElse(null);
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp timestamp) {
return Optional.ofNullable(timestamp)
.map(Timestamp::toLocalDateTime)
.orElse(null);
}
}