Week of year, base year 2000

Posted on 2005-04-25
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?


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

- the second week of 2001 would be week 54 ...
Question by:mariec
    LVL 86

    Expert Comment

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

    Author Comment

    Because some "rare" years have 53 weeks
    LVL 86

    Expert Comment

    >> 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?

    Author Comment

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

    Expert Comment

    You'll probably have to do it the long way....
    LVL 2

    Accepted Solution

    try something like this:

    Calendard base = Calendar.getInstance();

    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.

    LVL 25

    Expert Comment

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

    Author Comment

    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;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Title # Comments Views Activity
    post4 challenge 28 70
    Convert ArrayList of custom objects to JSON 4 44
    countX 22 49
    array220 challenge 8 28
    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now