我知道我的两个日期是相同的,因为我可以使用存取方法来获取日,月和年,然后将所有日期作为字符串打印出来。当我将两个日期打印为字符串时,它们打印出来是相同的。我尝试使用equals方法使用几种不同的字符串和日期实现无济于事。当我正在比较日期的字符串是“1/1/2000”时,我只有一个问题。这是我的方法。我的toString()方法将日期作为字符串写为“01/01/2000。”
equals()
方法如果它们是相同的日期则返回false,但是一个是长显示而另一个是短显示,即“1990年1月1日”和“01/01/1990”应该如果通过equals
方法进行比较,则返回false值。但是,通过equals()
方法进行比较时,日期(例如“1/1/2000”和“01/01/2000”)将导致返回true值。
这个Date
类是为我所在的java类编写的,用于理解面向对象的程序及其类的工作原理。
默认日期构造函数 - 日期()
public Date() {
setDate(2000, 01, 01);
}
日期(String dateStr)
public Date(String dateStr) {
setDate(dateStr);
}
setDate(String dateStr)
public void setDate(String dateStr) {
int slashCount = 0;
for (int i = 0; i < dateStr.length(); i++) {
if(dateStr.charAt(i) == '/') {
slashCount++;
}
}
if (slashCount == 2) {
if (dateStr.indexOf('/') >= 0) {
int index = dateStr.indexOf('/');
String dd = dateStr.substring(index+1, dateStr.length());
if (dd.substring(1, dd.length()).lastIndexOf('/') >= 0 ) {
int index2 = dd.lastIndexOf('/');
String yy = dd.substring(index2+1, dd.length());
String mm = dateStr.substring(0, index);
dd = dd.substring(0, index2);
yy = yy.substring(0, yy.length());
//System.out.println(mm + "\n" + dd + "\n" + yy );
int y = Integer.parseInt(yy);
int d = Integer.parseInt(dd);
int m = Integer.parseInt(mm);
setYear(y);
setMonth(m);
setDay(d);
//System.out.println("get year returns " + getYear());
//System.out.println("get month returns " + getMonth());
//System.out.println("get day returns " + getDay());
}
}
} else {
throw new IllegalArgumentException();
}
}
等于(对象其他)
public boolean equals(Object other) {
if (other instanceof Date) {
Date that = (Date) other;
return this.day == that.day
&& this.month == that.month
&& this.year == that.year;
} else if (other instanceof String) {
Date that = new Date((String) other);
String thatString = that.toString();
String thisString = this.toString();
return thatString.equals(thisString);
}
return false;
}
的toString()
public String toString() {
if (isShortDisplay()) {
return String.format("%02d/%02d/%04d", month, day, year);
} else {
return String.format(monthString(month) + " %d, %04d", day, year);
}
}
Date Test.java
String date = "1/1/2000";
Date dstring = new Date(date);
Date d1 = new Date();
System.out.println(d1);
if (!(d1.toString().equals("1/1/2000"))) {
System.out.println("Error 1");
System.out.println();
System.out.println("This day (d1) day = " + d1.getDay());
System.out.println("This month (d1) month = " + d1.getMonth());
System.out.println("This year (d1) year = " + d1.getYear());
System.out.println("This date (d1) string = " + d1.toString());
System.out.println();
System.out.println("That (dstring) day = " + dstring.getDay());
System.out.println("That (dstring) month = " + dstring.getMonth());
System.out.println("That (dstring) year = " + dstring.getYear());
System.out.println("That date (dstring) string = " + dstring.toString());
System.out.println(d1.toString().equals(dstring.toString()));
System.out.println(d1.equals(date));
System.out.println(d1.toString().toString());
System.out.println(d1.toString());
System.out.println(dstring);
}
DateTest.java控制台
01/01/2000
Error 1
This day (d1) day = 1
This month (d1) month = 1
This year (d1) year = 2000
This date (d1) string = 01/01/2000
That (dstring) day = 1
That (dstring) month = 1
That (dstring) year = 2000
That date (dstring) string = 01/01/2000
true
true
01/01/2000
01/01/2000
01/01/2000
答案 0 :(得分:0)
我使用以下作为测试...
Date date1 = new Date("01/01/2000");
Date date2 = new Date("1/1/2000");
System.out.println(date1);
System.out.println(date1.equals(date2));
System.out.println(date2.equals(date1));
System.out.println(date1.equals("1/1/2000"));
System.out.println(date1.equals("01/01/2000"));
System.out.println(date2.equals("1/1/2000"));
System.out.println(date2.equals("01/01/2000"));
我得到以下输出......
01/01/2000
true
true
true
true
true
true
所以,除非你愿意提供实际的runnable example that demonstrates your problem,否则我不确定还能说些什么......