What is the difference between Calendar.HOUR and Calendar.HOUR_OF_DAY ?
When to use Calendar.HOUR and Calendar.HOUR_OF_DAY ?
I am confused sometime Calendar.HOUR this works fine and othertime Calendar.HOUR_OF_DAY this works fine. What they return in the form of int?
I have read this documentation but not understood the difference.
Any suggestions
Thanks.
From http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#HOUR:
Calendar.HOUR = Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock. E.g., at 10:04:15.250 PM the HOUR is 10.
Calendar.HOUR_OF_DAY = Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.
This code will help you understand better
import java.util.Calendar; import java.util.GregorianCalendar;
public class test{ public static void main(String[] args) {
GregorianCalendar gc = new GregorianCalendar(2013, 8, 15, 21, 69,55);
//minutes = 69 is equal to 1 hour and 09 minutes. This hour will add to Hour place (21+1 = 22)//Sun Sep 15 22:09:55 IST 2013
p(gc, Calendar.YEAR); //gives year
p(gc, Calendar.MONTH); // gives month staring at 0 for January
p(gc, Calendar.DATE); // date
p(gc, Calendar.DAY_OF_WEEK);// Sunday=1, Monday=2, .....Saturday -7
p(gc, Calendar.WEEK_OF_MONTH);//what week its running in week ,whether its first or second;
p(gc, Calendar.DAY_OF_WEEK_IN_MONTH);//In this case, How may times does Sunday is repeating in the month = 3;
p(gc, Calendar.DAY_OF_YEAR);//count of the day in the year
p(gc, Calendar.HOUR);//12 hour format. if the time is 22:09:55, answer would be (22-12)=10
p(gc, Calendar.HOUR_OF_DAY);// hour of day that is 22 (24h format)
p(gc, Calendar.MINUTE);// 09
p(gc, Calendar.SECOND);// 55
System.out.println();
System.out.println(gc.getTime());
}
static void p(Calendar c, int type) {
System.out.print(c.get(type) + "-");
}
}
*output :
2013-8-15-1-3-3-258-10-22-9-55-
Sun Sep 15 22:09:55 IST 2013
*
output Date visualization
Related
I have this very old code block from PROD (>7 years) to be debugged. There's one point I couldnt understand.
A section in the code does a calculation of next time a task will run and for tasks who need to run specifically on sunday, monday it uses Calendar.SUNDAY. But there's one statement whose behaviour I cannot interpret even after reading the docs multiple times
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, 0);
since the days are numbered from 1-7 (Calendar.SUNDAY to Calendar.SATURDAY) that can be interpreted, but how does zero work here and why there is no exception?
why there is no exception?
It is because you haven't set the lenient mode to false and it is true by default.
Demo:
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.set(Calendar.DAY_OF_WEEK, 0);
System.out.println(cal.getTime());
}
}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: DAY_OF_WEEK
The documentation says:
Any out of range values are either normalized in lenient mode or
detected as an invalid value in non-lenient mode
As part of the normalization, the value are rolled over e.g. the following code sets the value equivalent to cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY - 1):
cal.set(Calendar.DAY_OF_WEEK, 0);
Similarly, the following code sets the value equivalent to cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY - 2):
cal.set(Calendar.DAY_OF_WEEK, -1);
By trying it out in a "test bench", I found this:
It looks the "calendar set" adjust value/integer when number 1-7 is "overflown".
I can see this pattern:
Day Of Week: 1 2 3 4 5 6 7 | 1 2 3 4 5 6 7 | 1 2 3 4 5 6 7 | ...
Value of calendar: -6 -5 -4 -3 -2 -1 0 | 1 2 3 4 5 6 7 | 8 9 10 11 12 13 14 | ...
Test bench:
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, -6);
System.out.println("Calendar value -6 returns: " + cal.get(Calendar.DAY_OF_WEEK));
cal.set(Calendar.DAY_OF_WEEK, 0);
System.out.println("Calendar value 0 returns: " + cal.get(Calendar.DAY_OF_WEEK));
cal.set(Calendar.DAY_OF_WEEK, 1);
System.out.println("Calendar value 1 returns: " + cal.get(Calendar.DAY_OF_WEEK));
cal.set(Calendar.DAY_OF_WEEK, 7);
System.out.println("Calendar value 7 returns: " + cal.get(Calendar.DAY_OF_WEEK));
cal.set(Calendar.DAY_OF_WEEK, 8);
System.out.println("Calendar value 8 returns: " + cal.get(Calendar.DAY_OF_WEEK));
cal.set(Calendar.DAY_OF_WEEK, 14);
System.out.println("Calendar value 14 returns: " + cal.get(Calendar.DAY_OF_WEEK));
}
Output:
Calendar value -6 returns: 1
Calendar value 0 returns: 7
Calendar value 1 returns: 1
Calendar value 7 returns: 7
Calendar value 8 returns: 1
Calendar value 14 returns: 7
Output is according to "pattern".
java.time
I recommend that you use java.time, the modern Java date and time API, for your date and time work. For example:
LocalDate ld = LocalDate.now(ZoneId.systemDefault())
.with(DayOfWeek.TUESDAY);
System.out.println(ld);
When I ran this code today, Saturday, June 5, the output was:
2021-06-01
And yes, June 1 was Tuesday. Since we are passing an enum constant to with(), there is really no possibility of passing an out-of-range value. DayOfWeek is an enum holding 7 values for the 7 days of the week. Only trouble we can get ourselves into is by passing null, which will throw a NullPointerException, which I think you wanted.
If we do insist on passing the day of week as a number, that is possible, though. java.time numbers the days of the week from Monday = 1 through Sunday = 7.
LocalDate ld = LocalDate.now(ZoneId.systemDefault())
.with(ChronoField.DAY_OF_WEEK, 2);
So far the output is 2021-06-01 as before. But what if we pass 0?
.with(ChronoField.DAY_OF_WEEK, 0);
Exception in thread "main" java.time.DateTimeException: Invalid value
for DayOfWeek (valid values 1 - 7): 0
Not only do we get the exception you asked for, we are also getting a clear and helpful exception message, IMHO.
How does day of week zero work here?
With Calendar day of week 0 works the same as 7 = Saturday. It seems that at least a lenient old-fashioned GregorianCalendar performs a kind of modulo 7 operation on the day of week to bring it inside the interval 1 through 7. I did not find this documented. GregorianCalendar is probably the concrete subclass of Calendar that you are dealing with. I tried with different numbers that are all equivalent to 7 modulo 7:
int[] dows = { 0, 7, -7, 14, -14, -98 };
for (int dow : dows) {
Calendar cal = new GregorianCalendar(2021, Calendar.JUNE, 2);
Date dateBefore = cal.getTime();
cal.set(Calendar.DAY_OF_WEEK, dow);
System.out.format("%s and day of week %3d yields %s%n", dateBefore, dow, cal.getTime());
}
Output:
Wed Jun 02 00:00:00 CEST 2021 and day of week 0 yields Sat Jun 05 00:00:00 CEST 2021
Wed Jun 02 00:00:00 CEST 2021 and day of week 7 yields Sat Jun 05 00:00:00 CEST 2021
Wed Jun 02 00:00:00 CEST 2021 and day of week -7 yields Sat Jun 05 00:00:00 CEST 2021
Wed Jun 02 00:00:00 CEST 2021 and day of week 14 yields Sat Jun 05 00:00:00 CEST 2021
Wed Jun 02 00:00:00 CEST 2021 and day of week -14 yields Sat Jun 05 00:00:00 CEST 2021
Wed Jun 02 00:00:00 CEST 2021 and day of week -98 yields Sat Jun 05 00:00:00 CEST 2021
Tutorial link
Oracle tutorial: Date Time explaining how to use java.time.
This is a question related to Java Date year calculation is off by year for two days
I understand the problem appeared from using 'YYYY' instead of 'yyyy', whereby 'YYYY' refers to calendar year instead of the actual year, resulting in the year being wrong if the dates fell onto the first week of January's calendar year.
I tried to read further and understand the problem in
https://docs.oracle.com/javase/9/docs/api/java/util/GregorianCalendar.html#week_year
And it says
"A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year may have different calendar year values."
I have been trying to see if there are any time of the year where 01-Jan-XXXX is actually displayed as 01-Jan-(XXXX-1) but have not managed to find any. Is there a case where this may happen?
I did something simple to take string dates and print out using YYYYMMdd format
public static void main(String[] args) throws ParseException
{
Calendar testCalendar = Calendar.getInstance();
System.out.println("First day of the week: " + testCalendar.getFirstDayOfWeek());
System.out.println("Minimal Days in First Week: " + testCalendar.getMinimalDaysInFirstWeek());
SimpleDateFormat YYYYMMdd= new SimpleDateFormat("YYYYMMdd");
String dateString = "01/01/2016";
Date date = new Date();
date = new SimpleDateFormat("dd/MM/yyyy").parse(dateString);
testCalendar.setTime(date);
int week = testCalendar.get(Calendar.WEEK_OF_YEAR);
String date2 = YYYYMMdd.format(date);
System.out.println("Week Number: " + week);
System.out.println("Date: " + date2);
}
And the output was
First day of the week: 1
Minimal Days in First Week: 1
Week Number: 1
Date: 20161231
If I change the date to "01/01/2016"
The output was
First day of the week: 1
Minimal Days in First Week: 1
Week Number: 1
Date: 20160101
So 01/01/2016 is the first week of of 2016, and not week 53 of 2015.
For a concrete answer, the following table shows January 1 for each year from 2010 through 2020 with day-of-week, week-based year and week number in ISO (the international standard) and in the US.
Year DOW ISO US
2010 Fri 2009-53 2010-01
2011 Sat 2010-52 2011-01
2012 Sun 2011-52 2012-01
2013 Tue 2013-01 2013-01
2014 Wed 2014-01 2014-01
2015 Thu 2015-01 2015-01
2016 Fri 2015-53 2016-01
2017 Sun 2016-52 2017-01
2018 Mon 2018-01 2018-01
2019 Tue 2019-01 2019-01
2020 Wed 2020-01 2020-01
As you can see, internationally January 1 regularly falls in week 52 or 53 of the previous year, while in the US it always falls in week 1 of its own year.
International rule: Week 1 is the first one that contains at least 4 days of the new year. In other words, a week belongs in the year where most of its days are. In yet other words, week 1 is the one that holds the first Thursday of the year (since weeks begin on Mondays). This implies that when January 1 is a Friday, Saturday or Sunday, it belongs to the last week of the previous year.
US rule: Week 1 is the week that contains January 1. That January 1 always falls in week 1 of its own year follows from this definition (weeks begin on Sundays).
The table came out of this snippet:
System.out.println("Year DOW ISO US");
for (int year = 2010; year <= 2020; year++) {
LocalDate jan1 = LocalDate.of(year, Month.JANUARY , 1);
System.out.format(Locale.ROOT, "%4d %3s %4d-%02d %4d-%02d%n",
year, jan1.getDayOfWeek().getDisplayName(TextStyle.SHORT_STANDALONE, Locale.ROOT),
jan1.get(WeekFields.ISO.weekBasedYear()), jan1.get(WeekFields.ISO.weekOfWeekBasedYear()),
jan1.get(WeekFields.SUNDAY_START.weekBasedYear()), jan1.get(WeekFields.SUNDAY_START.weekOfWeekBasedYear()));
}
I am of course using java.time, the modern Java date and time API. I warmly recommend it over the outdated Calendar, SimpleDateFormat and Date.
No. The week year is only to be used in conjunction with the week. For example the 1st January 2016 (a Friday) is in the 53. week of 2015. It will never be displayed as 1. January 2015 since that would be ambiguous.
Calendar d = Calendar.getInstance();
d.set(Calendar.YEAR, 2016)
d.set(Calendar.MONTH, Calendar.JANUARY);
d.set(Calendar.DATE, 1);
SimpleDateFormat ft = new SimpleDateFormat("w-YYYY");
ft.format(d.getTime());
// => "53-2015"
In the US, the first week of the year is defined as being the week containing January 1*.
As a consequence, the week-year for January 1 will always be the same as the calendar year, in the US.
*https://en.wikipedia.org/wiki/Week#Week_numbering
I am working on an Android app that will display a list of activities. Every activity (i.e. waling, running) has a property Date (i.e. 9 March 8:58 2017). I have two buttons on the screen - Daily and Weekly and I want to switch betweren the two and change the list accordingly. Now, for the Daily list, I don't have to change anything, since a new Activity is created for every day.
However, I am not sure how to go about the Weekly list. It will essentially calculate stats (adding up the statistics for the individual weeks).
For example, I have a list of dates for the last 50 days. How to distinguish an individual list of Dates that would represent an individual week so I can construct a list of Weeks? Basically, convert those 50 dates into their week equivalent (e.g. about 7 weeks)
This is a test list of Dates that I am trying to get working first:
HashMap<Integer,Date> dateHashMap = new HashMap<>();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
List<Date> dates = new ArrayList<>();
dates.add(sdf.parse("10/03/2017"));
dates.add(sdf.parse("9/03/2017"));
dates.add(sdf.parse("8/03/2017"));
dates.add(sdf.parse("7/03/2017"));
dates.add(sdf.parse("6/03/2017"));
dates.add(sdf.parse("23/02/2017"));
dates.add(sdf.parse("3/02/2017"));
dates.add(sdf.parse("2/02/2017"));
dates.add(sdf.parse("1/02/2017"));
for(Date d:dates){
dateHashMap.put(d.getDay(),d);
}
System.out.println(dateHashMap.toString());
An example UI design that I am trying to achieve:
As you already have Date property for each activity, then its quite simple actually
First just decide how you want your weeks
ex: I would just go from Mon to Sun as one week
So here Week nth will have dates - 1st to 5th March and Week nth+1 will have 6th to 12th March and so on..
and as far as i can understand you already have every activity (i.e. waling, running) with a property Date (i.e. 9 March 8:58 2017)
So taking an example here (let me know if this isn't how you have your data) :
waling - 1 March 2017 8:58 to 9:58, 3 March 2017 6:20 to 6:50, 8 March 2017 12:00 to 13:00
running - 2 March 2017 6:10 to 8:00, 3 2017 March 7:00 to 8:00, 9 March 2017 5:50 to 7:00
Now data for Week nth you can calculate by adding up duration for waling activity for dates 1st and 3rd March as waling was present only on these dates on Week nth of March 2017 and similarly for week nth+1 and so on
Same goes for running activity for week nth adding up for dates 2nd March, 3rd March and similarly for week nth+1 and so on..
Now you will have something like :
Week nth :
Wailing - 1 hr and 30 min
Running - 2 hrs and 50 min
Week nth+1 :
Wailing - 1 hr
Running - 1 hr and 10 min
And on clicking of each activity you can show some more details..
Hope this helps :)
Edit :
Considering this is how you have your dates list
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
List<Date> dates = new ArrayList<>();
try {
dates.add(sdf.parse("10/03/2017"));
dates.add(sdf.parse("9/03/2017"));
dates.add(sdf.parse("8/03/2017"));
dates.add(sdf.parse("7/03/2017"));
dates.add(sdf.parse("6/03/2017"));
dates.add(sdf.parse("23/02/2017"));
dates.add(sdf.parse("3/02/2017"));
dates.add(sdf.parse("2/02/2017"));
dates.add(sdf.parse("1/02/2017"));
} catch (ParseException e) {
e.printStackTrace();
}
You can create a custom List just to identify the dates that falls in the same week ex (I just used what #Jameson suggested in his answer, you can always write this a lot better):
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;
}
}
And you can use getListOfWeeksFromListOfDates(dates); to have a list with Dates and Week number, this week number can serve as an identifier to compare the dates and then you can add the activities for dates with same Week number.. Hope you are getting what i am trying to convey here :)
/**
* Gets a list of week numbers (as strings) from a list of dates.
*
* #param listOfDates the list of dates
* #return a list of week in year (as string), corresponding
* one-to-one to the values in the input list.
*/
public List<String> getListOfWeeksFromListOfDates(List<Date> listOfDates) {
List<String> listOfWeeks = new ArrayList<>();
for (Date date : listOfDates) {
listOfWeeks.add(new SimpleDateFormat("w").format(date));
}
return listOfWeeks;
}
Week?
You have not defined what you mean by week.
Do you mean the standard ISO 8601 week where each week starts on a Monday and week # 1 contains the first Thursday, each week numbered 1-52 or 53?
Or do you mean a United States type week beginning on a Sunday with weeks numbered 1-52/53 starting with January 1, and if so are the last days of the previous week in the previous year or the new year?
Or do you mean something else?
Time zone?
What time zone do want to use as the context for determining the date? Or do you want to keep your date-times in UTC like Stack Overflow does in tracking your activity for “today” vs “yesterday”?
Avoid legacy date-time classes
The troublesome old date classes including Date and Calendar should be avoided whenever possible. They are now supplanted by the java.time classes.
Convert your given Date objects to Instant, a moment on the timeline in UTC.
Instant instant = myDate.toInstant();
ISO 8601
I suggest using the standard week whenever possible.
Adjust your Instant into the desired time zone.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );
Retrieve the standard week number.
int weekNumber = zdt.get( WeekFields.ISO.weekOfWeekBasedYear() );
Keep in mind that the year number to go with this week number is not the calendar year. We want the year of the week-based year. For example, in some years, December 30 and 31 can belong to the following year number of a week-based year.
int yearOfWeekBasedYear = zdt.get( WeekFields.ISO.weekBasedYear() );
You could track your records against a string composed of this yearOfWeekBasedYear and weekNumber. Use standard format, yyyy-Www such as 2017-W07.
ThreeTen-Extra YearWeek
Instead I suggest you use meaningful objects rather than mere strings. Add the ThreeTen-Extra library to your project to gain the YearWeek class.
This code replaces the WeekFields code we did above.
YearWeek yw = YearWeek.from( zdt );
Thanks to #shadygoneinsane and # Basil Bourque for pointing me to the right direction I solved the problem the following way:
TreeMap<Integer, List<Date>> dateHashMap = new TreeMap<>();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
List<Date> dates = new ArrayList<>();
dates.add(sdf.parse("10/03/2017"));
dates.add(sdf.parse("9/03/2017"));
dates.add(sdf.parse("8/03/2017"));
dates.add(sdf.parse("7/03/2017"));
dates.add(sdf.parse("6/03/2017"));
dates.add(sdf.parse("23/02/2017"));
dates.add(sdf.parse("3/02/2017"));
dates.add(sdf.parse("2/02/2017"));
dates.add(sdf.parse("1/02/2017"));
for (int i = 0; i < dates.size(); i++) {
List<Date> datesList = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(dates.get(i));
int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
for (Date date : dates) {
Calendar c = Calendar.getInstance();
c.setTime(date);
if (weekOfMonth == c.get(Calendar.WEEK_OF_MONTH)) {
datesList.add(date);
}
}
dateHashMap.put(weekOfMonth, datesList);
}
System.out.println(dateHashMap.toString());
}
And the result:
Output:
1=[Fri Feb 03 00:00:00 GMT 2017,
Thu Feb 02 00:00:00 GMT 2017,
Wed Feb 01 00:00:00 GMT 2017],
2=[Fri Mar 10 00:00:00 GMT 2017,
Thu Mar 09 00:00:00 GMT 2017,
Wed Mar 08 00:00:00 GMT 2017,
Tue Mar 07 00:00:00 GMT 2017,
Mon Mar 06 00:00:00 GMT 2017],
4=[Thu Feb 23 00:00:00 GMT 2017]
Exactly what I needed! So now I can iterate through each week and sum up the statistics and thus formulate the "Weekly" view of the list
This question already has answers here:
Why is January month 0 in Java Calendar?
(18 answers)
Closed 3 years ago.
According to doc, calendar set() is:
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#set%28int,%20int,%20int%29
set(int year, int month, int date)
Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.
code:
Calendar c1 = GregorianCalendar.getInstance();
c1.set(2000, 1, 30); //January 30th 2000
Date sDate = c1.getTime();
System.out.println(sDate);
output:
Wed Mar 01 19:32:21 JST 2000
Why it's not Jan 30 ???
1 for month is February. The 30th of February is changed to 1st of March.
You should set 0 for month. The best is to use the constant defined in Calendar:
c1.set(2000, Calendar.JANUARY, 30);
Months in Calendar object start from 0
0 = January = Calendar.JANUARY
1 = february = Calendar.FEBRUARY
Selected date at the example is interesting. Example code block is:
Calendar c1 = GregorianCalendar.getInstance();
c1.set(2000, 1, 30); //January 30th 2000
Date sDate = c1.getTime();
System.out.println(sDate);
and output Wed Mar 01 19:32:21 JST 2000.
When I first read the example i think that output is wrong but it is true:)
Calendar.Month is starting from 0 so 1 means February.
February last day is 28 so output should be 2 March.
But selected year is important, it is 2000 which means February 29 so result should be 1 March.
I have the start date and the end date. I need to iterate through every day between these 2 dates.
What's the best way to do this?
I can suggest only something like:
Date currentDate = new Date (startDate.getTime ());
while (true) {
if (currentDate.getTime () >= endDate.getTime ())
break;
doSmth ();
currentDate = new Date (currentDate.getTime () + MILLIS_PER_DAY);
}
ready to run ;-)
public static void main(String[] args) throws ParseException {
GregorianCalendar gcal = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
Date start = sdf.parse("2010.01.01");
Date end = sdf.parse("2010.01.14");
gcal.setTime(start);
while (gcal.getTime().before(end)) {
gcal.add(Calendar.DAY_OF_YEAR, 1);
System.out.println( gcal.getTime().toString());
}
}
Ditto on those saying to use a Calendar object.
You can get into surprising trouble if you try to use a Date object and add 24 hours to it.
Here's a riddle for you: What is the longest month of the year? You might think that there is no answer to that question. Seven months have 31 days each, so they are all the same length, right? Well, in the United States that would be almost right, but in Europe it would be wrong! In Europe, October is the longest month. It has 31 days and 1 hour, because Europeans set their clocks back 1 hour for Daylight Saving Time in October, making one day in October last 25 hours. (Americans now begin DST in November, which has 30 days, so November is still shorter than October or December. Thus making that riddle not as amusing for Americans.)
I once ran into trouble by doing exactly what you're trying to do: I used a Date object and added 24 hours to it in a loop. It worked as long as I didn't cross Daylight Saving Time boundaries. But when I did, suddenly I skipped a day or hit the same day twice, because Midnight March 8, 2009 + 24 hours = 1:00 AM March 10. Drop off the time, as I was doing, and March 9 was mysteriously skipped. Likewise midnight Nov 1, 2009 + 24 hours = 11:00 PM Nov 1, and we hit Nov 1 twice.
Use a Calendar object if you want to manipulate dates.
Calendar c = Calendar.getInstance();
// ... set the calendar time ...
Date endDate = new Date();
// ... set the endDate value ...
while (c.getTime().before(endDate) {
// do something
c.add(Calendar.DAY_OF_WEEK, 1);
}
Or use Joda Time
I highly recommend using Joda time:
Java Joda Time - Implement a Date range iterator