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

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
Asked:
mariec
1 Solution
 
CEHJCommented:
Why not just add 52 onto the value returned from the Calendar if after 2000?
0
 
mariecAuthor Commented:
Because some "rare" years have 53 weeks
0
 
CEHJCommented:
>> 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mariecAuthor Commented:
yes it does. For example, in 2009 there will be 53 weeks. I can't just add 52 in this case.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You'll probably have to do it the long way....
0
 
limaidealCommented:
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
 
InteractiveMindCommented:
Can't you work out the difference in days, then divide by 7?
0
 
mariecAuthor 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.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now