oracle查看当前日期是第几个星期的方法

编程学习 2021-07-05 16:08www.dzhlxh.cn编程入门
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));

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板