[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Week of year, base year 2000

Posted on 2005-04-25
8
Medium Priority
?
204 Views
Last Modified: 2010-03-31
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
Comment
Question by:mariec
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13859153
Why not just add 52 onto the value returned from the Calendar if after 2000?
0
 

Author Comment

by:mariec
ID: 13859200
Because some "rare" years have 53 weeks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13859246
>> 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:mariec
ID: 13859425
yes it does. For example, in 2009 there will be 53 weeks. I can't just add 52 in this case.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13859676
You'll probably have to do it the long way....
0
 
LVL 2

Accepted Solution

by:
limaideal earned 150 total points
ID: 13860300
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
 
LVL 25

Expert Comment

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

Author Comment

by:mariec
ID: 13865889
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question