Mike
Mike

Reputation: 2339

Creating a custom week counter in Java?

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;
}

Upvotes: 0

Views: 607

Answers (1)

fvu
fvu

Reputation: 32953

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

Upvotes: 1

Related Questions