6

我想知道某天是一个月中的哪一周。例如 20-Sep-2012 落在 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);
4

6 回答 6

8

This is due to two reasons:

The first one is this (from the API):

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

The default value for this varies (mine was 4), but you can set this to your preferred value with

Calendar.setMinimalDaysInFirstWeek()

The second reason is the one @Timmy brought up in his answer. You need to perform both changes for your code to work. Complete working example:

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

This prints

Week of Month :4
于 2012-09-21T10:15:44.240 回答
3

月份是从零开始的。所以 ca1.set(2012,9,20) 实际上是将日历设置为 10 月。

于 2012-09-21T10:17:01.807 回答
1

要确保设置正确的月份,请尝试使用 Calendar-Class 提供的月份常量。

于 2012-09-21T10:19:49.573 回答
0

对于那些使用 Joda time 的人来说,这就是我正在使用的:

/**
 * 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;
}
于 2016-07-23T02:57:06.867 回答
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 time 仅支持 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
于 2020-07-06T08:15:50.053 回答
-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());
    }
}
于 2016-07-21T13:10:28.947 回答