Calender - Equals Problem

EmmaM95

Mitglied
Ich muss in meiner Equals Methode nur das Datum beachten, also wenn das gleiche Datum, dann ist gleich. Die Uhrzeit soll nicht beachtet werden. Mein Problem ist, dass es beachtet wird und ich im Internet nichts dazu gefunden habe.

Code:
    private Calendar date = Calendar.getInstance();
   
    public Dataset(Calendar date, double open, double high, double low, double close){
        this.date=date;
        //this.open=open;
        //this.high=high;
        //this.low=low;
       // this.close=close;
    }

    public  Calendar getDate(){
      return date;
}

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        long temp;
        temp = Double.doubleToLongBits(close);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        result = prime * result + ((date == null) ? 0 : date.hashCode());
        temp = Double.doubleToLongBits(high);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(low);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(open);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Dataset)) {
            return false;
        }
        Dataset other = (Dataset) obj;
        if (Double.doubleToLongBits(close) != Double
                .doubleToLongBits(other.close)) {
            return false;
        }
        if (date == null) {
            if (other.date != null) {
                return false;
            }
        } else if (!date.equals(other.date)) {
            return false;
        }
        if (Double.doubleToLongBits(high) != Double
                .doubleToLongBits(other.high)) {
            return false;
        }
        if (Double.doubleToLongBits(low) != Double.doubleToLongBits(other.low)) {
            return false;
        }
        if (Double.doubleToLongBits(open) != Double
                .doubleToLongBits(other.open)) {
            return false;
        }
        return true;
    }



Ich muss in meiner toString Methode auch nur das Datum ausgeben, aber dafür müsste ich Calendar formatieren. Nur weiß ich nicht wie.
 
Könntest du mir einmal ein Datum schicken in welcher form du es nun angezeigt bekommst, bzw die zu vergleichenden Daten einmal bitte..
 
Ich habe keine Daten, die es vergleichen. Ich habe ein Portal wo ich es reinstelle und es den Code testet.
Das sind die Fehlermeldungen:
Two instances of type Dataset with the same date but different time should still be equal.
Expected: <java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Berlin",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Europe/Berlin,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2013,MONTH=7,WEEK_OF_YEAR=35,WEEK_OF_MONTH=5,DAY_OF_MONTH=22,DAY_OF_YEAR=239,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=5,HOUR_OF_DAY=16,MINUTE=7,SECOND=37,MILLISECOND=10,ZONE_OFFSET=3600000,DST_OFFSET=3600000];9.65;9.75;9.61;9.74>
but: was <java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Berlin",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Europe/Berlin,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2013,MONTH=7,WEEK_OF_YEAR=35,WEEK_OF_MONTH=5,DAY_OF_MONTH=22,DAY_OF_YEAR=239,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=8,HOUR_OF_DAY=16,MINUTE=10,SECOND=10,MILLISECOND=10,ZONE_OFFSET=3600000,DST_OFFSET=3600000];9.65;9.75;9.61;9.74>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:865)
at jpp.marketanalysis.tests.data.TestDataset.testEqualsAndHashCode(TestDataset.java:109)


Dataset#toString did not give a correctly formatted string.
Expected: "22.08.2013;9.65;9.75;9.61;9.74"
but: was "java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=\"Europe/Berlin\",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Europe/Berlin,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2013,MONTH=7,WEEK_OF_YEAR=35,WEEK_OF_MONTH=5,DAY_OF_MONTH=22,DAY_OF_YEAR=239,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=8,HOUR_OF_DAY=16,MINUTE=10,SECOND=10,MILLISECOND=10,ZONE_OFFSET=3600000,DST_OFFSET=3600000];9.65;9.75;9.61;9.74"
 
Damit kann ich nichts anfangen. Was genau hast du erwartet und wo bekommst du deinen Daten her?
Auf welches Portal lädst du deine Datei?
Wir brauchen mehr angaben zu deinem Problem, so kommen wir nicht weiter
 
Das ist ein Portal von der Uni.

Die Aufgabenstellung sieht so aus:
public boolean equals(Object other) Vergleicht zwei Objekte Dataset Instanzen miteinander. Dabei sollen alle Felder auf Gleichheit geprüft werden. Achten Sie darauf, dass beim Vergleich der beiden Calendar-Objekte ausschließlich das Datum geprüft wird, nicht etwa die Uhrzeit.

Und in der toString Methode soll es das Datum in diesem Format ausgeben😀D.MM.YYYY

