简体   繁体   English

使用java.util.Calendar获取Java下一周的Java

[英]Get next week of year in Java with java.util.Calendar

I'd like to have a function which returns the next week and year given a week and year. 我希望有一个函数可以返回下周和一年给出的一周和一年。 It looks something like: 它看起来像:

public static int[] getNextWeek(int year, int week) {
    Calendar c = Calendar.getInstance(); // I'm Locale.US and TimeZone "America/New_York"
    c.clear();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.WEEK_OF_YEAR, week);

    c.add(Calendar.WEEK_OF_YEAR, 1);

    return new int[] {c.get(Calendar.YEAR), c.get(Calendar.WEEK_OF_YEAR)}
}

This sometimes does not work around year boundaries. 这有时不适用于年份边界。 It seems to depend on what day you invoke it and for what parameters you use! 它似乎取决于您调用它的日期以及您使用的参数! For example, if I invoke it with year 2012 and week 52 then I expect the result to be year 2013 and week 1. If you invoke it today (Tuesday July 17 2012) it works. 例如,如果我在2012年和第52周调用它,那么我预计结果将是2013年和第1周。如果您今天(2012年7月17日星期二)调用它,它可以正常工作。 If you set the day of week to yesterday it does not; 如果您将星期几设置为昨天则不会; and oddly results in year 2012 week 1. Weird. 奇怪的结果在2012年的第1周。奇怪。 What is going on? 到底是怎么回事? It appears to relate to the day of the week because it doesn't work if invoked with SUNDAY or MONDAY, which are the last two days of 2012! 它似乎与星期几有关,因为如果使用SUNDAY或MONDAY(2012年的最后两天)调用它,它将无效! If I set the day of the week to the last day of the week the function seems to work; 如果我将星期几设置为一周的最后一天,则该功能似乎有效; before calling Calendar.add() I do: 在调用Calendar.add()之前我做了:

    // Must set to last day of week; initially set to first day due to API
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // SUNDAY
    c.add(Calendar.DAY_OF_WEEK, 6); // Must roll forward not backwards

There doesn't seem to be any weirdness like this if I create a getPreviousWeek method. 如果我创建一个getPreviousWeek方法,似乎没有像这样的任何古怪。 Is this a java.util.Calendar bug? 这是一个java.util.Calendar错误吗? Next time I guess I'll use Joda time! 下次我想我会用Joda时间!

Have you considered the fact that ISO8601 week algorithm considers the first week of the year to be the one that has at least 4 days fall within that year? 您是否考虑过ISO8601周算法认为一年中的第一周是在该年内至少有4天的那一周?

So if the Jan 1 is on thurdsay, that week is actually considered week 52 of the previous year, not week one of the current year. 因此,如果1月1日是星期六,那个星期实际上被认为是上一年的第52周,而不是当年的第1周。

You might want to at least consider joda-time for this kind of calculation, as it has proper handling of the ISO standard. 您可能希望至少考虑joda-time进行此类计算,因为它正确处理ISO标准。

    LocalDate ld = new LocalDate();
    ld = ld.withWeekOfWeekyear(29);
    ld = ld.withWeekyear(2012);

    System.out.println(ld.getWeekOfWeekyear());
    System.out.println(ld.getWeekyear());

    // 29
    // 2012


    System.out.println(ld.plusWeeks(1).getWeekOfWeekyear());
    System.out.println(ld.plusWeeks(1).getWeekyear());
    // 30
    // 2012

And this will work across year boundaries. 这将跨越年界。

Just do an explicit check to see if there's a rollover. 只是做一个明确的检查,看看是否有翻滚。

return new int[] {c.get(Calendar.WEEK_OF_YEAR) == 1 ? c.get(Calendar.YEAR) + 1 :
    c.get(Calendar.YEAR), c.get(Calendar.WEEK_OF_YEAR)};

As far as I can tell, the reason why WEEK_OF_YEAR doesn't add correctly is because it's not an actual property of Calendar like SECOND , MINUTE , MONTH , DAY , or YEAR . 据我所知, WEEK_OF_YEAR没有正确添加的原因是因为它不是Calendar的实际属性,如SECONDMINUTEMONTHDAYYEAR It's a derived property like WEEK_OF_MONTH . 它是一个派生属性,如WEEK_OF_MONTH One way to get around this is to use c.add(Calendar.DAY, 7) to add exactly 7 days instead of incrementing WEEK_OF_YEAR . 解决此问题的一种方法是使用c.add(Calendar.DAY, 7)准确添加7天而不是增加WEEK_OF_YEAR

At this point I'm going to assume this really is a bug. 在这一点上,我将假设这真的是一个错误。 I've filled a bug report with Oracle here: 我在这里填写了一份关于Oracle的错误报告:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7197761 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7197761

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM