java 如何找到一个月中的第几周

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12528537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:15:22  来源:igfitidea点击:

How to find week of the month

java

提问by user1686408

I like to know which week of the month a particular day falls. For example 20-Sep-2012 falls on 4th week of September but the below code displays it as 3 which is not correct. The system is dividing the days by 7 and returning the output and which is not I require. I have searched in Joda API's but not able to find the solution. Please let me know is there any way to figure out the week of a month, a day falls

我想知道某个特定日期是一个月的哪一周。例如 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);

回答by Keppil

This is due to two reasons:

这是由于两个原因:

The first one is this (from the API):

第一个是这个(来自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

一个月或一年的第一周定义为从 getFirstDayOfWeek() 开始并至少包含 getMinimalDaysInFirstWeek() 天的最早 7 天时间段

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

这个默认值会有所不同(我的是 4),但您可以将其设置为您的首选值

Calendar.setMinimalDaysInFirstWeek()

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:

第二个原因是@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);
}

This prints

这打印

Week of Month :4

回答by Timmy

Month is zero-based. So ca1.set(2012,9,20) is actually setting the calendar to October.

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

回答by AnarchoEnte

To get sure the right month is set try using the month-constants provided by the Calendar-Class.

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

回答by Jason K.

For those working with Joda time, this is what I am using:

对于那些使用 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;
}

回答by kondra p

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