如何在 java 中重新创建 compareTo 方法?
How to recreate compareTo method in java?
日期的 compareTo()
方法在 java 中如何工作?我知道当你比较两个日期时,如果相等,结果总是 0
,如果日期是 1
compared inside compareTo()
parameter is older, and -1
if the date inside the parameter is more recent.
//Just an example
String[] da = {"01/14/1975", "08/20/1975", "08/20/1975"};
SimpleDateFormat f = new SimpleDateFormat("MM/dd/yyyy");
Date d1 = new Date();
Date d2 = new Date();
//this outputs 1 because d2 is older than d1
d1 = f.parse(da[1]);
d2 = f.parse(da[0]);
System.out.println(d1.compareTo(d2));
//this outputs 0 because dates are the same
d1 = f.parse(da[1]);
d2 = f.parse(da[2]);
System.out.println(d1.compareTo(d2));
//this outputs -1 because d2 is more recent than d1
d1 = f.parse(da[0]);
d2 = f.parse(da[1]);
System.out.println(d1.compareTo(d2));
现在我想比较日期而不使用 compareTo()
方法或 java 中的任何内置方法。我想尽可能多地使用 java.
中的基本运算符
compareTo()
方法在比较日期时的计算或算法是什么,使其能够与 return、-1
、0
和 1
?
编辑:
在我书中的示例问题中,禁止使用 java.util.Date,应该做的是像这样创建自己的日期对象:
public class DatesObj
{
protected int day, month, year;
public DatesObj (int mm, int dd, int yyyy) {
month = mm;
day = dd;
year = yyyy;
}
public int getMonth() { return month; }
public int getDay() { return day; }
public int getYear() { return year; }
}
现在我如何比较它们,就好像它们是 int
并确定哪个是旧的,哪个是新的??
实施 Comparable 并覆盖 compareTo()。
class DatesObj implements Comparable<DatesObj>{
protected int day, month, year;
public DatesObj(int mm, int dd, int yyyy) {
month = mm;
day = dd;
year = yyyy;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getYear() { return year; }
@Override
public int compareTo(DatesObj o) {
int diff = this.year - o.year;
if(diff != 0) {
return diff;
}
diff = this.month - o.month;
if(diff != 0) {
return diff;
}
return this.day - o.day;
}
}
比较年份。如果两个日期的年份相同,则比较月份。
如果月份相同,则比较日期。
public int compareDate(DatesObj d) {
if (this.year != d.year) {
if (this.year > d.year)
return 1;
else
return -1;
}
if (this.month != d.month) {
if (this.month > d.month)
return 1;
else
return -1;
}
if (this.day != d.day) {
if (this.day > d.day)
return 1;
else
return -1;
}
return 0;
}
参考:https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html
参考:https://developer.android.com/reference/java/util/Calendar
创建自己的 class
class DateCompare implements Comparable<Date>
{
protected int day, month, year;
public DateCompare(int mm, int dd, int yyyy) {
month = mm;
day = dd;
year = yyyy;
}
@Override
public int compareTo(Date o) {
Calendar cal = Calendar.getInstance();
cal.setTime(o);
int diff = this.year - cal.get(Calendar.YEAR);
if(diff != 0) {
return diff;
}
diff = this.month - cal.get(Calendar.MONTH);
if(diff != 0) {
return diff;
}
return this.day - cal.get(Calendar.DAY_OF_MONTH);
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getYear() { return year; }
}
以及其他更有帮助的
https://gist.github.com/Ashusolanki/fed3b6a680092985ac0ab93ed70fcd7c
private String postTime(Date date)
{
long postTime = date.getTime();
long atTime = System.currentTimeMillis();
long diff = atTime - postTime;
long sec = TimeUnit.SECONDS.convert(diff, TimeUnit.MILLISECONDS);
if (sec >= 60) {
long minit = TimeUnit.MINUTES.convert(diff, TimeUnit.MILLISECONDS);
if (minit >= 60) {
long hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
if (hours >= 24) {
long days = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
return days + " Days Ago";
} else {
return hours + " Hours Ago";
}
} else {
return minit + " Minutes Ago";
}
} else {
return sec + " Secounds Ago";
}
}
如果您想比较两个日期,就好像它们只是普通整数一样,您必须首先将每个日期转换为普通整数。将日期的 year/month/day 表示形式转换为可以与其他日期的普通整数进行有效比较的普通旧整数的最简单方法是按照以下顺序排列各个部分:首先是年份,下个月,最后一天:
// in DateObj class....
public int getDateInt() {
return (yyyy * 10000) + (mm * 100) + dd;
}
因此,对于 2019 年 3 月 19 日,您得到 20190319,对于 1941 年 12 月 7 日,您得到 19411207;通过比较日期的 "integerized" 版本,您可以看到:
- 19411207 < 20190319,正如1941年12月7日早于2019年3月19日;
- 20190319 > 19411207,正如2019年3月19日晚于1941年12月7日;
- 19411207 != 20190319,因为1941年12月7日和2019年3月19日是不同的日期
对于这个特定的实现,您仅限于共同时代内的日期,并且不超过未来 200,000 年。但是稍加调整,您就可以轻松处理这些范围之外的日期,正如教科书经常说的那样,我将把这个练习留作 reader.
的练习。
日期的 compareTo()
方法在 java 中如何工作?我知道当你比较两个日期时,如果相等,结果总是 0
,如果日期是 1
compared inside compareTo()
parameter is older, and -1
if the date inside the parameter is more recent.
//Just an example
String[] da = {"01/14/1975", "08/20/1975", "08/20/1975"};
SimpleDateFormat f = new SimpleDateFormat("MM/dd/yyyy");
Date d1 = new Date();
Date d2 = new Date();
//this outputs 1 because d2 is older than d1
d1 = f.parse(da[1]);
d2 = f.parse(da[0]);
System.out.println(d1.compareTo(d2));
//this outputs 0 because dates are the same
d1 = f.parse(da[1]);
d2 = f.parse(da[2]);
System.out.println(d1.compareTo(d2));
//this outputs -1 because d2 is more recent than d1
d1 = f.parse(da[0]);
d2 = f.parse(da[1]);
System.out.println(d1.compareTo(d2));
现在我想比较日期而不使用 compareTo()
方法或 java 中的任何内置方法。我想尽可能多地使用 java.
compareTo()
方法在比较日期时的计算或算法是什么,使其能够与 return、-1
、0
和 1
?
编辑: 在我书中的示例问题中,禁止使用 java.util.Date,应该做的是像这样创建自己的日期对象:
public class DatesObj
{
protected int day, month, year;
public DatesObj (int mm, int dd, int yyyy) {
month = mm;
day = dd;
year = yyyy;
}
public int getMonth() { return month; }
public int getDay() { return day; }
public int getYear() { return year; }
}
现在我如何比较它们,就好像它们是 int
并确定哪个是旧的,哪个是新的??
实施 Comparable 并覆盖 compareTo()。
class DatesObj implements Comparable<DatesObj>{
protected int day, month, year;
public DatesObj(int mm, int dd, int yyyy) {
month = mm;
day = dd;
year = yyyy;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getYear() { return year; }
@Override
public int compareTo(DatesObj o) {
int diff = this.year - o.year;
if(diff != 0) {
return diff;
}
diff = this.month - o.month;
if(diff != 0) {
return diff;
}
return this.day - o.day;
}
}
比较年份。如果两个日期的年份相同,则比较月份。 如果月份相同,则比较日期。
public int compareDate(DatesObj d) {
if (this.year != d.year) {
if (this.year > d.year)
return 1;
else
return -1;
}
if (this.month != d.month) {
if (this.month > d.month)
return 1;
else
return -1;
}
if (this.day != d.day) {
if (this.day > d.day)
return 1;
else
return -1;
}
return 0;
}
参考:https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html
参考:https://developer.android.com/reference/java/util/Calendar
创建自己的 classclass DateCompare implements Comparable<Date>
{
protected int day, month, year;
public DateCompare(int mm, int dd, int yyyy) {
month = mm;
day = dd;
year = yyyy;
}
@Override
public int compareTo(Date o) {
Calendar cal = Calendar.getInstance();
cal.setTime(o);
int diff = this.year - cal.get(Calendar.YEAR);
if(diff != 0) {
return diff;
}
diff = this.month - cal.get(Calendar.MONTH);
if(diff != 0) {
return diff;
}
return this.day - cal.get(Calendar.DAY_OF_MONTH);
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getYear() { return year; }
}
以及其他更有帮助的 https://gist.github.com/Ashusolanki/fed3b6a680092985ac0ab93ed70fcd7c
private String postTime(Date date)
{
long postTime = date.getTime();
long atTime = System.currentTimeMillis();
long diff = atTime - postTime;
long sec = TimeUnit.SECONDS.convert(diff, TimeUnit.MILLISECONDS);
if (sec >= 60) {
long minit = TimeUnit.MINUTES.convert(diff, TimeUnit.MILLISECONDS);
if (minit >= 60) {
long hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
if (hours >= 24) {
long days = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
return days + " Days Ago";
} else {
return hours + " Hours Ago";
}
} else {
return minit + " Minutes Ago";
}
} else {
return sec + " Secounds Ago";
}
}
如果您想比较两个日期,就好像它们只是普通整数一样,您必须首先将每个日期转换为普通整数。将日期的 year/month/day 表示形式转换为可以与其他日期的普通整数进行有效比较的普通旧整数的最简单方法是按照以下顺序排列各个部分:首先是年份,下个月,最后一天:
// in DateObj class....
public int getDateInt() {
return (yyyy * 10000) + (mm * 100) + dd;
}
因此,对于 2019 年 3 月 19 日,您得到 20190319,对于 1941 年 12 月 7 日,您得到 19411207;通过比较日期的 "integerized" 版本,您可以看到:
- 19411207 < 20190319,正如1941年12月7日早于2019年3月19日;
- 20190319 > 19411207,正如2019年3月19日晚于1941年12月7日;
- 19411207 != 20190319,因为1941年12月7日和2019年3月19日是不同的日期
对于这个特定的实现,您仅限于共同时代内的日期,并且不超过未来 200,000 年。但是稍加调整,您就可以轻松处理这些范围之外的日期,正如教科书经常说的那样,我将把这个练习留作 reader.
的练习。