C++判断时间格式满足yyyy-mm-dd-hh:mm与时间1小于时间2
2017-01-05 18:11
686 查看
C++判断时间格式满足yyyy-mm-dd-hh:mm与时间1小于时间2
#include <iostream> #include <sstream>//调用istringstream需包含 using namespace std; int stringToInt(string str)/*字符串转int*/ { int result; istringstream is(str); is >> result; return result; } bool CheckTime(string time)/*时间满足yyyy-mm-dd-hh:mm格式*/ { bool judge = false; /*符合字数为16*/ if(time.length()!=16) return judge; /*符合年>2017*/ int i; string s; for(i=0;i<4;i++) s += time[i]; int year = stringToInt(s); if(year<2017) return judge; /*符合01<=月<=12*/ s = "";//置空 s += time[5]; s += time[6]; int month = stringToInt(s); if(month<1 || month>12) return judge; /*符合01<=日<=31*/ s = "";//置空 s += time[8]; s += time[9]; int day = string 4000 ToInt(s); if(day<1 || day>31) return judge; /*符合00<=时<=24*/ s = "";//置空 s += time[11]; s += time[12]; int hour = stringToInt(s); if(hour<0 || hour>=24) return judge; /*符合00<=分<=60*/ s = "";//置空 s += time[14]; s += time[15]; int minute = stringToInt(s); if(minute<0 || minute>=60) return judge; /*符合横杆*/ static int dash[3]={4,7,10}; for(i=0;i<3;i++) if(time[dash[i]]!='-')//不为横杆 return judge; /*符合冒号*/ if(time[13]!=':') return judge; /*全部匹配*/ judge = true; return judge; } bool CheckTime2(string time1,string time2)/*时间1<时间2*/ { bool judge = false; /*年*/ int i; string s1,s2; for(i=0;i<4;i++) { s1 += time1[i]; s2 += time2[i]; } int year1 = stringToInt(s1); int year2 = stringToInt(s2); /*月*/ s1 = "";//置空 s2 = "";//置空 s1 += time1[5]; s1 += time1[6]; s2 += time2[5]; s2 += time2[6]; int month1 = stringToInt(s1); int month2 = stringToInt(s2); /*日*/ s1 = "";//置空 s2 = "";//置空 s1 += time1[8]; s1 += time1[9]; s2 += time2[8]; s2 += time2[9]; int day1 = stringToInt(s1); int day2 = stringToInt(s2); /*时*/ s1 = "";//置空 s2 = "";//置空 s1 += time1[11]; s1 += time1[12]; s2 += time2[11]; s2 += time2[12]; int hour1 = stringToInt(s1); int hour2 = stringToInt(s2); /*分*/ s1 = "";//置空 s2 = "";//置空 s1 += time1[14]; s1 += time1[15]; s2 += time2[14]; s2 += time2[15]; int minute1 = stringToInt(s1); int minute2 = stringToInt(s2); if(year1<year2) { return true; } else { if(year1==year2) { if(month1<month2) { return true; } else { if(month1==month2) { if(day1<day2) { return true; } else { if(day1==day2) { if(hour1<hour2) { return true; } else { if(hour1==hour2) { if(minute1<minute2) { return true; } else { return false; } } else { return false; } } } else { return false; } } } else { return false; } } } else { return false; } } return judge; } int main() { cout << CheckTime("2017-01-28-08:05") << "\t符合yyyy-mm-dd-hh:mm格式" << endl; cout << CheckTime("2017-01-28-08:000") << "\t不符合字数" << endl; cout << CheckTime("2000-01-28-08:05") << "\t不符合年>2017" << endl; cout << CheckTime("2017-00-28-08:05") << "\t不符合01<=月<=12" << endl; cout << CheckTime("2017-01-00-08:05") << "\t不符合01<=日<=31" << endl; cout << CheckTime("2017-01-28-99:05") << "\t不符合00<=时<=24" << endl; cout << CheckTime("2017-01-28-08:99") << "\t不符合00<=分<=60" << endl; cout << CheckTime("2017-01-28.08:05") << "\t不符合横杆" << endl; cout << CheckTime("2017-01-28-08.05") << "\t不符合冒号" << endl; cout << endl << endl << endl; cout << CheckTime2("2017-01-28-08:05","2017-01-28-08:06") << "\t符合时间1<时间2" << endl; cout << CheckTime2("2018-01-28-08:05","2017-01-28-11:30") << "\t不符合年1<年2" << endl; cout << CheckTime2("2017-12-28-08:05","2017-01-28-11:30") << "\t不符合月1<月2" << endl; cout << CheckTime2("2017-01-31-08:05","2017-01-28-11:30") << "\t不符合日1<日2" << endl; cout << CheckTime2("2017-01-28-12:05","2017-01-28-11:30") << "\t不符合时1<时2" << endl; cout << CheckTime2("2017-01-28-11:30","2017-01-28-11:30") << "\t不符合分1<分2" << endl; return 0; }
相关文章推荐
- <fmt:formatDate value="${ abc}" pattern="hh:mma, MM/dd/yyyy" /> jstl 时间data格式
- 判断时间大小 yyyy-MM-dd 格式
- JAVA判断时间格式为 "YYYY-MM-DD"
- 解析形如yyyy-MM-dd'T'HH:mm:ss.SSS'Z'的时间格式
- 时间戳格式化转换为日期格式,date.Format('yyyy/MM/dd hh:mm:ss')
- java判断时间格式 //格式必须为“YYYY-MM-dd”
- java判断时间格式 //格式必须为“YYYY-MM-dd”
- dd/MM/yyyy:HH:mm:ss +0800时间格式的转换
- 判断是否为YYYY/MM/DD格式的时间
- [乐意黎原创] 时间格式 yyyy-MM-dd'T'HH:mm:ss.SSSZ 中的T和Z表示什么
- 将字符串"yyyy-MM-dd'T'hh:mm:ss"转换其他时间格式
- js判断两个yyyy-MM-dd格式的日期是否在同一周内
- 这里是判断YYYY-MM-DD这种格式的,基本上把闰年和2月等的情况都考虑进去了,不过我已经忘了在哪里找到的。(转)
- WinForm 中要求在textBox中输入的内容必须是日期格式yyyy-mm-dd,判断用户输入是否有效!
- asp.net 页面如何将Eval中的时间显示为“yyyy-MM-dd ” 格式
- 获取系统时间 yyyy-MM-dd hh:mm:ss
- 得到 yyyy/mm/dd 格式时间
- 获取 yyyy-mm-dd 格式的 Date 时间类型
- 绑定时间格式yyyy-mm-dd
- C#将格式为yyyyMMdd的时间转化为yyyy-MM-dd类型