0

EDIT: IT WORKED THANKS TO EVERYONE!

For an assignment I have to construct a java class that checks whether or not the year of a specific date and time, exactly in the same form as this: "28/04/2019 18:06:25" , is a leapyear. I managed to write the code that checks the leapyear. However our assignment states that there should be an equals method (public boolean equals(Object other)) that checks whether the current instance is the same time as the given object. I really don't know how to do this, the best I could come up with is this:

public class DateTime {
    private static String datumtijd;
    public DateTime(String dateTime){
        datumtijd = dateTime;
    }

    public void setDateTime(String newtime){
        datumtijd = newtime;
    }

    public boolean equals(Object other){
        return (datumtijd.equals(other.datumtijd));

    }

    public int getDay(){
        char nulkarakter = datumtijd.charAt(0);
        char nul = 0;
        if (nulkarakter == nul){
            String day = datumtijd.substring(1);
            int dayint = Integer.parseInt(day);
            return dayint;
        }
        else {
            String daylang = datumtijd.substring(0, 2);
            int daylangint = Integer.parseInt(daylang);
            return daylangint;
        }
    }

...

But this doesn't seem to work. Sorry for being such a noob but I hope you could help me since I'm struggling with this for way to long.

Thank you so much!

  • 1
    [How to Implement Java’s equals Method Correctly](https://www.sitepoint.com/implement-javas-equals-method-correctly/). And this one: [Overriding equals method in Java](https://www.geeksforgeeks.org/overriding-equals-method-in-java/). Were your search engine broken? :-) – Ole V.V. Nov 13 '19 at 19:16
  • 1
    Rather than storing the strnig you get I recommend you store numbers for year, month, day of month, hour, minute and second. So you parse the string once when your object is created instead of doing it every time someone wants to get information from it. – Ole V.V. Nov 13 '19 at 19:22

3 Answers3

0

First, check if it's a DateTime object. Second, cast to DateTime. Finally, do your equals like you are doing it

public boolean equals(Object other) {

  if (other instanceof DateTime) {

     DateTime dt2 = (DateTime) other;
     return (datumtijd.equals(dt2.datumtijd));
  } else return false;

}
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

Can you not use GregorianCalendar methods? You might need to parse using SimpleDateFormat to parse and get the Calendar object.

  • 1
    This should be a comment, but poster doesn't have the comment privilege yet – ControlAltDel Nov 13 '19 at 19:18
  • I recommend you neither use `SimpleDateFormat` nor `GregorianCalendar`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. If the questioner is allowed to use library classes, use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 13 '19 at 19:20
0

It seems you are on the correct track. Comparing Strings in java does require the .equals method or the .contentEquals method. You cannot use '=='. However, because the equals method is passed an Object object and not a DateTime object you must cast the Object object into a DateTime object. You can check if it is a DateTime object by using 'instance of' operator. After that it's just DateTime dt = (DateTime)object;

Cole L
  • 1