Java——MyDate类

Description

构造日期类MyDate类,包含年月日,提供相应的get和set函数,提供void print()函数打印日期,提供int compare(MyDate d)测试当前对象和参数对象d的早晚,如果早则返回-1,晚则返回1,相等则返回0
在main函数中,读入两个日期对象,输出第一个日期对象的信息,输出两个对象的比较结果

Input

两个日期对象,第一个为当前日期对象的年月日,第二个为待比较日期对象的年月日

Output

当前日期对象的信息,当前对象和待比较日期对象的比较结果

Sample Input

2008 6 12 2009 6 22

Sample Output

6/12/2008 -1
import java.util.Scanner;

public class Main{

    public static void main(String[] argc)

    {

        Scanner scan = new Scanner(System.in);

        int Year = scan.nextInt();

        int Month = scan.nextInt();

        int Day = scan.nextInt();

        

        int AnotherYear = scan.nextInt();

        int AnotherMonth = scan.nextInt();

        int AnotherDay = scan.nextInt();

        scan.close();

        

        MyDate firstDay = new MyDate(Year,Month,Day);

        MyDate secondDay = new MyDate(AnotherYear,AnotherMonth,AnotherDay);

        

        firstDay.print();

        System.out.print(firstDay.compare(secondDay));

    }

    

}

class MyDate{

    private int year;

    private int month;

    private int day;

    

    public MyDate(int year,int month,int day)

    {

        this.year = year;

        this.month = month;

        this.day = day;

    }

    public void setYear(int y)

    {

        year = y;

    }

    public void setMonth(int m)

    {

        month = m;

    }

    public void setDay(int d)

    {

        day = d;

    }

    public int getYear()

    {

        return year;

    }

    public int getMonth()

    {

        return month;

    }

    public int getDay()

    {

        return day;

    }

    

    public void print()

    {

        System.out.print(month+"/"+day+"/"+year+" ");

    }

    public int compare(MyDate d)

    {

        if(d.year > this.year)

        {

            return -1;

        }

        else if(d.year == this.year)

        {

            if(d.month > this.month)

            {

                return -1;

            }

            else if(d.month == this.month)

            {

                if(d.day > this.day)

                {return -1;}

                else if(d.day == this.day)

                {

                    return 0;

                }

                    

            }

                

        }

        return 1;

    }

    

}

猜你喜欢

转载自blog.csdn.net/Yolanda_Salvatore/article/details/82790211
今日推荐