java - 比较月份和年份的两个日期值

java - compare two date values for the month and year

我有一个匹配两个日期的要求,如果它们 month/year 相同,我应该 return 为真,否则为假。根据我的搜索,我找到了以下解决方案。还有其他更好的方法来进行这种比较吗?

Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);
    cal2.setTime(date2);
    boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                      cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);

您可以使用date1.getYear() == date2.getYear() && date1.getMonth() == date2.getMonth()

tl;博士

match two dates and if their month/year are same

Is there any other better way to do this comparison?

是的,更好的方法是使用现代的 java.time 类.

YearMonth.from(                                   // Represent the year-month without a day-of-month, without a time-of-day, and without a time zone.
    LocalDate.of( 2018 , Month.JANUARY , 23 ) ,   // Represent a date-only, without a time-of-day and without a time zone.
)                                                 // Returns a `YearMonth` object.
.equals(                                          // Compare one `YearMonth` object to another.
    YearMonth.now()                               // Capture today’s year-month as seen in the JVM’s current default time zone.
)

详情

是的,有更好的方法。

您正在使用与 Java 的最早版本捆绑在一起的旧日期时间 类。这些 类 已被证明设计不当、令人困惑和麻烦。避开它们,包括 java.util.Date.

java.time

那些旧的 类 已被 Java 8 及更高版本中内置的 java.time 框架所取代。

Instant

将给定的 java.util.Date 个对象转换为 Instant objects, a moment on the timeline in UTC

Instant i1 = myJavaUtilDate1.toInstant();
Instant i2 = myJavaUtilDate2.toInstant();

我们的目标是 YearMonth objects as you only care about the year and the month. But to get there we need to apply a time zone. The year and the month only have meaning in the context of a specific time zone, and unless you are from Iceland 我怀疑你想要 year/month UTC 上下文。

ZoneId

所以我们需要指定desired/expected时区(ZoneId)。

ZoneId zoneId = ZoneId.of( "America/Montreal" );

ZonedDateTime

将该时区应用于每个 Instant,生成 ZonedDateTime 个对象。

ZonedDateTime zdt1 = ZonedDateTime.ofInstant( i1 , zoneId );
ZonedDateTime zdt2 = ZonedDateTime.ofInstant( i2 , zoneId );

YearMonth

现在提取 YearMonth 个对象。

YearMonth ym1 = YearMonth.from( zdt1 );
YearMonth ym2 = YearMonth.from( zdt2 );

比较。

Boolean same = ym1.equals( ym2 );

顺便说一下,您可能还有其他涉及年份和月份的业务逻辑。请记住 java.time 类 现在已内置到 Java 中。因此,您可以在整个代码库中使用和传递 YearMonth 对象,而不是重新计算或传递字符串。


关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time 类.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

您可以直接与数据库交换 java.time 对象。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类.

在哪里获取java.time类?

ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

快车道: 使用 Joda Time 库。

  • 首先,请确保您 downloaded/implemented 它。
  • 然后导入。
import org.joda.time.LocalDate;
import java.util.Date;
  • 给你:
Date Date1 = new Date();
Date Date2 = new Date();

if (new LocalDate(Date1).getYear() == new LocalDate(Date2).getYear()) {
 // Year Matches
 if (new LocalDate(Date1).getMonthOfYear() == new LocalDate(Date2).getMonthOfYear()) {
  // Year and Month Matches
 }
}