Jeff Janes
Jeff Janes

Reputation: 895

Why does this code generate incorrect data?

I have this code in my project

    Calendar subval = Calendar.getInstance();
    final int WOY= subval.WEEK_OF_YEAR;

and when I check it for value of WOY it outputs 3 now it is currently Feb 25 2013 and I know the week number is not three. I am storing this value to help set automatic refresh times so I am able to force a refresh to make sure the device has the most current data. In between refresh periods some crucial data is stored locally. Now I need a reliable fixed time slot and I chose once a week basically if the WEEK OF YEAR is not the same as the stored value for WEEK OF YEAR set data to be refreshed at next opportunity and then store current WEEK OF YEAR on device. I started coding this within 1 week so I have not transitioned to the new week so I am not sure if it working correctly but the value of three scares me.

Upvotes: 0

Views: 81

Answers (5)

Four_lo
Four_lo

Reputation: 1150

Subclasses define WEEK_OF_YEAR for days before the first week of year according to the java oracle documentation

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html

see: week_of_year

Upvotes: 0

Reimeus
Reimeus

Reputation: 159794

Calendar.WEEK_OF_YEAR is a constant to be used to specify which field to return from your Calendar instance. Instead of assigning the value to the fixed value, you need to call Calendar#get:

int WOY = subval.get(Calendar.WEEK_OF_YEAR);

Upvotes: 2

alex
alex

Reputation: 6409

Calendar.WEEK_OF_YEAR is a static constant.

You need to call calendar.get(Calendar.WEEK_OF_YEAR)

Upvotes: 1

Pragnani
Pragnani

Reputation: 20155

You need to get it like this

 Calendar subval = Calendar.getInstance();
   int year=subval.get(Calender.Calendar.WEEK_OF_YEAR);

Upvotes: 1

John3136
John3136

Reputation: 29266

WEEK_OF_YEAR is a flag used to the get() method. It's value never changes. You use it like this:

Calendar subval = Calendar.getInstance();
final int WOY= subval.get(Calendar.WEEK_OF_YEAR);

Upvotes: 4

Related Questions