Java中通过日期类获取某个日期是当月的第几周,如今天(2017-12-19),应该返回4 10C

1、用Java新增的java.time.LocalDate
LocalDate.now().get(ChronoField.ALIGNED_WEEK_OF_MONTH); // 3

2、用java.util.Calendar
Calendar.getInstance().get(Calendar.DAY_OF_WEEK_IN_MONTH); // 3

以上两种方法返回的都是3,但是我看日历今天(2017-12-19)明明是12月的第4周才对啊!!!

查阅官方文档:
For example, in a calendar systems with a seven day week, the first aligned-week-of-year starts on day-of-year 1, the second aligned-week starts on day-of-year 8, and so on. Thus, day-of-year values 1 to 7 are in aligned-week 1, while day-of-year values 8 to 14 are in aligned-week 2, and so on.

1到7为第一周,8到14为第二周......扑街,感觉就是除以7再向上取整啊!

请问:**_按照我的计算方式_,有大神知道Java中有自带的API直接获取周数的吗?或者说有什么方法可以很机智快捷地算出当天对应的周数吗?**

9个回答

public static int getWeek(String str) throws Exception{
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//第几周
int week = calendar.get(Calendar.WEEK_OF_MONTH);
//第几天,从周日开始
int day = calendar.get(Calendar.DAY_OF_WEEK);
return week;
}

Calendar calendar = Calendar.getInstance();
int weekNum = calendar.get(Calendar.WEEK_OF_MONTH);
System.out.println(weekNum);


int week = calendar.get(Calendar.WEEK_OF_MONTH);

 int day_of_week_in_month = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH); //某月中第几周,
按这个月1号算,1号起就是第1周,8号起就是第2周.以月份天数为标准 
int week_of_month = calendar.get(Calendar.WEEK_OF_MONTH);//日历式的第几周
(例如今天是8-21,是八月的第四周)

如果你要取某个日期是当月的第几周,应该用的是Calendar.WEEK_OF_MONTH。
你现在用的Calendar.DAY_OF_WEEK_IN_MONTH是被IN_MONTH限制的,1-7号第一周以此类推。

你用的Calendar.DAY_OF_WEEK_IN_MONTH返回:这周的第几天,应该用WEEK_OF_MONTH

图片说明

Calendar的自己写把,LocalDate的参考如下:

设置了每个月从1号开始,一周七天,然后28号之后就是第五周了。

 import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.WeekFields;

/**
 * 
 * 
 * @author puruidong
 *
 */
public class Main {



    public static void main(String[] args)   {
        /*
         * now.withDayOfMonth(1).getDayOfWeek()
         * 是先获取一个月的第一天,然后获取这一天是星期几。
         * 
         * 一周7天,所以28号之后是第五周也就是下个月的第一周了.
         * 
         * 
         */
        LocalDate now = LocalDate.now();
        System.out.println(LocalDate.of(2017, 12, 7).get(WeekFields.of(now.withDayOfMonth(1).getDayOfWeek(),7).weekOfMonth()));
        System.out.println(LocalDate.of(2017, 12, 13).get(WeekFields.of(now.withDayOfMonth(1).getDayOfWeek(),7).weekOfMonth()));
        System.out.println(LocalDate.now().get(WeekFields.of(DayOfWeek.MONDAY,7).weekOfMonth()));
        System.out.println(LocalDate.of(2017, 12, 22).get(WeekFields.of(now.withDayOfMonth(1).getDayOfWeek(),7).weekOfMonth()));
        System.out.println(LocalDate.of(2017, 12, 28).get(WeekFields.of(now.withDayOfMonth(1).getDayOfWeek(),7).weekOfMonth()));
        System.out.println(LocalDate.of(2017, 12, 29).get(WeekFields.of(now.withDayOfMonth(1).getDayOfWeek(),7).weekOfMonth()));

    }

}

用 calendar.get(calendar.WEEK_OF_MONTH)
图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