作业:日期比较

时间:2017-09-19 04:06:50

标签: java

我需要创建一个名为comeBefore的布尔方法,该方法检查日期是否在另一个日期之前,而只传递1个变量。

public static void test(Date d1, Date d2)
{
    if (d1.comesBefore(d2))
    {
        System.out.println(d1 + " comes before " + d2);
    }
    else
    {
        System.out.println(d1 + " doesn't come before " + d2);
    }
}

我试图弄清楚如何与仅传递1个变量进行比较。

 d1.comesBefore(d2)

调用此方法

public boolean comesBefore(Date d2)
{

}

我一直在试图弄清楚如何实现这一目标。知道我,它可能非常简单。

提前致谢。

这是完整的代码。

DateDemo.java(无法编辑/更改)

public class DateDemo
{
    public static void test(Date d1, Date d2)
    {
        if (d1.comesBefore(d2))
        {
            System.out.println(d1 + " comes before " + d2);
        }
        else
        {
            System.out.println(d1 + " doesn't come before " + d2);
        }
    }

    public static void main(String[] args)
    {
        test(new Date(3, 23, 1900), new Date(3, 23, 1900));

        test(new Date(3, 23, 1899), new Date(3, 23, 1900));
        test(new Date(2, 23, 1900), new Date(3, 23, 1900));
        test(new Date(3, 22, 1900), new Date(3, 23, 1900));

        test(new Date(3, 27, 1900), new Date(3, 23, 1900));
        test(new Date(5, 23, 1900), new Date(3, 23, 1900));
        test(new Date(3, 22, 1901), new Date(3, 23, 1900));
    }
}

Date.java(我正在创建的类,需要提交到hypergrade)

private String month = "";
private int day, year;

public Date(int date, int day, int year)
{
    if(date == 1)
        this.month = "January";
    else if(date == 2)
        this.month = "February";
    else if(date == 3)
        this.month = "March";
    else if(date == 4)
        this.month = "April";
    else if(date == 5)
        this.month = "May";
    else if(date == 6)
        this.month = "June";
    else if(date == 7)
        this.month = "July";
    else if(date == 8)
        this.month = "August";
    else if(date == 9)
        this.month = "September";
    else if(date == 10)
        this.month = "October";
    else if(date == 11)
        this.month = "November";
    else if(date == 12)
        this.month = "December";
    else
        this.month = "Invalid this.month";

    this.day = day;
    this.year = year;
}

public Date(String month, int day, int year)
{
    this.month = month;
    this.day = day;
    this.year = year;
}

public String toString()
{
    return month + " " + day + ", " + year;
}

public boolean comesBefore(Date d2)
{
    //>>>>>Trying to figure out what goes here<<<<<
}

0 个答案:

没有答案