Aber was bei mir rauskommt sieht man oben. Ich weiß nicht wie ich es formatieren soll, weil ich es mit dem DateFormatter probbiert hab und es nicht geht.
 
Lass dir das Datum von dem Uni Portal mal als String ausgeben, du könntest natürlich auch mit dem Datensatz arbeiten der dir oben zurueck geben wurde da er ja Daten wie ERA=1,YEAR=2013,MONTH=7,WEEK_OF_YEAR=35,WEEK_OF_MONTH=5,DAY_OF_MONTH=22,DAY_OF_YEAR=239,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4 enthält.
 
Stell doch mal die komplette Klasse rein.

Generell würde ich equals und Hashcode nicht selber schreiben, sondern von der IDE generieren lassen.
 
hab ich auch gemacht :
Code:
package jpp.marketanalysis.data;


import java.text.DecimalFormat;
import java.util.Calendar;



public class Dataset {


    private final static DecimalFormat DECIMAL_FORMAT = new DecimalFormat(
            "0.00");
   
    private Calendar date = Calendar.getInstance();
    private double open;
    private double high;
    private double low;
    private double close;
   
    public Dataset(Calendar date, double open, double high, double low, double close){
        this.date=date;
        this.open=open;
        this.high=high;
        this.low=low;
        this.close=close;
    }

    public Calendar getDate() {
        return date;
    }
    }
    public double getOpen() {
        return open;
    }

    public double getHigh() {
        return high;
    }

    public double getLow() {
        return low;
    }

    public double getClose() {
        return close;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        long temp;
        temp = Double.doubleToLongBits(close);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        result = prime * result + ((date == null) ? 0 : date.hashCode());
        temp = Double.doubleToLongBits(high);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(low);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(open);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Dataset)) {
            return false;
        }
        Dataset other = (Dataset) obj;
        if (Double.doubleToLongBits(close) != Double
                .doubleToLongBits(other.close)) {
            return false;
        }
        if (date == null) {
            if (other.date != null) {
                return false;
            }
        } else if (!date.equals(other.date)) {
            return false;
        }
        if (Double.doubleToLongBits(high) != Double
                .doubleToLongBits(other.high)) {
            return false;
        }
        if (Double.doubleToLongBits(low) != Double.doubleToLongBits(other.low)) {
            return false;
        }
        if (Double.doubleToLongBits(open) != Double
                .doubleToLongBits(other.open)) {
            return false;
        }
        return true;
    }
   
    @Override
    public String toString() {
        return  date+ DECIMAL_FORMAT.format(open) + ";" + high
                + ";" + DECIMAL_FORMAT.format(low) + ";" + DECIMAL_FORMAT.format(close) ;
    }
   
   
}
 
Probier mal:

Java:
import java.util.Calendar;
import java.util.Comparator;

public class TimeIgnoringComparator implements Comparator<Calendar> {
    public int compare(Calendar c1, Calendar c2) {
        if (c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR))
            return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
        if (c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH))
            return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
        return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);
    }
}
Java:
import java.text.DecimalFormat;
import java.util.Calendar;


public class Dataset {

    private final static DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.00");

    private Calendar date = Calendar.getInstance();
    private double open;
    private double high;
    private double low;
    private double close;

    public Dataset(Calendar date, double open, double high, double low, double close) {
        this.date = date;
        this.open = open;
        this.high = high;
        this.low = low;
        this.close = close;
    }

    public Calendar getDate() {
        return date;
    }

    public double getOpen() {
        return open;
    }

    public double getHigh() {
        return high;
    }

    public double getLow() {
        return low;
    }

    public double getClose() {
        return close;
    }

    @Override
    public boolean equals(Object o) {
        TimeIgnoringComparator timeIgnoringComparator = new TimeIgnoringComparator();

        if (this == o) return true;
        if (!(o instanceof Dataset)) return false;

        Dataset dataset = (Dataset) o;

        if (Double.compare(dataset.open, open) != 0) return false;
        if (Double.compare(dataset.high, high) != 0) return false;
        if (Double.compare(dataset.low, low) != 0) return false;
        if (Double.compare(dataset.close, close) != 0) return false;

        return timeIgnoringComparator.compare(dataset.date, date) == 0;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = date != null ? date.hashCode() : 0;
        temp = Double.doubleToLongBits(open);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(high);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(low);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(close);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    public String toString() {
        return date + DECIMAL_FORMAT.format(open) + ";" + high
                + ";" + DECIMAL_FORMAT.format(low) + ";" + DECIMAL_FORMAT.format(close);
    }
}
 

Neue Themen


Zurück
Oben