简体   繁体   English

Java比较两个日期年,月和日

[英]Java compare two Dates Year, month and day

I have a method to compare two dates to see if transactionDate is in range between validFrom and validUntil, validFrom and validUnti has value like "2017-01-01 00:00:00" but the transaction date sometimes comes with different hours due to conversion to different timezone. 我有一个比较两个日期的方法,以查看transactionDate是否在validFrom和validUntil之间,validFrom和validUnti的值类似于“2017-01-01 00:00:00”,但交易日期有时会因转换而有不同的小时数到不同的时区。

public boolean isDateWithinEmploymentDeploymentValidityPeriod(Date transcationDate, Parameter parameter) {
    return transcationDate.compareTo(parameter.getValidFrom()) >= 0
            && (parameter.getValidUntil() == null || transcationDate.compareTo(parameter.getValidUntil()) <= 0);
}

So i need to compare compare only Year Month and day and don't take time into account, what would be most efficient way without transforming date to gregorianCalendar and geting year month and day separately? 所以我需要比较仅比较年月和日,并且不考虑时间,如果没有将日期转换为gregorianCalendar并且分别将年月和日分别转换,那么最有效的方法是什么?

LocalDate

I think the easy solution is to convert to LocalDate : 我认为简单的解决方案是转换为LocalDate

LocalDate validFrom = LocalDate.parse("2017-01-01 00:00:00", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"));

The LocalDate class accepts parsing with a pattern that includes hours, minutes and seconds, it just ignores those values from the string. LocalDate接受使用包含小时,分钟和秒的模式进行解析 ,它只是忽略字符串中的那些值。

Do the same with validUntil and the transaction date. 使用validUntil和事务日期执行相同validUntil Compare the resulting LocalDate instances using isBefore() , isAfter() , isEqual() , or compareTo() . 使用isBefore()isAfter()isEqual()compareTo()比较生成的LocalDate实例。

If you were using Java < 8, unfortunately there would have been no simple answer (ie you'd have to use Calendar as you said in your question, or maybe rely on a third-party library like Joda-Time). 如果你使用的是Java <8,那么很遗憾没有简单的答案(即你必须像你在问题中所说的那样使用Calendar ,或者可能依赖于像Joda-Time这样的第三方库)。

Hopefully you use Java 8, so we can leverage the goodness that is the java.time package. 希望您使用Java 8,因此我们可以利用java.time包的java.time Since you want to just compare dates (not times), and do not care about timezones, what you want to use is java.time.LocalDate . 由于您只想比较日期(而不是时间),而不关心时区,因此您要使用的是java.time.LocalDate

// Ideally, instances of the java.time classes are used throughout
// the application making this method useless.
private LocalDate toLocalDate(Date date) {
    return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate();
}

public boolean inRange(Date date, Date from, Date until) {
    return inRange(toLocalDate(date), toLocalDate(from), toLocalDate(until));
}

public boolean inRange(LocalDate date, LocalDate from, LocalDate until) {
    return !date.isBefore(from) && !date.isAfter(until);
}

You can use Calendar and set the other parameters apart from year, month and day to 0. 您可以使用日历并将除年,月和日之外的其他参数设置为0。

Calendar transactionCalendar= Calendar.getInstance();

Date transactionDate = ...;

transactionCalendar.setTime(transactionDate );
transactionCalendar.set(Calendar.HOUR_OF_DAY, 0);
transactionCalendar.set(Calendar.MINUTE, 0);
transactionCalendar.set(Calendar.SECOND, 0);
transactionCalendar.set(Calendar.MILLISECOND, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM