1、方法一
public static void main(String[] args)
Calendar cal = Calendar.getInstance();
int week = cal.get(cal.WEEK_OF_YEAR);
System.out.println("今天是今年的第" + week + "周;");
}
2、方法二:
public static void main(String[] args)
{
Date date = new Date();
System.out.println(date.getTime());// 1493690652008
long time1 = date.getTime();
date.setMonth(0);
date.setDate(1);
long time2 = date.getTime();
long time = time1 - time2;
System.out.println(date);// Sun Jan 01 10:06:02 CST 2017
time = (time1 - time2) / 86400000 / 7 + 1;
System.out.println("今天是本年的第" + time + "周");
}
控制台输出:
今天是今年的第18周;
jsp方法:
<script type="text/javascript>
function getWeekNumOfYear(dateStr){ // dateStr类型为2017-04-26
var time,week;
var checkDate=new Date(Date.parse(dateStr.replace(/-/g,"/")));
checkDate.setDate(checkDate.getDate()+4-(checkDate.getDay() || 7));
time=checkDate.getTime();
checkDate.setMonth(0);
checkDate.setDate(1);
week=Math.floor(Math.round(time-checkDate)/86400000/7)+1;
return week;
}
</script>