I am generating all dates between two dates.
public List<Date> generateRangeDates(Date firstDate, Date lastDate) {
List<Date> allDates = new ArrayList<Date>();
Calendar calendar = new GregorianCalendar();
calendar.setTime(firstDate);
while (calendar.getTime().before(lastDate)) {
Date resultado = calendar.getTime();
allDates .add(resultado);
calendar.add(Calendar.DATE, 1);
}
Date lastDate1 = allDates .get(dates.size()-1);
Calendar cal = new GregorianCalendar();
cal.setTime(lastDate1);
cal.add(Calendar.DATE, 1);
allDates .add(cal.getTime());
for(Date date1 : dates) {
System.out.println("All Dates : "+ date1);
}
return dates;
}
Above method generating All Dates starting from the first Date till last date. I know it has a major shortcomings. like i could have used joda-time instead of java.util.Calendar. there is also a ambiguity about daylight saving. also it didn’t mention TIMEZONE. Also using GregorianCalendar, which totally ignores the current Locale.
– for the time being – I DO NOT WANT TO USE JODA API.
Help needed to update the above function which works in all TIMEZONES. I have googled a lot on this but also got confused a lot.
also a question ( might sound a bit stupid)-
Why do people insist using JODA API? doesn’t it have any ambiguity?
Note – these lines generating the last date and being added in allDates list.
Date lastDate1 = allDates .get(dates.size()-1);
Calendar cal = new GregorianCalendar();
cal.setTime(lastDate1);
cal.add(Calendar.DATE, 1);
allDates .add(cal.getTime());
Is there any reason not to use
longfor times? If you need to use Dates you can do this.Date is time zone independent.
You can’t create a list which is complete TimeZone independent as there is no such data structure in Java.
The reason other may have suggested using JODA is it have proper Date (without time) objects as it is properly timezone independent.