私が持っているJavaの人々は、オブジェクト:日付でオブジェクトの比較(実装コンパレータ)
public class People {
String lastname;
String firstname;
String gender;
String datebirth;
String fcolor;
public People(String lastname, String firstname, String gender,String datebirth, String fcolor) {
this.lastname = lastname;
this.firstname = firstname;
this.gender = gender;
this.datebirth = datebirth;
this.fcolor = fcolor;
}
public String getLastname() {
return lastname;
}
public String getFirstname() {
return firstname;
}
public String getGender() {
return gender;
}
public String getFcolor() {
return fcolor;
}
public String getDatebirth() {
return datebirth;
}
}
は、私は(datebirthは時々このフォーマット「1943年2月13日」であるdatebirthで比較するコンパレータを作成したいです、時々、「1943年2月13日」の形式で、あなたはそれを実装する方法で私を助けることができる
を私はこれを始めたが、混乱してしまった:。
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.text.DateFormat;
public class CompareDateBirth implements Comparator<People>{
public int compare(People p, People q) {
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Date Pdate = null;
Date Qdate= null;
try {
Pdate = df.parse(p.getDatebirth());
Qdate = df.parse(q.getDatebirth());
} catch (Exception e) {
e.printStackTrace();
}
return Pdate.compareTo(Qdate) > 0 ? 1 : 0;
}
}
ありがとうございました。私は今比較するコンパレータを持っています:性別(女性の前に男性)、姓の昇順、あなたの助けを感謝していただきありがとうございます。 – akram
警告、pまたはqはnullでもかまいません。 – Nico
5年遅れです。もちろん、OPはそれをすべてチェックしなければなりません。これは、実例コードではなく、実例として意図されていました。 – duffymo