试图找到给定日期的周.
使用下面的代码. [
Java 1.7]
问题:
2012年12月29日=第52周
2012年12月30日=第1周
2013年12月30日=第52周
2013年12月31日=第1周
当年份= 365或366时,它将一年中的一周提供给1.
看起来有些模52发生了.
如何解决这个问题?
import java.util.Calendar;
import java.util.GregorianCalendar;
public class cal2{
public static void main (String[] args) {
Calendar mycal = GregorianCalendar.getInstance();
mycal.setLenient(false);
int year = 2012;
int month = 11; //0=Jan,11=Dec
int date = 29;
mycal.set(year,1);
mycal.setFirstDayOfWeek(mycal.get(mycal.DAY_OF_WEEK));
mycal.set(year,month,date);
System.out.println("n>>>>>>>>WEEK :"+mycal.get(mycal.WEEK_OF_YEAR));
// System.out.println("nDATE :"+mycal);
}
}
来自Docs的
SEE HERE
第一周
Calendar defines a locale-specific seven day week using two
parameters: the first day of the week and the minimal days in first
week (from 1 to 7). These numbers are taken from the locale resource
data when a Calendar is constructed. They may also be specified
explicitly through the methods for setting their values.
When setting or getting the WEEK_OF_MONTH or WEEK_OF_YEAR fields,Calendar must determine the first week of the month or year as a reference point. The first week of a month or year is defined as the earliest seven day period beginning on getFirstDayOfWeek() and containing at least getMinimalDaysInFirstWeek() days of that month or year. Weeks numbered …,-1,0 precede the first week; weeks numbered 2,3,… follow it. Note that the normalized numbering returned by get() may be different. For example,a specific Calendar subclass may designate the week before week 1 of a year as week n of the previous year.