在 .before() 中传递 int 以计算它是否早于 x 天
Pass int in .before() to calculate if its older than x days
我想知道如何使用 date.before()
函数检查给定的测量值是否早于 x 天。我试过按照练习中给出的方式将它传递给它,但需要给它一个日期,所以我不知道如何传递它。
代码:
class WeatherStation {
List<Measurements> measurements;
int days;
public WeatherStation(int days) {
this.days = days;
this.measurements = new ArrayList<>();
}
public void addMeasurements(float temperature, float wind, float humidity, float visibility, Date date) {
measurements.add(new Measurements(temperature, wind, humidity, visibility, date));
for(Measurements m: measurements) {
if(m.date.before(days))
}
}
您可以从 现在 减去 "x" 天计算日期,并将其与 before()
方法一起使用。
示例:
Instant now = Instant.now();
Instant now_minus_x_days = now.minus(x, ChronoUnit.DAYS);
someDate.before(Date.from(now_minus_x_days));
在你的 class 中应该是这样的:
class WeatherStation {
List<Measurements> measurements;
int days;
public WeatherStation(int days) {
this.days = days;
this.measurements = new ArrayList<>();
}
public void addMeasurements(float temperature, float wind, float humidity, float visibility, Date date) {
measurements.add(new Measurements(temperature, wind, humidity, visibility, date));
for(Measurements m: measurements) {
if(m.date.before(Date.from(Instant.now().minus(days, ChronoUnit.DAYS)))) {
}
}
}
}
我想知道如何使用 date.before()
函数检查给定的测量值是否早于 x 天。我试过按照练习中给出的方式将它传递给它,但需要给它一个日期,所以我不知道如何传递它。
代码:
class WeatherStation {
List<Measurements> measurements;
int days;
public WeatherStation(int days) {
this.days = days;
this.measurements = new ArrayList<>();
}
public void addMeasurements(float temperature, float wind, float humidity, float visibility, Date date) {
measurements.add(new Measurements(temperature, wind, humidity, visibility, date));
for(Measurements m: measurements) {
if(m.date.before(days))
}
}
您可以从 现在 减去 "x" 天计算日期,并将其与 before()
方法一起使用。
示例:
Instant now = Instant.now();
Instant now_minus_x_days = now.minus(x, ChronoUnit.DAYS);
someDate.before(Date.from(now_minus_x_days));
在你的 class 中应该是这样的:
class WeatherStation {
List<Measurements> measurements;
int days;
public WeatherStation(int days) {
this.days = days;
this.measurements = new ArrayList<>();
}
public void addMeasurements(float temperature, float wind, float humidity, float visibility, Date date) {
measurements.add(new Measurements(temperature, wind, humidity, visibility, date));
for(Measurements m: measurements) {
if(m.date.before(Date.from(Instant.now().minus(days, ChronoUnit.DAYS)))) {
}
}
}
}