乔达时间:DateTimeComparator。 Java 8 时间 Api 有什么相似之处?
Joda time: DateTimeComparator. What is similar in Java 8 Time Api?
使用 Joda Time,您可以做一些非常酷的事情,例如:
package temp;
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
import org.joda.time.DateTimeFieldType;
public class TestDateTimeComparator {
public static void main(String[] args) {
//Two DateTime instances which have same month, date, and hour
//but different year, minutes and seconds
DateTime d1 = new DateTime(2001,05,12,7,0,0);
DateTime d2 = new DateTime(2014,05,12,7,30,45);
//Define the lower limit to be hour and upper limit to be month
DateTimeFieldType lowerLimit = DateTimeFieldType.hourOfDay();
DateTimeFieldType upperLimit = DateTimeFieldType.monthOfYear();
//Because of the upper and lower limits , the comparator shall only consider only those sub-elements
//within the lower and upper limits i.e.month, day and hour
//It shall ignore those sub-elements outside the lower and upper limits: i.e year, minute and second
DateTimeComparator dateTimeComparator = DateTimeComparator.getInstance(lowerLimit,upperLimit);
int result = dateTimeComparator.compare(d1, d2);
switch (result) {
case -1:
System.out.println("d1 is less than d2");
break;
case 0:
System.out.println("d1 is equal to d2");
break;
case 1:
System.out.println("d1 is greater than d2");
break;
default:
break;
}
}
}
我找到了这个例子 here。
我想要相同的步骤,但 Java 时间 API,但不幸的是,我没有看到任何类似的比较器。
如何只比较某些日期和时间字段而不比较其他字段与 Java 时间 API?
您可以使用 Comparator
.
上提供的通用辅助方法来复制其中的一些行为,稍微手动一些
假设我们import static java.util.Comparator.comparing;
,我们可以在LocalDateTimes
上定义一个只比较月份的比较器:
Comparator<LocalDateTime> byMonth = comparing(LocalDateTime::getMonth);
或仅比较月、日和小时的方法,如您的示例所示:
Comparator<LocalDateTime> byHourDayMonth = comparing(LocalDateTime::getMonth) //
.thenComparing(LocalDateTime::getDayOfMonth) //
.thenComparing(LocalDateTime::getHour);
这确实让您处于手动决定顺序的位置......不是很自动,但有一些更细粒度的控制。
使用 Joda Time,您可以做一些非常酷的事情,例如:
package temp;
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
import org.joda.time.DateTimeFieldType;
public class TestDateTimeComparator {
public static void main(String[] args) {
//Two DateTime instances which have same month, date, and hour
//but different year, minutes and seconds
DateTime d1 = new DateTime(2001,05,12,7,0,0);
DateTime d2 = new DateTime(2014,05,12,7,30,45);
//Define the lower limit to be hour and upper limit to be month
DateTimeFieldType lowerLimit = DateTimeFieldType.hourOfDay();
DateTimeFieldType upperLimit = DateTimeFieldType.monthOfYear();
//Because of the upper and lower limits , the comparator shall only consider only those sub-elements
//within the lower and upper limits i.e.month, day and hour
//It shall ignore those sub-elements outside the lower and upper limits: i.e year, minute and second
DateTimeComparator dateTimeComparator = DateTimeComparator.getInstance(lowerLimit,upperLimit);
int result = dateTimeComparator.compare(d1, d2);
switch (result) {
case -1:
System.out.println("d1 is less than d2");
break;
case 0:
System.out.println("d1 is equal to d2");
break;
case 1:
System.out.println("d1 is greater than d2");
break;
default:
break;
}
}
}
我找到了这个例子 here。
我想要相同的步骤,但 Java 时间 API,但不幸的是,我没有看到任何类似的比较器。
如何只比较某些日期和时间字段而不比较其他字段与 Java 时间 API?
您可以使用 Comparator
.
假设我们import static java.util.Comparator.comparing;
,我们可以在LocalDateTimes
上定义一个只比较月份的比较器:
Comparator<LocalDateTime> byMonth = comparing(LocalDateTime::getMonth);
或仅比较月、日和小时的方法,如您的示例所示:
Comparator<LocalDateTime> byHourDayMonth = comparing(LocalDateTime::getMonth) //
.thenComparing(LocalDateTime::getDayOfMonth) //
.thenComparing(LocalDateTime::getHour);
这确实让您处于手动决定顺序的位置......不是很自动,但有一些更细粒度的控制。