日期计算函数:已知当前日期,求过了一段时间后的日期。

2024-08-28 11:58

本文主要是介绍日期计算函数:已知当前日期,求过了一段时间后的日期。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

函数的功能是,根据当前日期,计算过了一段时间后的日期。

1. 流程图:

这里写图片描述

2.函数实现:
    /*** Method called to check if the date is valid. * short duration: unit is day. * short[] curData: current date, format is YYMMDD* byte [] expData: store the expected date, format is YYMMDD* Return value: false if fail, else succeed.*/private static boolean vGetExpectDate(short[] curDate, short duration, short [] expDate){short year;short month;short day;short days_left;short day_add = 0;// Assign the real date to year, month, day and houryear = curDate[0];month = curDate[1];day = curDate[2];day_add = duration;// days add to yearwhile(day_add > (days_left = sDaysToNextYear(year, month, day))){// come to the 1st day in 1st month of the next year.year++;month = 1;day = 1;day_add -= days_left;}// days add to monthwhile(day_add > (days_left = sDaysToNextMonth(year, month, day))){month++;day = 1;day_add -= days_left;}// days add to dayday += day_add;// store the expected date into the workbuf.expDate[0] = year;expDate[1] = month;expDate[2] = day;return true;}private static short sDaysToNextYear(byte year, byte month, byte day){byte days_Feb;byte days_month;short ret;switch(month){case 1:case 3:case 5:case 7:case 8:case 10:case 12:days_month = 31;break;case 4:case 6:case 9:case 11:days_month = 30;break;case 2:// Here assumes the year is 20xx, so if the year is leap or not depends on the last byte xx.if(0 == (year % 4))days_month = 29;elsedays_month = 28;break;default:break;}ret = days_month - day + 1;month++;switch(month){case 2:ret += days_Feb;case 3:ret += 31;case 4:ret += 30;case 5:ret += 31;case 6:ret += 30;case 7:ret += 31;case 8:ret += 31;case 9:ret += 30;case 10:ret += 31;case 11:ret += 30;case 12:ret += 31;default:break;}return ret;}private static short sDaysToNextMonth(byte year, byte month, byte day){byte days_month;switch(month){case 1:days_month = 31;break;case 2:// Here assumes the year is 20xx, so if the year is leap or not depends on the last byte xx.if(0 == (year % 4))days_month = 29;elsedays_month = 28;break;case 3:days_month = 31;break;case 4:days_month = 30;break;case 5:days_month = 31;break;case 6:days_month = 30;break;case 7:days_month = 31;break;case 8:days_month = 31;break;case 9:days_month = 30;break;case 10:days_month = 31;break;case 11:days_month = 30;break;case 12:days_month = 31;break;default:break;}return (days_month - day + 1);}

这篇关于日期计算函数:已知当前日期,求过了一段时间后的日期。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1114703

相关文章

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分