Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem of using method add() in java.util.Calendar

Status
Not open for further replies.

2much

Programmer
Dec 16, 2001
10
TW
Hi...

I have a problem of using method add() in java.util.Calendar, it returns wrong value in some specific date.

here is my codes, it will return a string of year plus week when I move the date by week:

import java.util.*;
import java.text.SimpleDateFormat;


public class WorkTimeArranger
{
public static String getWorkNoString(int weekToMove)
{
Calendar cal = new GregorianCalendar(TimeZone.getDefault());
cal.add(Calendar.WEEK_OF_YEAR,weekToMove);

String year = String.valueOf(cal.get(Calendar.YEAR));
String week = String.valueOf(cal.get(Calendar.WEEK_OF_YEAR));
.length() == 1 ? "0" + weektmp : weektmp);

return year + weekNo;

}

public static void main(String[] args)
{
WorkTimeArranger wa = new WorkTimeArranger();

System.out.println("pre week:" + WorkTimeArranger.getWorkNoString(-1));
System.out.println("this week:" + WorkTimeArranger.getWorkNoString(0));
System.out.println("next week:" + WorkTimeArranger.getWorkNoString(1));

}

}

but if you adjust your date to 2001/12/31 or the last few days of the year, and run this , it will show:


preweek:200152
thisweek:20011
nextweek:20022


why does it return 20011 instead of 20021 or 200153 to 'thisweek' ?

 
This is what we call a "bug". I confirmed that it exists in both jdk1.2.2 and jdk1.4. In this specific case, the add() method is acting like the roll() method.

There are several such oddities in the date routines - some are bugs and some are just things that work differently than you're expecting. You just code around them.
 
Oh, I see..

Thanks,idarke !

And do you have any suggestion to get the correct string in such a case like mine? --move the week and get the string of the year plus the week of the year?
 
I would just forget the add method and calculate the value myself. Remove the add() method call, and get the week and year as ints. Add weekToMove to the week and check for the result <1 or >52 and roll the year accordingly:

while (week < 1 || week > 52)
{
if (week < 1)
{
week += 52;
year -= 1;
}
if (week > 52)
{
week -= 52;
year += 1;
}
}

Hope this helps.
 
Thank,idarke!

I've tried that before, but the problem still exists.. :( It seems to be the wrong values returned the methods get(Calendar.YEAR) and get(Calendar.WEEK_OF_YEAR) if we were on the last few days of a year.

I've tried to adjust my date to 2001/12/30 or 2002/12/30 and the result are:

<2001/12/30>

int year = cal.get(Calendar.YEAR); // This will be 2001
int week = cal.get(Calendar.WEEK_OF_YEAR); // This will be 1 --- ??

<2002/12/30>

int year = cal.get(Calendar.YEAR); // This will be 2002
int week = cal.get(Calendar.WEEK_OF_YEAR); // This will be 1 -- ??



so that's why I think we can not get the correct string by using add() or the method you suggested. We will get wrong value even we just call mehtod get(Calendar.XXX) on those days.

my java is &quot;1.3.0_02&quot;
 
I got this code to work:

import java.util.*;
import java.text.SimpleDateFormat;

public class WorkTimeArranger
{
private java.util.Calendar cal = new GregorianCalendar();

public String getWorkNoString(int weekToMove)
{
int iyear = cal.get(Calendar.YEAR);
int iweek = cal.get(Calendar.WEEK_OF_YEAR);
iweek += weekToMove;
while (iweek < 1 || iweek > 52)
{
if (iweek < 1)
{
iweek += 52;
iyear -= 1;
}
if (iweek > 52)
{
iweek -= 52;
iyear += 1;
}
}

String year = String.valueOf(iyear);
String weektmp = String.valueOf(iweek);
String weekNo = (weektmp.length() == 1 ? &quot;0&quot; + weektmp : weektmp);

return year + weekNo;
}


public static void main(String[] args)
{
WorkTimeArranger wa = new WorkTimeArranger();
System.out.println(&quot;pre week: &quot; + wa.getWorkNoString(-1));
System.out.println(&quot;this week: &quot; + wa.getWorkNoString(0));
System.out.println(&quot;next week: &quot; + wa.getWorkNoString(1));
System.out.println(&quot;week after:&quot; + wa.getWorkNoString(2));
}
}

 
I appreciate your kind help,idarke !

But this is what I got after running your code on the day 2002/12/31:

pre week: 200152
this week: 200201
next week: 200202
week after:200203


and on the day 2001/12/30:

pre week: 200052
this week: 200101
next week: 200102
week after:200103

the string of year seems to be wrong, it should be 1 more year. Did you get the same output as mine on those days?

Anyways, thanks a lot! And sorry for disturbing you.

 
I went back and looked and ... by golly, you're right. I can't believe I missed that. Sorry.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top