[英]How to compare two dates in Java and then compare the result vs another date?
Noob here!菜鸟在这里! First I am trying to compare
dateOne
and dateTwo
with each other, then which ever that has the most recent date (> 0)
take it and compare that versus the cutOffDate
.首先,我试图将
dateOne
和dateTwo
相互比较,然后哪个具有最近日期(> 0)
的那个将其与cutOffDate
进行比较。
If its after cutOffDate
, set that to be FinalDate
, else set cutOffDate
to be FinalDate
.如果它在
cutOffDate
之后,将其设置为FinalDate
,否则将cutOffDate
设置为FinalDate
。 How can I achieve this?我怎样才能做到这一点?
I'm not sure if I'm on the right track here, I am getting the following error with dateOne.compareTo(dateTwo)
:我不确定我是否在正确的轨道上,
dateOne.compareTo(dateTwo)
出现以下错误:
The method compareTo(String) in the type String is not applicable for the arguments (Date)
String 类型中的 compareTo(String) 方法不适用于 arguments (Date)
public DateServiceImpl process(final DateServiceImpl item) throws Exception {
final Date dateOne = item.getDateOne();
final Date dateTwo = item.getDateTwo();
final BigDecimal amountOne= item.getAmountOne();
final BigDecimal amountTwo= item.getAmountTwo();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String cutOffDate = "01/01/2020";
Date cDate = sdf.parse(cutOffDate);
if ((amountOne.compareTo(BigDecimal.ZERO) > 0) && (amountTwo.compareTo(BigDecimal.ZERO) > 0)){
if (dateOne.compareTo(dateTwo) <= 0) {
item.setFinalDate(cDate);
}
return null;
}
}
Do use java.time, the modern Java date and time API, for your date work.请务必使用 java.time,现代 Java 日期和时间 API,用于您的约会工作。 First, java.time allows to declare the formatter once and for all:
首先, java.time 允许一劳永逸地声明格式化程序:
private static final DateTimeFormatter DATE_FORMATTER
= DateTimeFormatter.ofPattern("MM/dd/yyyy");
I understand that the final date should be the latest of dateOne
, dateTwo
and the cut-off date.我了解最终日期应该是
dateOne
、 dateTwo
和截止日期中最晚的日期。 Given that DateServiceImpl
uses LocalDate
instead of Date
, the logic can be simplified:鉴于
DateServiceImpl
使用LocalDate
而不是Date
,逻辑可以简化:
LocalDate dateOne = item.getDateOne();
LocalDate dateTwo = item.getDateTwo();
String cutOffDate = "01/01/2020";
LocalDate cDate = LocalDate.parse(cutOffDate, DATE_FORMATTER);
LocalDate finalDate = Collections.max(Arrays.asList(dateOne, dateTwo, cDate));
item.setFinalDate(finalDate);
I have left out the final
declarations and the amount logic since they weren't relevant to the question asked;我省略了
final
声明和金额逻辑,因为它们与所提出的问题无关; you can put those back in yourself.你可以把它们放回你自己。
A LocalDate
is a date without time of day. LocalDate
是没有时间的日期。 I thought that this was what you needed, please check yourself.我认为这是您需要的,请检查自己。
Edit: as Basil Bourque suggests in a comment, if using Java 9 or later, you will probably prefer List.of()
over Arrays.asList()
:编辑:正如 Basil Bourque 在评论中建议的那样,如果使用 Java 9 或更高版本,您可能会更喜欢
List.of()
而不是Arrays.asList()
:
LocalDate finalDate = Collections.max(List.of(dateOne, dateTwo, cDate));
For situations where this is not enough LocalDate
also has methods isBefore
and isAfter
for comparisons.对于这还不够的情况,
LocalDate
也有方法isBefore
和isAfter
用于比较。
If you cannot afford to upgrade DateServiceImpl
to java.time just now, you may use Collections.max()
on three old-fashioned Date
objects in the same way.如果您现在无法将
DateServiceImpl
升级到 java.time,您可以以相同的方式在三个老式Date
对象上使用Collections.max()
。
Collections.max()
also comes in an overloaded version that takes Comparator
as its second argument. Collections.max()
也有一个重载版本,它将Comparator
作为其第二个参数。 Since both LocalDate
and Date
implement Comparable
we don't need that here.由于
LocalDate
和Date
都实现了Comparable
我们在这里不需要它。
Collections.max()
Collections.max()
的文档 You're on the right track.你在正确的轨道上。 You just need another comparison and you should be good.
你只需要另一个比较,你应该很好。
if (dateOne.compareTo(dateTwo) > 0) {
if (dateOne.compareTo(cDate) > 0) {
item.setFinalDate(dateOne);
} else {
item.setFinalDate(cDate);
}
} else {
if (dateTwo.compareTo(cDate) > 0) {
item.setFinalDate(dateTwo);
} else {
item.setFinalDate(cDate);
}
}
You needed to check each case of which order the dates come in (check if dateOne
comes before dateTwo
and check if dateOne
comes after dateTwo
).您需要检查日期进入的每种情况(检查 dateOne 是否在
dateOne
之前并检查dateTwo
是否在dateOne
dateTwo
)。 Then, for each of those checks, you have to check whether the most recent date comes before or after the cutoff date.然后,对于每一项检查,您必须检查最近的日期是在截止日期之前还是之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.