Calendar - Roll by day bug?

D

donlelel

Hi,

Is it by design that roll by day doesnt change the month? If I roll one
day less and it gets to the point when it should decrease the month, it
just doesnt do it. Is there any other way to "roll" days in the year,
with a month change if necessary? Well, I'll rewrite the method bellow
to set the date by the number of the day in the year and check if
that's Mon, but I was just curious...

public static Date MonOfWeek(Date aWeekDay) {
//Calendar rightNow = Calendar.getInstance();
GregorianCalendar rightNow = new GregorianCalendar();
if (aWeekDay == null) {
Trace("WARNING MonOfWeek Null date!");
return rightNow.getTime();
}

rightNow.setTime(aWeekDay);
Trace("INFO MonOfWeek
rightNow="+rightNow.get(rightNow.DAY_OF_YEAR));
while (rightNow.MONDAY != rightNow.get(Calendar.DAY_OF_WEEK)) {
rightNow.roll(rightNow.DATE,false);

int aYDay = rightNow.get(rightNow.DAY_OF_YEAR);

Trace("INFO MonOfWeek rightNow=" + aYDay);
}
Trace("INFO MonOfWeek
rightNow="+rightNow.get(rightNow.DAY_OF_YEAR));
return rightNow.getTime();
}

Output:
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=245
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=244
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=273
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=272
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=271
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=270
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=269
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=268
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=268
 
A

Andrew Thompson

donlelel wrote:
....
Is it by design that roll by day doesnt change the month?
From the JavaDocs.. for GC.roll()
"Adds or subtracts (up/down) a single unit of time on the
given time field without changing larger fields. "

Andrew T.
 
S

shakah

donlelel said:
Hi,

Is it by design that roll by day doesnt change the month? If I roll one
day less and it gets to the point when it should decrease the month, it
just doesnt do it. Is there any other way to "roll" days in the year,
with a month change if necessary? Well, I'll rewrite the method bellow
to set the date by the number of the day in the year and check if
that's Mon, but I was just curious...

public static Date MonOfWeek(Date aWeekDay) {
//Calendar rightNow = Calendar.getInstance();
GregorianCalendar rightNow = new GregorianCalendar();
if (aWeekDay == null) {
Trace("WARNING MonOfWeek Null date!");
return rightNow.getTime();
}

rightNow.setTime(aWeekDay);
Trace("INFO MonOfWeek
rightNow="+rightNow.get(rightNow.DAY_OF_YEAR));
while (rightNow.MONDAY != rightNow.get(Calendar.DAY_OF_WEEK)) {
rightNow.roll(rightNow.DATE,false);

int aYDay = rightNow.get(rightNow.DAY_OF_YEAR);

Trace("INFO MonOfWeek rightNow=" + aYDay);
}
Trace("INFO MonOfWeek
rightNow="+rightNow.get(rightNow.DAY_OF_YEAR));
return rightNow.getTime();
}

Output:
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=245
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=244
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=273
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=272
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=271
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=270
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=269
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=268
Sun Sep 03 09:22:21 EDT 2006 : INFO MonOfWeek rightNow=268

How about Calendar.add instead of roll:

public abstract void add(int field, int amount)

Date Arithmetic function. Adds the specified (signed) amount of time to
the given time field, based on the calendar's rules. For example, to
subtract 5 days from the current time of the calendar, you can achieve
it by calling:

add(Calendar.DATE, -5).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top