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

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

相关文章

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

Java获取当前时间String类型和Date类型方式

《Java获取当前时间String类型和Date类型方式》:本文主要介绍Java获取当前时间String类型和Date类型方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录Java获取当前时间String和Date类型String类型和Date类型输出结果总结Java获取

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

MySQL字符串常用函数详解

《MySQL字符串常用函数详解》本文给大家介绍MySQL字符串常用函数,本文结合实例代码给大家介绍的非常详细,对大家学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql字符串常用函数一、获取二、大小写转换三、拼接四、截取五、比较、反转、替换六、去空白、填充MySQL字符串常用函数一、

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:计算字符串