Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Calendar - Roll by day bug?

Thread view: 
donlelel - 03 Sep 2006 14:29 GMT
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
Andrew Thompson - 03 Sep 2006 14:52 GMT
...
> 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.
shakah - 04 Sep 2006 14:00 GMT
> Hi,
>
[quoted text clipped - 38 lines]
>   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).


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.