定义一个日期类 Date,该类对象存放一个日期,可以提供的操作有: void GetDate( )?

定义一个日期类 Date,该类对象存放一个日期,可以提供的操作有: void GetDate( )?,第1张

我给你加了不少合法性检查,使用默认构造函数构造时返回当前日期,你可以再main函数中多测试一下有没有问题,比如闰年二月月末增加一天,12月31号增加一天这些,我就只按照想法来写,没有完全测试过。代码:

Datejava:

import javautilArrays;

import javautilCalendar;

import javautilList;

public class Date {

private int y;

private int m;

private int d;

//定义小月smallM 后续经过判断控制,不在小月中的月份都是大月

private static List<Integer> smallM = ArraysasList(4, 6, 9, 11);

public Date() {//默认构造函数,返回当前日期

Calendar calendar = CalendargetInstance();

thisy = calendarget(CalendarYEAR);

thism = calendarget(CalendarMONTH);

thisd = calendarget(CalendarDAY_OF_MONTH);

}

public Date(int y, int m, int d) throws Exception {

SetDate(y, m, d);

}

public Date(Date date) {

thisy = datey;

thism = datem;

thisd = dated;

}

void GetDate() {//取日期值,格式如“2012 年 2 月 5 日”

Systemoutprintln(thisy + " 年 " + thism + " 月 " + thisd + " 日");

}

int GetYear() {//取年份

return thisy;

}

int GetMonth() {//取月份

return thism;

}

int GetDay() {//取日期

return thisd;

}

void SetDate(int y, int m, int d) throws Exception {//设置日期值

if (y < 0 || m < 0 || d < 0) {

throw new Exception("年、月、日均不能出现负数");

}

if (m > 12) {

throw new Exception("月份不能大于12月");

}

if (m == 2) {//二月特殊处理

if (y % 4 != 0 && d > 28) {

throw new Exception("平年二月不能大于28天");

} else if (y % 4 == 0 && d > 29) {

throw new Exception("闰年二月不能大于29天");

}

} else if (smallMcontains(m)) {//小月

if (d > 30) {

throw new Exception("小月天数不能大于30天");

}

} else {//剩余的都是大月

if (d > 31) {

throw new Exception("大月天数不能大于31天");

}

}

//所有不合法检查判断后,就可以赋值了

thisy = y;

thism = m;

thisd = d;

}

void AddOneDay() {//日期增加一天

if (thism == 2) {//还是先对2月进行特殊处理

if (thisy % 4 == 0) {//闰年

if (thisd == 29) {//闰年的2月29号增加一天为3月1号

thism = 3;

thisd = 1;

return;

}

} else {

if (thisd == 28) {//平年的2月28号增加一天为3月1号

thism = 3;

thisd = 1;

return;

}

}

} else if (smallMcontains(thism)) {//小月的30号增加一天为下月1号

if (thisd == 30) {

thism += 1;

thisd = 1;

return;

}

} else {//其他的都是大月

if (thism == 12) {//12月份特殊处理

if (thisd == 31) {//12月31号增加一天为下一年的1月1号

thisy += 1;

thism = 1;

thisd = 1;

return;

}

} else {

if (thisd == 31) {//大月的31号增加一天为下月1号

thism += 1;

thisd = 1;

return;

}

}

}

//剩下的请款不是月末,号数直接加1即可

thisd += 1;

}

}

测试类:

public class T {

public static void main(String[] args) throws Exception {

Date d1 = new Date(2001, 5, 8);//用所给日期定义一个日期变量

Date d2 = new Date();//定义一个日期对象

Date d3 = new Date(d1);//用已有的日期构造一个新对象

d1GetDate();

d2GetDate();

d3GetDate();

Systemoutprintln("\n增加一天:");

d1AddOneDay();

d2AddOneDay();

d3AddOneDay();

d1GetDate();

d2GetDate();

d3GetDate();

}

}

运行结果:

PEXPIREAT KEY_NAME TIME_IN_MILLISECONDS_IN_UNIX_TIMESTAMP

设置成功返回 1 。 当 key 不存在或者不能为 key 设置过期时间时(比如在低于 213 版本的 Redis 中你尝试更新 key 的过期时间)返回 0 。

例子:

pexpireat redisKey ovar_time

redisKey :是redis 的key值 over_time 是当天的24点

var over_time = (TimergetDayHour(Timernow(), 0, 0, 0) Timer_M_SEC) + TimerOneDay;

Timer_M_SEC = 1000 这里设置的 ms数

TimerOneDay = 2460601000; //86400000

Timernow():当前时间 可以根据不同的语言更换对应的取值方式

/

@Brief: 获取指定时间( 秒数)当天某时候的秒数

@param {Number} timeStampe 时间毫秒数

@param {Number} hour 小时

@param {Number} min 分钟

@param {Number} sec 秒

/

TimergetDayHour = function (timeStampe, hour, min, sec) {

var nowDate = new Date(timeStampe);

nowDatesetHours(hour);

nowDatesetMinutes(min);

nowDatesetSeconds(sec);

return Mathfloor(nowDategetTime() / _M_SEC);

};

#include<stdioh>

#include<iostreamh>

#include "windowsh"

class Date{

private:

int year;

int month;

int day;

int IsCurrentDate(int Year,int Month,int Day);//是不是正确的日期。

public:

Date();

Date(int y,int m,int d);

Date(const Date &other);

void GetDate();

int GetYear();

int GetMonth();

int GetDay();

void SetDate(int y,int m,int d);

void AddOneDay();

};

