Java根据日期得到当月第几周

请给下注释,谢谢
2024-11-01 07:15:46
推荐回答(2个)
回答1:

Calendar c_begin = Calendar.getInstance();
Calendar c_end = Calendar.getInstance();
c_begin.set(2010, 3, 1); //Calendar的月从0-11,所以4月是3.
c_end.set(2010, 4, 20); //Calendar的月从0-11,所以5月是4.

int count = 0;
while(c_begin.before(c_end)){
//System.out.println(new java.sql.Date(c_begin.getTime().getTime()));
if(c_begin.get(Calendar.DAY_OF_WEEK)==Calendar.MONDAY){
count++;
}
if(count == 2){
System.out.println(c_begin.get(Calendar.YEAR)+"年"+(c_begin.get(Calendar.MONTH)+1)+"月"+c_begin.get(Calendar.DATE)+"日");
break;
}

c_begin.add(Calendar.DAY_OF_YEAR, 1);
}

可以袜含这样试试,应告亮笑该键巧能理解吧。如有不懂再问我

回答2:

String dateString = "盯亩2013-07-28"拆则纯;

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"旅咐);
Date date = sdf.parse(dateString);

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
System.out.println(weekOfMonth);