Is it possible to list the all weeks/date given two date range for example:
Date from 1/1/2013 to 1/1/2020 result will be:
- 1-7,2013
- 8-14,2013
- 15-21,2013 and soon til 2020 and same with month.
Is it possible to list the all weeks/date given two date range for example:
Date from 1/1/2013 to 1/1/2020 result will be:
Please try out this for the case of weeks(check if you can optimize).
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to provide month range dynamically
Calendar calendar = Calendar.getInstance();
Date minDate = calendar.getTime();
calendar.add(Calendar.MONTH, 5); // current month + 5 months calendar
Date maxDate = calendar.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String startDate = dateFormat.format(minDate);
String endDate = dateFormat.format(maxDate);
List<Date> dates = getDates(startDate, endDate); // to get dates between range
int prevIdentifier = 0;
int identifier;
String initDate, finalDate;
List<WeekDay> weekDays = getListOfWeeksFromListOfDates(dates);
SimpleDateFormat dformatter = new SimpleDateFormat("dd");
SimpleDateFormat yformatter = new SimpleDateFormat("yyyy");
initDate = dformatter.format(weekDays.get(0).getDate());
finalDate = dformatter.format(weekDays.get(0).getDate());
String outputData = "";
for (WeekDay weekDay : weekDays) {
identifier = Integer.parseInt(weekDay.getWeekIdentifier()); // this value will be same for all days in same week
if (prevIdentifier != 0 && identifier != prevIdentifier) {
if (outputData.equalsIgnoreCase(""))
outputData += initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
else
outputData += " * " + initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
initDate = dformatter.format(weekDay.getDate());
} else {
finalDate = dformatter.format(weekDay.getDate());
}
prevIdentifier = identifier;
}
System.out.println("OUTPUT DATA :" + outputData);
}
public List<WeekDay> getListOfWeeksFromListOfDates(List<Date> listOfDates) {
List<WeekDay> listOfWeeks = new ArrayList<>();
WeekDay weekDay;
for (Date date : listOfDates) {
weekDay = new WeekDay(date, new SimpleDateFormat("w").format(date));
listOfWeeks.add(weekDay);
}
return listOfWeeks;
}
public class WeekDay {
Date date;
String weekIdentifier;
public WeekDay(Date Date, String WeekIdentifier) {
this.date = Date;
this.weekIdentifier = WeekIdentifier;
}
public Date getDate() {
return date;
}
public String getWeekIdentifier() {
return weekIdentifier;
}
}
private static List<Date> getDates(String dateString1, String dateString2) {
ArrayList<Date> dates = new ArrayList<Date>();
DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = null;
Date date2 = null;
try {
date1 = df1.parse(dateString1);
date2 = df1.parse(dateString2);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
while (!cal1.after(cal2)) {
dates.add(cal1.getTime());
cal1.add(Calendar.DATE, 1);
}
return dates;
}
I would use a LocalDate
for start date (e.g., 1/1/2013) and one for end date (1/1/2020). To represent the period you want (week, month or year) I might use either the appropriate ChronoUnit
constant or — more flexibly — a Period
. The mentioned classes are from java.time, the modern Java date and time API. A pretty simple loop will iterate through your start dates (Jan 1, Jan 8, Jan 15, etc.). Subtract 1 from each start date (except the first) to get the end dates (Jan 8 minus 1 day gives Jan 7, etc.).
java.time
on Android?Yes, java.time
works nicely on Android devices. It just requires at least Java 6.
org.threeten.bp
and subpackages.java.time
.LocalDate
, ChronoUnit
and Period
.