Date::Date(){//不赋值时,默认当前日期

SYSTEMTIME ct;

    GetLocalTime(&ct);//如果用GetSystemTime(&ct);那么获取的是格林尼治标准时间

    year=ctwYear;

    month=ctwMonth;

    day=ctwDay;

}

Date::Date(int y,int m,int d):year(y),month(m),day(d){

IsCurrentDate(y,m,d);

}

Date::Date(const Date &other){

year=otheryear;

month=othermonth;

day=otherday;

}

void Date::GetDate(){

cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;

}

int Date::GetYear(){

return year;

}

int Date::GetMonth(){

return month;

}

int Date::GetDay(){

return day;

}

void Date::SetDate(int y,int m,int d){

year=y;

month=m;

day=d;

}

void Date::AddOneDay(){

int sign=0;

if ((year%4==0&&year%100!=0)||year%400==0)

{

sign =1;

}

switch (month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

             if(day<31){

day=day+1;

}else{

day=1;

month=month+1;

}

break;

case 4:

case 6:

case 9:

case 11:

if(day<30){

day=day+1;

}else{

day=1;

month=month+1;

}

break;

case 2:

if (sign ==1)

if(day<29){

day=day+1;

}else{

day=1;

month=3;

}

else

if(day<28){

day=day+1;

}else{

day=1;

month=3;

}

break;

case 12:

if(day<30){

day=day+1;

}else{

day=1;

month=1;

year=year+1;

}

break;

default:

break;

}

}

int Date::IsCurrentDate(int year,int month,int day){

int sign=0,dayvalid;

if ((year%4==0&&year%100!=0)||year%400==0)

{

sign =1;

}

if (month>=1&&month<=12)

{

switch (month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

dayvalid = 31;

break;

case 4:

case 6:

case 9:

case 11:

dayvalid = 30;

break;

case 2:

if (sign ==1)

dayvalid = 29;

else

dayvalid = 28;

break;

default:

break;

}

if (!(day >0&&day <=dayvalid))

{

cout<<"日期格式错误"<<endl;

return 2;

}

}

else

{

cout<<"月份格式错误"<<endl;

return 1;

}

cout<<"日期格式正确"<<endl;

return 0;

}

int main(){

Date d;

dAddOneDay();

dGetDate();

Date dt(2014,2,28);

dtAddOneDay();

dtGetDate();

return 0;

}

欢迎分享,转载请注明来源:品搜搜测评网

原文地址:https://pinsoso.cn/shuma/781364.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-08-07
下一篇2023-08-07

随机推荐

  • 天气丹水云系列好用吗

    好用。天气丹套盒里的乳液对于皮肤的维稳效果相当不错,天气丹乳液的质地不厚重,使用起来不会让肌肤产生负担感,保湿力度也很高,适合全肤质所有人群使用。天气丹套盒中还有一盒面霜,天气丹面霜质地比较绵密,可以牢牢锁住之前所使用护肤品中的营养成分。天

    2024-04-15
    50100
  • 日本好用的护手霜有哪些?哪些日本品牌护手霜好用?

    除了脸部肌肤,手部肌肤也是很多人护肤所关注的,手部肌肤有时候比较粗糙,缺乏水分,需要用些护手的产品,护手霜有很多的品牌,日本有许多好用的护肤产品,适合亚洲人的肌肤使用,那么日本好用的护手霜有哪些?哪些日本品牌护手霜好用?1、日本好用的护手霜

    2024-04-15
    48600
  • 补水的精华和美白的精华可以叠加使用吗?

    随着时间的流逝,不经意间就会发现,脸上的细纹又增多了,随之而来的还有毛孔变的粗大等问题。感觉用了很多护肤品,还是不能够掩饰时间在面部留下的痕迹。看过抗皱紧致精华排名榜后,才知道需要一款能够带来紧致肌肤的抗皱紧致精华。但是,哪款精华液好用?真

    2024-04-15
    53000
  • 谷雨光甘草精华效果好吗 谷雨光甘草精华好用吗

    谷雨是个比较有名气的护肤品牌,这个品牌有很多人都喜欢用,那么谷雨光甘草精华效果好吗,谷雨光甘草精华好用吗,下面就来看看吧。谷雨光甘草精华效果好吗 谷雨这款光甘草精华真的超级好用,很保湿完全打破对国货的看法,包装也越来越大气漂亮了,小

    2024-04-15
    46600
  • 妮维雅男士蓝罐怎么区分国产和进口

    妮维雅男士蓝罐是一款非常受欢迎的护肤品,深受许多消费者的喜爱和好评。但是,对于如何区分国产和进口的妮维雅男士蓝罐,我们需要从多个角度进行考虑。我们可以通过产品包装上的标识来进行区分。通常情况下,国产产品会在包装上标注“中国制造”或者“中文标

    2024-04-15
    50200
  • 圣诞跨年送女朋友什么口红比较好?适合圣诞跨年的口红品牌榜单

    圣诞跨年送女朋友什么口红比较好?适合圣诞跨年的口红哪个好?现在的大牌圣诞限定口红每一款都超级好看,非常适合朋友们入手,用来送给自己的对象是再好不过的了,有喜欢的朋友们千万不要错过这些美好的大牌口红礼物啦。1、迪奥(Dior

    2024-04-15
    40200
  • 国外化妆品的生产日期怎么查?

    生产日期即是批号,以下是常见品牌的批号识别方法:1、欧莱雅:例如:FB263代表2005年第263天生产。F代表产地(法国),第二个字母是代表年份,A代表2004年,B代表2005年,C代表2006年,但没有Z,因为Z和2易相混。2、碧欧泉

    2024-04-15
    47400

发表评论

登录后才能评论
保存