Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Object by dates ( implements Comparator)

I have a java People object :

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;
        }        
}

I want to create a Comparator to compare by datebirth (datebirth is sometimes in this format "2/13/1943", sometimes in this format "2-13-1943", can you help me on how to implement it.

I started this , but got confused :

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;
        }
}
like image 300
akram Avatar asked Dec 12 '22 07:12

akram


1 Answers

If you'd store birth date using its proper type - a java.util.Date object instead of a String - you wouldn't have this problem. Formatting is a display issue.

A class named "People"?

Here's how I might do it (all classes below in separate .java files in a package named model):

package model;

public enum Gender { MALE, FEMALE }

public class Person {
    private String firstName;
    private String lastName;
    private Gender gender;
    private Date birthDate;  // I'd make sure to set hh:mm:ss all to midnight

    // constructors, getters, equals, hashCode, and toString are left for you
}

public class BirthDateComparator implements Comparator<Person> {
    public int compare(Person p, Person q) {
        if (p.getBirthDate().before(q.getBirthDate()) {
            return -1;
        } else if (p.getBirthDate().after(q.getBirthDate()) {
            return 1;
        } else {
            return 0;
        }        
    }
}
like image 74
duffymo Avatar answered Dec 31 '22 17:12

duffymo