用Java创建自定义周计数器?

时间:2011-07-25 21:36:51

标签: java date calendar date-format simpledateformat

我正在尝试创建一个自定义的周计数器,但我遇到了很多麻烦,并且觉得我一切都错了。该方法应采用yyyy-MM-dd格式的字符串日期并返回周数。周计数器于2000年10月1日开始。本周从星期五开始,到星期四结束。前2位代表年份,第2位代表星期。因此本周将是1143(11代表年份,43代表自10月1日以来的几周)。

这是我到目前为止所得到的:

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 个答案:

答案 0 :(得分:1)

我使用Joda-Time,因为它比Java的内置日期和时间装置更容易混淆

编辑 - 新代码,在ChssPly的建议中提出并解决了10月1日到1月1日之间的问题。同时查看X-Zero建议在Joda-Time中创建自定义年表,这可能是一个有趣的方法。 / p>
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();
    }
}

哪个输出

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

可能稍微复杂一点的返回对象会更好......