I need to create a boolean method called comesBefore that checks to see if a date comes before another date while only passing 1 variable.
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);
}
}
I'm trying to figure out how to do a comparison with only 1 variable is being passed.
d1.comesBefore(d2)
Calls this method
public boolean comesBefore(Date d2)
{
}
I've been wracking my brain trying to figure out how to accomplish this. And knowing me, it's probably something incredibly simple.
Thanks in advance.
Here is the full code.
DateDemo.java (cannot be edited/changed)
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 (class I am creating and need to submit to 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<<<<<
}