下面是我的代码。我试图获取在当前日期之前30天启动的实例ID。我无法获取数据,因为它返回了一个空列表。
public List<String> getFilteredInstances(){
List<String> instanceIds=getAllInstanceIds();
Filter instanceIdFilter = new Filter("instance-id",instanceIds);
//Creating date
LocalDate today = LocalDate.now();
List<String> launchTimeFilterValue = new ArrayList<>();
//Subtracting 30 days from the current date
launchTimeFilterValue.add(today.minusDays(30).toString());
Filter launchTimeFilter = new Filter("launch-time",launchTimeFilterValue);
AmazonEC2 amazonEC2 = AmazonEC2ClientBuilder.defaultClient();
DescribeInstancesRequest request = new DescribeInstancesRequest();
DescribeInstancesResult response = amazonEC2.describeInstances(request.withFilters(instanceIdFilter, launchTimeFilter));
List<String> filteredInstanceIds=new ArrayList<>();
response.getReservations().stream()
.flatMap(reservation -> reservation.getInstances().stream())
.forEach(instance -> filteredInstanceIds.add(instance.getInstanceId()));
return filteredInstanceIds;}