Now I am doing a small project. And this project includes the compare among several dates. So I convert "yyyy-MM-dd" to millisecond by using SimpleDateFormat. However, it did not work out. By the way, when I use more easy way, that is, compare year, month and day one by one, it works out! So the problem is about the SimpleDateFormat.
Here are the code:
package GenericsPractice;
public class Employee {
private String name;
private int age;
private MyDate birthday;
public Employee(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday.toString();
}
}
package GenericsPractice;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class MyDate {
private int month;
private int day;
private int year;
public MyDate(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public long dateToMills() throws ParseException {
String date = (getYear())+"-"+(getMonth())+"-"+(getDay());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
long time = sdf.parse(date).getTime();
return time;
}
@Override
public String toString() {
return "month=" + month +
", day=" + day +
", year=" + year +
'}';
}
}
import GenericsPractice.Employee;
import GenericsPractice.MyDate;
import java.text.ParseException;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class EmployeeTest {
public static void main(String[] args) {
// TreeSet<Employee> set = new TreeSet<>(new Comparator<Employee>() {
// @Override
// public int compare(Employee o1, Employee o2) {
// int year = o1.getBirthday().getYear() - o2.getBirthday().getYear();
// int month = o1.getBirthday().getMonth() - o2.getBirthday().getMonth();
// int day = o1.getBirthday().getDay() - o2.getBirthday().getDay();
// if(year != 0){
// return year;
// }
// if(month != 0){
// return month;
// }
// if(day != 0){
// return day;
// }
// return o1.getName().compareTo(o2.getName());
// }
// });
TreeSet<Employee> set = new TreeSet<>(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
try {
return (int)(o1.getBirthday().dateToMills()-o2.getBirthday().dateToMills());
} catch (ParseException e) {
e.printStackTrace();
}
throw new RuntimeException("输入类型不一致");
}
});
set.add(new Employee("Chandler",23,new MyDate(10,18,1997)));
set.add(new Employee("Ross",25,new MyDate(10,18,1992)));
set.add(new Employee("Joey",20,new MyDate(3,15,1999)));
set.add(new Employee("Monica",35,new MyDate(12,31,1990)));
set.add(new Employee("Rachel",21,new MyDate(6,2,1998)));
Iterator<Employee> iterator = set.iterator();
while(iterator.hasNext()){
Employee obj = iterator.next();
System.out.println(obj);
}
}
}
When running, it did not sort by date:
Employee{name='Chandler', age=23, birthday=month=10, day=18, year=1997}
Employee{name='Monica', age=35, birthday=month=12, day=31, year=1990}
Employee{name='Ross', age=25, birthday=month=10, day=18, year=1992}
Employee{name='Joey', age=20, birthday=month=3, day=15, year=1999}
Employee{name='Rachel', age=21, birthday=month=6, day=2, year=1998}