I am using following code to get the last 7 days:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
String[] days = new String[6];
days[0] = sdf.format(date);
for(int i = 1; i < 6; i++){
cal.add(Calendar.DAY_OF_MONTH, -1);
date = cal.getTime();
days[i] = sdf.format(date);
}
for(String x: days){
System.out.println(x);
}
And this is giving the following output:
2016-04-14
2016-04-13
2016-04-12
2016-04-11
2016-04-10
2016-04-09
But I want this instead:
2016-04-09
2016-04-10
2016-04-11
2016-04-12
2016-04-13
2016-04-14
If I use the following line below the code it will give me the correct order:
List<String> list = Arrays.asList(days);
Collections.reverse(list);
days = (String[]) list.toArray();
for(String x: days){
System.out.println(x);
}
But is there any other way to get the last 7 days in ascending order in one shot?