IMO,这些方法愚蠢得可笑,但我想了解其他开发人员对此类代码的看法。批评可能包括技术和文体错误。更正可以使用 Apache commons-lang 中的任何内容,例如 StringUtils、DateUtils 等,以及 Java 5 中的任何内容。如果这会影响您的风格,该代码适用于 Web 应用程序。如果重要的话,这四个方法也都定义在同一个文件中。我有没有提到这段代码也没有单元测试?!你会怎么做才能解决这个问题?我只是偶然发现了这个文件,修复这个代码不是我的当务之急。如果需要,我可以在业余时间。
方法一:
public static boolean isFromDateBeforeOrSameAsToDate(final String fromDate,
final String toDate) {
boolean isFromDateBeforeOrSameAsToDate = false;
Date fromDt = null;
Date toDt = null;
try {
fromDt = CoreUtils.parseTime(fromDate, CoreConstants.DATE_PARSER);
toDt = CoreUtils.parseTime(toDate, CoreConstants.DATE_PARSER);
// if the FROM date is same as the TO date - its OK
// if the FROM date is before the TO date - its OK
if (fromDt.before(toDt) || fromDt.equals(toDt)) {
isFromDateBeforeOrSameAsToDate = true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return isFromDateBeforeOrSameAsToDate;
}
方法二:
public static boolean isDateSameAsToday(final Date date) {
boolean isSameAsToday = false;
if (date != null) {
Calendar current = Calendar.getInstance();
Calendar compare = Calendar.getInstance();
compare.setTime(date);
if ((current.get(Calendar.DATE) == compare.get(Calendar.DATE))
&& (current.get(Calendar.MONTH) == compare
.get(Calendar.MONTH))
&& (current.get(Calendar.YEAR) == compare
.get(Calendar.YEAR))) {
isSameAsToday = true;
}
}
return isSameAsToday;
}
方法三:
public static boolean areDatesSame(final String fromDate,
final String toDate) {
boolean areDatesSame = false;
Date fromDt = null;
Date toDt = null;
try {
if (fromDate.length() > 0) {
fromDt = CoreUtils.parseTime(fromDate,
CoreConstants.DATE_PARSER);
}
if (toDate.length() > 0) {
toDt = CoreUtils.parseTime(toDate, CoreConstants.DATE_PARSER);
}
if (fromDt != null && toDt != null) {
if (fromDt.equals(toDt)) {
areDatesSame = true;
}
}
} catch (ParseException e) {
if (logger.isDebugEnabled()) {
e.printStackTrace();
}
}
return areDatesSame;
}
方法四:
public static boolean isDateCurrentOrInThePast(final Date compareDate) {
boolean isDateCurrentOrInThePast = false;
if (compareDate != null) {
Calendar current = Calendar.getInstance();
Calendar compare = Calendar.getInstance();
compare.setTime(compareDate);
if (current.get(Calendar.YEAR) > compare.get(Calendar.YEAR)) {
isDateCurrentOrInThePast = true;
}
if (current.get(Calendar.YEAR) == compare.get(Calendar.YEAR)) {
if (current.get(Calendar.MONTH) > compare.get(Calendar.MONTH)) {
isDateCurrentOrInThePast = true;
}
}
if (current.get(Calendar.YEAR) == compare.get(Calendar.YEAR)) {
if (current.get(Calendar.MONTH) == compare.get(Calendar.MONTH)) {
if (current.get(Calendar.DATE) >= compare
.get(Calendar.DATE)) {
isDateCurrentOrInThePast = true;
}
}
}
}
return isDateCurrentOrInThePast;
}
这是我倾向于编写相同内容的方式(嗯,首先我会编写单元测试,但我会在这里跳过)。
public static int compareDatesByField(final Date firstDate,
final Date secondDate, final int field) {
return DateUtils.truncate(firstDate, field).compareTo(
DateUtils.truncate(secondDate, field));
}
public static int compareDatesByDate(final Date firstDate,
final Date secondDate) {
return compareDatesByField(firstDate, secondDate, Calendar.DATE);
}
// etc. as required, although I prefer not bloating classes which little
// methods that add little value ...
// e.g., the following methods are of dubious value, depending on taste
public static boolean lessThan(int compareToResult) {
return compareToResut < 0;
}
public static boolean equalTo(int compareToResult) {
return compareToResut == 0;
}
public static boolean greaterThan(int compareToResult) {
return compareToResut > 0;
}
public static boolean lessThanOrEqualTo(int compareToResult) {
return compareToResut <= 0;
}
public static boolean greaterThanOrEqualTo(int compareToResult) {
return compareToResut >= 0;
}
// time-semantic versions of the dubious methods - perhaps these go in TimeUtils ?
public static boolean before(int compareToResult) {
return compareToResut < 0;
}
public static boolean on(int compareToResult) {
return compareToResut == 0;
}
public static boolean after(int compareToResult) {
return compareToResut > 0;
}
public static boolean onOrBefore(int compareToResult) {
return compareToResut <= 0;
}
public static boolean onOrAfter(int compareToResult) {
return compareToResut >= 0;
}
然后客户可以使用以下方法:
/* note: Validate library from Apache Commons-Lang throws
* IllegalArgumentException when arguments are not valid
* (this comment would not accompany actual code since the
* Javadoc for Validate would explain that for those unfamiliar with it)
*/
Validate.isTrue(onOrAfter(compareDatesByDate(registrationDate, desiredEventDate),
"desiredEventDate must be on or after the *day* of registration: ", desiredEventDate);