• Status: Solved
• Priority: Medium
• Security: Public
• Views: 209

# Week of year, base year 2000

Is there an efficient way in Java to get the week number, but using 1/1/2000 as a reference?

i.e.:

- date 04/01/2001 would be week 53, not 1

- the second week of 2001 would be week 54 ...
0
mariec
1 Solution

Commented:
Why not just add 52 onto the value returned from the Calendar if after 2000?
0

Author Commented:
Because some "rare" years have 53 weeks
0

Commented:
>> Because some "rare" years have 53 weeks

You're adding the number of weeks in 2000, so the question of rarity doesn't come into it does it?
0

Author Commented:
yes it does. For example, in 2009 there will be 53 weeks. I can't just add 52 in this case.
0

Associate Director - Product EngineeringCommented:
You'll probably have to do it the long way....
0

Commented:
try something like this:

Calendard base = Calendar.getInstance();
base.set(2000,0,1);

Calendard now = Calendar.getInstane();

int weeks = ( now.getTimeInMillis() - base.getTimeInMillis() )/1000/3600/24/7;

You get the points? Simply get difference of your date and the base date in millseconds. then convert to second, hours, days and weeks.

0

Commented:
Can't you work out the difference in days, then divide by 7?
0

Author Commented:
It seems to be a bit more complexe than this since there is the "Daylight Savings" issue. So if I do everything in UTC, it should work fin:

Calendar calBase = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calBase.set(2000, 0, 3, 0, 0, 0);
calBase.set(Calendar.MILLISECOND, 0);

Calendar today = Calendar.getInstance();
Calendar calNow = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calNow.set(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DATE), 0, 0, 0);
calNow.set(Calendar.MILLISECOND, 0);

long milli = (calNow.getTimeInMillis() - calBase.getTimeInMillis());
long days = (calNow.getTimeInMillis() - calBase.getTimeInMillis()) /1000/3600/24;
long weeks = (days / 7) + 1;

System.out.println(weeks);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.