Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6194961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:23:07+00:00 2026-05-24T03:23:07+00:00

I am trying to create a custom week counter but am having quite a

  • 0

I am trying to create a custom week counter but am having quite a lot of trouble and feel like I am going about it all wrong. The method should take in a string date that is in yyyy-MM-dd format and return the week number. The week counter started October 1, 2000. The week starts Friday and ends Thursday. The first 2 digits represents the years and the second 2 represent the week. So this week would be 1143 (11 to represent the year and 43 to represent the weeks since Oct 1).

This is what I have gotten so far:

public static String get_week(String date){

    try{
        Calendar first_dt = Calendar.getInstance();
        first_dt.set(1999, 10, 01);
        long first_dt_milliseconds = first_dt.getTimeInMillis();

        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date format_date = (Date)formatter.parse(date);

        SimpleDateFormat month = new SimpleDateFormat("MM"); 
        SimpleDateFormat year = new SimpleDateFormat("yyyy");

        long drop_dt_milliseconds = format_date.getTime() - first_dt_milliseconds;
        long drop_dt_years = drop_dt_milliseconds / (24 * 60 * 60 * 1000) / 365;

        Calendar year_ago = Calendar.getInstance();
        year_ago.set(Integer.parseInt(year.format(format_date))-1, 10, 01);

        long year_ago_milliseconds = year_ago.getTimeInMillis();

        long year_ago_diff = format_date.getTime() - year_ago_milliseconds;
        year_ago_diff = year_ago_diff / (24 * 60 * 60 * 1000) / 7;

        if (month.format(format_date).equals("10") || month.format(format_date).equals("11") || month.format(format_date).equals("12")){
            date = drop_dt_years+1+""+year_ago_diff;
        }
        else{
            date = year_ago_diff;
        }
    }catch(Exception e){
        e.printStackTrace();
    }

    return date;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:23:07+00:00Added an answer on May 24, 2026 at 3:23 am

    I used Joda-Time because it’s less confusing than Java’s built-in date and time gear

    EDIT – new code, rolled in ChssPly’s suggestion and fixed a problem with the weeks between Oct 1 and Jan 1. Also check out X-Zero’s suggestion to create a custom Chronology in Joda-Time, might be an interesting approach.

    import org.joda.time.DateMidnight;
    import org.joda.time.Weeks;
    import org.joda.time.Years;
    
    public class Main {
    
        private String getWeek (DateMidnight dt2) {
            DateMidnight dt = new DateMidnight(2000,10,1);
    
            // First get the number of elapsed years, ChssPly76's way
            int yearz = Years.yearsBetween(dt, dt2).getYears();
            /*
             * We now need the number of weeks in the current year, which can be
             * calculated using the Weeks class.
             */
            int yearOffset = 1;
            // But if the new date is Oct 1 thru Dec 12 year must remain the same
            if (!dt2.isBefore (new DateMidnight(dt2.getYear(),10,1))) {
                yearOffset = 0;
            }
    
            int weekz = Weeks.weeksBetween(dt.withYear(dt2.getYear()-yearOffset), dt2).getWeeks();
            return(yearz + " " + weekz);
        }
    
        private void test (DateMidnight testDate) {
            System.out.println("For date " + testDate + " years/weeks = " + getWeek(testDate));
        }
    
        private void run() {
            test (new DateMidnight());
            test (new DateMidnight(2010,10,8));
            test (new DateMidnight(2010,9,30));
            test (new DateMidnight(2000,10,1));
        }
    
        public static void main(String[] args) {
            new Main().run();
        }
    }
    

    Which outputs

    For date 2011-07-26T00:00:00.000+02:00 years/weeks = 10 42
    For date 2010-10-08T00:00:00.000+02:00 years/weeks = 10 1
    For date 2010-09-30T00:00:00.000+02:00 years/weeks = 9 52
    For date 2000-10-01T00:00:00.000+02:00 years/weeks = 0 0
    

    Probably a slightly more sophisticated return object would be better….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom UIButton that should look like a UIButtonTypeRoundedRect. In
I was trying to create a custom combobox that inherited from ComboBox, but I've
I'm trying to create Custom UIPickerView, something like below.. : http://img824.imageshack.us/img824/3015/pickercustom.png Example : From
I'm trying to create custom view that shows some info about it: Name, Id,
I'm trying to create custom attributes to my button but I dont know which
I'm trying to create a custom helper like this: # app/controllers/my_controller.rb class MyController <
I'm trying to create custom layout for android. It normally draws on screen, but
I'm trying to create a custom webpart. To implement error handling I would like
I am trying create my custom Style spinner with help of this site. But
I am trying to create my custom view through xml, but the screen does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.