我想知道某个特定日期的哪个星期。例如,2012年9月20日是9月的第4周,但下面的代码显示为3,这是不正确的。系统将天数除以7并返回输出,这不是我要求的。我在Joda API中搜索过,但无法找到解决方案。请让我知道有没有办法找出一个月的一周,一天下降
Calendar ca1 = Calendar.getInstance();
ca1.set(2012,9,20);
int wk=ca1.get(Calendar.WEEK_OF_MONTH);
System.out.println("Week of Month :"+wk);
答案 0 :(得分:7)
这是由于两个原因:
第一个是(来自API):
一个月或一年的第一周被定义为从getFirstDayOfWeek()开始并且至少包含的最早的七天时段 getMinimalDaysInFirstWeek()天
此默认值有所不同(我的是4),但您可以使用
将其设置为首选值Calendar.setMinimalDaysInFirstWeek()
第二个原因是@Timmy在答案中提出了这个问题。您需要执行这两项更改才能使代码正常工作。完整的工作示例:
public static void main(String[] args) {
Calendar ca1 = Calendar.getInstance();
ca1.set(2012, Calendar.SEPTEMBER, 20);
ca1.setMinimalDaysInFirstWeek(1);
int wk = ca1.get(Calendar.WEEK_OF_MONTH);
System.out.println("Week of Month :" + wk);
}
打印
Week of Month :4
答案 1 :(得分:3)
月份从零开始。所以ca1.set(2012,9,20)实际上是将日历设置为10月。
答案 2 :(得分:1)
要确保设置正确的月份,请尝试使用Calendar-Class提供的月份常量。
答案 3 :(得分:0)
对于那些使用Joda时间的人来说,这就是我正在使用的:
/**
* For a week starting from Sunday, get the week of the month assuming a
* valid week is any week containing at least one day.
*
* 0=Last,1=First,2=Second...5=Fifth
*/
private int getWeekOfMonth( MutableDateTime calendar )
{
long time = calendar.getMillis();
int dayOfMonth = calendar.getDayOfMonth();
int firstWeekday;
int lastDateOfMonth;
int week;
int weeksInMonth;
calendar.setDayOfMonth( 1 );
firstWeekday = calendar.getDayOfWeek();
if (firstWeekday == 7)
{
firstWeekday = 0;
}
calendar.setHourOfDay( 0 );
calendar.addMonths( 1 );
calendar.addDays( -1 );
lastDateOfMonth = calendar.getDayOfMonth();
weeksInMonth = (int)Math.ceil( 1.0*(lastDateOfMonth + firstWeekday)/7 );
week = (byte)(1 + (dayOfMonth + firstWeekday - 1)/7);
week = (week == weeksInMonth)?0:week;
calendar.setMillis( time );
return week;
}
答案 4 :(得分:0)
这个问题我来晚了,但是我觉得完整的答案缺失了,也就是说,根据Locale
,周的解释方式可能会有很大不同。
这个问题似乎需要针对美国(或类似的Locale
)进行设置,在美国,第一周的最少天数为1,星期日为一周的第一天。
问题和所有答案均采用默认的Calendar
实例,该实例在第一周的最少天数为4天,而星期一为一周的第一天。
一个简单的演示程序显示了这一点:
public static void main(String[] args) {
{
System.out.println("--- ISO ---");
Calendar calendar = Calendar.getInstance();
System.out.println("First day of week : " + calendar.getFirstDayOfWeek());
System.out.println("Minimal days in 1st week : " + calendar.getMinimalDaysInFirstWeek());
calendar.set(2012, Calendar.SEPTEMBER, 20);
int wk = calendar.get(Calendar.WEEK_OF_MONTH);
System.out.println("Week of Month : " + wk);
}
{
System.out.println("--- USA ---");
Calendar calendar = Calendar.getInstance(Locale.US);
System.out.println("First day of week : " + calendar.getFirstDayOfWeek());
System.out.println("Minimal days in 1st week : " + calendar.getMinimalDaysInFirstWeek());
calendar.set(2012, Calendar.SEPTEMBER, 20);
int wk = calendar.get(Calendar.WEEK_OF_MONTH);
System.out.println("Week of Month : " + wk);
}
}
这将产生此输出:
--- ISO ---
First day of week : 2
Minimal days in 1st week : 4
Week of Month : 3
--- USA ---
First day of week : 1
Minimal days in 1st week : 1
Week of Month : 4
结论:
无需手动设置第一周或一周中第一天的最少天数。只要确保您使用的是正确的Locale
。
额外
Joda时间仅支持ISO周。 由于有了Java 8和新的time API,您可以像这样:
LocalDate localDate = LocalDate.of(2012, 9, 20);
TemporalField usWeekOfMonth = WeekFields.of(Locale.US).weekOfMonth();
TemporalField isoWeekOfMonth = WeekFields.ISO.weekOfMonth();
System.out.println("USA week of month " + usWeekOfMonth.getFrom(localDate));
System.out.println("ISO week of month " + usWeekOfMonth.getFrom(localDate));
输出:
USA week of month 4
ISO week of month 4
格式化程序甚至还提供支持:
DateTimeFormatter usDateTimeFormatter = DateTimeFormatter.ofPattern("W/MM")
.withLocale(Locale.US);
System.out.println("USA formatter : " + usDateTimeFormatter.format(localDate));
DateTimeFormatter isoDateTimeFormatter = DateTimeFormatter.ofPattern("W/MM");
System.out.println("ISO formatter : " + isoDateTimeFormatter.format(localDate));
输出:
USA formatter : 4/09
ISO formatter : 3/09
答案 5 :(得分:-1)
class test {
public static void main(String[] args){
Calendar cal = Calendar.getInstance();
cal.set(2016, Calendar.AUGUST, 01);
printDayOFWeek(cal, Calendar.MONDAY, 5);//prints monday on 5th week
}
}
private static void printDayOFWeek(Calendar cal, int day, int whatweek) {
cal.set(Calendar.DAY_OF_WEEK, day);//day = Mon, tue, wed,..etc
cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1); //-1 return last week
Date last = cal.getTime();
cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, whatweek);
Date one = cal.getTime();
if(one.before(last) || one.compareTo(last) ==0)
{
System.out.println(whatweek +"WEEK" + cal.getTime());
}
}