0

I spent hours thinking about this. I need to write 2 methods. before and after. after can use ONLY the before method. I wrote the code:

public boolean before(Date d) 
{
    if(getYear()<d.getYear() || (getYear()==d.getYear() && getMonth()<d.getMonth()) 
       || (getYear()==d.getYear() && getMonth()==d.getMonth() && getDay()<d.getDay()))
    {
     return true;
    }
return false;
}

and then:

public boolean after(Date d)
{
   if(!before(d))
   {
      return true;
   }
return false;
}

but the problem is that for the same date before returns false and after returns true and i need them both to return false. My teacher told me there is a way to do it but i really don't know how and i spent tooooo much time on this. Is there a way?

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
Edmond
  • 43
  • 1
  • 10
  • 1
    [`java.util.Date.before(Date when)`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) – OneCricketeer Nov 24 '16 at 02:37
  • 1
    I am not allowed to use it – Edmond Nov 24 '16 at 02:40
  • can you not just check if day/month/year are equal to getDay/getMonth/getYear and return the proper value? – Mogzol Nov 24 '16 at 02:42
  • @Mogzol what do you mean? – Edmond Nov 24 '16 at 02:47
  • @Edmond, kindly explain this _"for the same date before returns false and after returns true and i need them both to return false"_ with an example –  Nov 24 '16 at 02:48
  • @Edmond What I mean is, in `after`, can you just change your `if` statement to `if (!before(day, month, year) && (getYear() != year && getMonth() != month && getDay() != day)) { ...` – Mogzol Nov 24 '16 at 02:50
  • @Arvind Date d = new Date(23, 11, 2016); System.out.println(d.before(23, 11, 2016)); <-- returns false, as it should System.out.println(d.after(23, 11, 2016)); <-- returns true, as it should not – Edmond Nov 24 '16 at 02:54
  • @Mogzol no. after can use only 1 method and its before – Edmond Nov 24 '16 at 02:56
  • Are you trying to compare a `Date d` with the current date?? If not, shouldn't your `before()` and `after()` methods take two date parameters to check the conditions?? Then you can make use of `compareTo()` method present in `Date` class – Shyam Baitmangalkar Nov 24 '16 at 05:49

4 Answers4

0

Assuming day is as precise as your dates get, you could do

public boolean after(int day, int month, int year)
{
   if(!before(day + 1, month, year))
   {
      return true;
   }
   return false;
}

Also, since you are just returning a boolean, that can be shortened to

public boolean after(int day, int month, int year)
{
   return !before(day + 1, month, year);
}
Mogzol
  • 1,405
  • 1
  • 12
  • 18
0

OK this is your simple logic as @Mogzol mentioned (in your case instead of int -> date),

static int j = 1;

public static void main(String[] args) {
    System.out.println("EQUAL : isBefore  = " + before(1) + " & isAfter = " + after(1));
    System.out.println("BEFORE : isBefore = " + before(0) + " & isAfter = " + after(0));
    System.out.println("AFTER : isBefore = " + before(2) + " & isAfter = " + after(2));
}

public static boolean before(int i) {
    return i - j > 0;
}

public static boolean after(int i) {
    return !before(i+1);
}

EQUAL : isBefore = false & isAfter = false

BEFORE : isBefore = false & isAfter = true

AFTER : isBefore = true & isAfter = false

the complexity here is you must build a logic to always keep your date valid like in your 31 + 1 case,

for which you can use any of the idea mentoned here how to increment a day by 1

Simplest one is ,

public boolean after(Date d)
{
   return !before(new Date (d.getTime()+24*60*60*1000));
}
Community
  • 1
  • 1
Sagar
  • 818
  • 1
  • 6
  • 13
0

I found the right code for the after method. the before method remains the same.

public boolean after(Date other)
{
  if(this.before(other)==true)
  {
     return false;
  }
  else if(this.before(other)==other.before(this))  //same day.
  {
     return false;
  }
return true;
}
Edmond
  • 43
  • 1
  • 10
0

Use -1, 0 and 1 t differentiate between before, same and after date i.e.

  1. "-1" - if the given date is before
  2. "0" - If the given date is same
  3. "1" - If the given date is after

Then you can use the function in other method and update your condition like

public boolean after(Date d)
{
   if(before(d) == 1)
   {
      return true;
   }
return false;
}
Hitesh Garg
  • 1,211
  • 1
  • 10
  • 21