oracle查看日期是第幾周

系統當前時間是多少周,就是是今年的第幾個星期 
 
select to_char(sysdate,'ww') ,to_char(sysdate,'iw') from dual;
select to_char(sysdate,'ddd') from dual;
select TRUNC(SYSDATE,'MM') from dual;1)
ww的算法爲每一年1月1日爲第一週開始,date+6爲每一週結尾
例如:20050101爲第一週的第一天,而第一週的最後一天爲20050101+6=20050107  
公式: 每週第一天 :date + 周 * 7 - 7  每週最後一天:date + 周 * 7 - 12)
 
iw的算法爲星期一至星期日算一週,且每一年的第一個星期一爲第一週,
例如:20050101爲星期六,因此用iw的算法是前年的53周,而20050103以後纔是第一週的開始。  
公式: 每週第一天 :next_day(date) + 周 * 7 - 7每週最後一天:next_day(date) + 周 * 7 - 13)
 
其它:  
A、查今天是 "本月" 的第幾周  SELECT TO_CHAR(SYSDATE,'WW') - TO_CHAR(TRUNC(SYSDATE,'MM'),'WW') + 1 AS "weekOfMon" from dual;  或  SELECT TO_CHAR(SYSDATE,'W') AS "weekOfMon" from dual;  
B、查今天是 "今年" 的第幾周  select to_char(sysdate,'ww') from dual;  或  select to_char(sysdate,'iw') from dual;
 
統計哪年哪一個星期的哪一天:
 int year=2011;
        int week=1;
        Calendar calFirstDayOfTheYear = new GregorianCalendar(year,
                Calendar.JANUARY, 1);
            calFirstDayOfTheYear.add(Calendar.DATE, 7 * (week-1));


            int dayOfWeek = calFirstDayOfTheYear.get(Calendar.DAY_OF_WEEK); 


            Calendar calFirstDayInWeek = (Calendar)calFirstDayOfTheYear.clone();
            calFirstDayInWeek.add(Calendar.DATE, 
                calFirstDayOfTheYear.getActualMinimum(Calendar.DAY_OF_WEEK) - dayOfWeek);
            Date firstDayInWeek = calFirstDayInWeek.getTime();
            System.out.println(year + "年第" + week + "個禮拜的第一天是" + ReportDateUtil.getFromatDay().format(firstDayInWeek));


            Calendar calLastDayInWeek = (Calendar)calFirstDayOfTheYear.clone();
            calLastDayInWeek.add(Calendar.DATE, 
                calFirstDayOfTheYear.getActualMaximum(Calendar.DAY_OF_WEEK) - dayOfWeek);
            Date lastDayInWeek = calLastDayInWeek.getTime();
            System.out.println(year + "年第" + week + "個禮拜的最後一天是" + ReportDateUtil.getFromatDay().format(lastDayInWeek));
相關文章
相關標籤/搜索