next_day函数用法

在Oracle是提供了next_day求指定日期的下一个日期.



语法 : next_day( date, weekday ) 

date is used to find the next weekday. 
weekday is a day of the week (ie: SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY) 

可用于: 
Oracle 9i, Oracle 10g, Oracle 11g

 

举例:

可以求出当前日期的下个星期一。

select next_day( sysdate, 'MONDAY') from dual;

使用方法并不复杂,主要注意如下:

  1)  当第二个参数传的星期数比现有星期数小的时候,会返回下一个星期的日期;当第二个参数所传的星期数比 现有的星期数大的时候,则会返回本周的相应星期日期。

 

例如:

ps:请注意如下红色字体。

SQL> select sysdate from dual;

SYSDATE
-------------------
2010-12-30 19:30:26

1 row selected.

Elapsed: 00:00:00.01

 

SQL> select next_day(sysdate,'tuesday') from dual; 

NEXT_DAY(SYSDATE,'T
-------------------
2011-01-04 18:57:37

1 row selected.

Elapsed: 00:00:00.01
SQL> 
SQL>  select next_day(sysdate,'MONDAY') from dual;

NEXT_DAY(SYSDATE,'M
-------------------
2011-01-03 18:58:45

1 row selected.

Elapsed: 00:00:00.00
SQL> select next_day(sysdate,'WEDNESDAY') from dual;

NEXT_DAY(SYSDATE,'W
-------------------
2011-01-05 18:59:15

1 row selected.

Elapsed: 00:00:00.00
SQL> select next_day(sysdate,'THURSDAY') from dual;

NEXT_DAY(SYSDATE,'T
-------------------
2011-01-06 18:59:30

1 row selected.

Elapsed: 00:00:00.00
SQL> select next_day(sysdate,'FRIDAY') from dual;

NEXT_DAY(SYSDATE,'F
-------------------
2010-12-31 18:59:43

1 row selected.

Elapsed: 00:00:00.00


SQL> select next_day(sysdate,'SATURDAY') from dual;

NEXT_DAY(SYSDATE,'S
-------------------
2011-01-01 19:00:19

1 row selected.

Elapsed: 00:00:00.01


SQL> select next_day(sysdate,'SUNDAY') from dual;

NEXT_DAY(SYSDATE,'S
-------------------
2011-01-02 19:00:37

1 row selected.

Elapsed: 00:00:00.00

 

2)好的方法

统计上个星期日到上个星期六的总和,

具体写法:

l_begin_date ~ l_end_date:

            NEXT_DAY(SYSDATE-7*2-1,'sunday') ~ NEXT_DAY(SYSDATE-7-2,'saturday')

 

然后再在程序中作判断:

IF l_begin_date>NEXT_DAY(SYSDATE-7*2-1,'sunday')
    --限制统计开始日期必须是周日
    OR TO_CHAR(l_begin_date,'d')<>1
    --限制统计结束日期必须是周六
    OR TO_CHAR(l_end_date,'d')<>7
    --限制开始日期和结束日期时间间隔必须是一周
    OR l_end_date-l_begin_date<>6

THEN
        raise_application_error(-20503,'you enter error parameter');
    END IF;

    --每周一跑数据,其它时候空跑
    IF TO_CHAR(SYSDATE,'d')=2 
    THEN

         *****************

   end IF;

原地址:http://blog.csdn.net/ningjieshuijing/article/details/6107965

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: def next_day(year, month, day): if is_leap_year(year): days_in_month = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] else: days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if day < days_in_month[month-1]: day += 1 else: day = 1 if month == 12: month = 1 year += 1 else: month += 1 return year, month, day答:函数next_day的代码用于获取输入日期的下一天,is_leap_year用于判断给定年份是否为闰年。 ### 回答2: 补充函数`next_day`的代码如下: ```python def is_leap_year(year): if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: return True else: return False def next_day(year, month, day): if month == 2 and is_leap_year(year): if day < 29: day += 1 else: day = 1 month += 1 elif month == 2 and not is_leap_year(year): if day < 28: day += 1 else: day = 1 month += 1 elif month == 12 and day == 31: year += 1 month = 1 day = 1 elif month in [1, 3, 5, 7, 8, 10] and day == 31: month += 1 day = 1 elif month in [4, 6, 9, 11] and day == 30: month += 1 day = 1 else: day += 1 return (year, month, day) ``` 函数`is_leap_year`用于判断给定年份是否为闰年,如果是闰年则返回`True`,否则返回`False`。函数`next_day`接受年份`year`、月份`month`和日期`day`作为输入参数,然后根据输入的日期计算下一天的日期,并以元组`(year, month, day)`的形式返回结果。 在`next_day`函数中,根据每个月的天数来处理日期的递增。对于闰年的二月份,如果当前日期`day`小于29,则日期加1;如果当前日期`day`等于29,则日期重置为1,月份加1。对于非闰年的二月份,如果当前日期`day`小于28,则日期加1,否则日期重置为1,月份加1。对于其他月份,如果当前日期`day`等于月份的最后一天,则日期重置为1,月份加1。如果当前月份是12月且日期是31号,则年份加1,月份和日期重置为1。其他情况下,日期加1。 示例输出: ``` python print(next_day(2020, 2, 28)) # 输出 (2020, 2, 29) print(next_day(2021, 2, 28)) # 输出 (2021, 3, 1) print(next_day(2022, 12, 31)) # 输出 (2023, 1, 1) ``` ### 回答3: 下面是一个用Python编写的函数next_day,用于求给定日期的下一天。其中is_leap_year函数用于判断给定年份是否为闰年。 ```python def is_leap_year(year): if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: return True else: return False def next_day(year, month, day): # 如果是12月31日,则进入下一年的第一天 if month == 12 and day == 31: next_year = year + 1 next_month = 1 next_day = 1 # 如果是非12月31日,则进入当月的下一天 else: # 判断给定月份的天数 if month in [1, 3, 5, 7, 8, 10, 12]: num_days = 31 elif month in [4, 6, 9, 11]: num_days = 30 else: if is_leap_year(year): num_days = 29 else: num_days = 28 # 如果是当月的最后一天,则进入下个月的第一天 if day == num_days: next_year = year next_month = month + 1 next_day = 1 # 如果是非当月的最后一天,则进入当月的下一天 else: next_year = year next_month = month next_day = day + 1 return next_year, next_month, next_day ``` 注意:这个函数仅仅根据给定的年、月、日求出下一天的日期,并没有做错误处理。例如,输入的日期不存在或者不合法,函数也会返回一个日期。 使用方法示例: ```python next_year, next_month, next_day = next_day(2021, 12, 31) print(next_year, next_month, next_day) ``` 以上代码的输出结果为: ``` 2022 1 1 ``` 表示给定日期为2021年12月31日,下一天的日期为2022年1月1日。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值