0

Assume that I have a data input like this:

018492114,51863406,X0,1,20160218
018529816,51864472,X0,1,20150603
018543434,51864629,X0,1,20150702
018543464,51864990,N5+,1,2015063
018530309,51865142,X0,1,20150603

I want only to convert the 5 column's element to Date format because it was imported as a string. And I want to do a sorting operation by DA_PRM_CTR_ORDER variable (the end column).

Note that I am using arraylist object defined as Personne and I am using Comparable interface to use Comparable method for sorting:

this is my class personne which includes the needed object:

public class Personne implements Comparable {

    private String IDC_PSE_PCL;
    private String IDC_CD_NOT;
    private String CD_NOTE;
    private String IDT_ETT_PSE;
    private String DA_PRM_CTR_ORDER;

    public Personne(String IDC_PSE_PCL, String IDC_CD_NOT,
                    String DA_PRM_CTR_ORDER, String IDT_ETT_PSE, String CD_NOTE) {
        this.IDC_PSE_PCL = IDC_PSE_PCL;
        this.IDC_CD_NOT = IDC_CD_NOT;
        this.IDT_ETT_PSE = IDT_ETT_PSE;
        this.CD_NOTE = CD_NOTE;
        this.DA_PRM_CTR_ORDER = DA_PRM_CTR_ORDER;

    }

    public String getIDC_CD_NOT() {
        return this.IDC_CD_NOT;
    }

    public String getIDC_PSE_PCL() {
        return this.IDC_PSE_PCL;
    }

    public String getDA_PRM_CTR_ORDER() {
        return this.DA_PRM_CTR_ORDER;
    }

    public String getIDT_ETT_PSE() {
        return this.IDT_ETT_PSE;
    }

    public String getCD_NOTE() {
        return this.CD_NOTE;
    }

  @Override
    public int compareTo(Object o) {
        Personne other = (Personne) o;
        
        DateFormat df = new SimpleDateFormat("yyyyMMdd"); 
        Date converted = (Date) df.parse(other.getDA_PRM_CTR_ORDER());
        
                        
        int res = this.DA_PRM_CTR_ORDER.compareTo(converted);
        // Si Egalite des dates
        if (res == 0) {
            res = IDT_ETT_PSE.compareTo(other.getIDT_ETT_PSE());
        }
        return res;
    }

My problem is in the line:

    int res = this.DA_PRM_CTR_ORDER.compareTo(converted);

when I want to sort by DA_PRM_CTR_ORDER values but it show me this problem:

The method compareTo(String) in the type String is not applicable for the arguments (Date)

How can I resolve this issue please?

6
  • 1
    you are comparing DA_PRM_CTR_ORDER, a STRING, to converted, a DATE. Convert DA_PRM_CTR_ORDER to a date type Commented Jul 24, 2019 at 8:01
  • 1
    I bet other.getIDT_ETT_PSE() does not convert the String to a Date, does it? I recommend using suitable data types instead of String. If an attributes represents a date, use java.time.LocalDate, you can directly compare it since it implements Comparable. Commented Jul 24, 2019 at 8:02
  • @BenoitF yes I want to compare DA_PRM_CTR_ORDER . So I need to convert it to yyyymmdd date type. how I can do it ? Commented Jul 24, 2019 at 8:08
  • SA2018: the same as you do it one line earlier. Commented Jul 24, 2019 at 8:11
  • I recommend you don’t use DateFormat, SimpleDateFormat and Date. Those classes are poorly designed and long outdated, the first two in particular notoriously troublesome. Instead use LocalDate and DateTimeFormatter.BASIC_ISO_DATE, both from java.time, the modern Java date and time API. Commented Jul 24, 2019 at 18:22

3 Answers 3

3

A quick fix could be to parse this.DA_PRM_CTR_ORDER to Date too. So the line you highlighted would look like:

int res = df.parse(this.DA_PRM_CTR_ORDER).compareTo(converted);
Sign up to request clarification or add additional context in comments.

1 Comment

@SA2018 yes, nothing prevents to file to contain "Hello everyone" in place of "20160218", so a parsing error may occur when trying to parse the date. Choose what you want to do if such an error occurs, and make it so with try/catch.
1

you should use Date.compareTo(Date) instead of String.compareTo(Date).

suggestion:

Long currentDate = Long.parseLong(this.DA_PRM_CTR_ORDER); 
return currentDate.compareTo(Long.parseLong(other.getDA_PRM_CTR_ORDER()));

Comments

0

It would be better if you compare the timestamp of two dates to compare.

        Date self = (Date) df.parse(this.getDA_PRM_CTR_ORDER());
        String ConvertedTs = String.valueOf(converted.getTime());
        String selfTs = String.valueOf(self.getTime());
        int res = selfTs.compareTo(ConvertedTs);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.