java-比较月份和年份的两个日期值 [英] java - compare two date values for the month and year

查看:967
本文介绍了java-比较月份和年份的两个日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要匹配两个日期,如果它们的月份/年份相同,我应该返回true,否则返回false。根据我的搜索,我找到了以下解决方案。还有其他更好的方法可以进行此比较吗?

I have a requirement to match two dates and if their month/year are same, i should return true, otherwise false. Based on my search, i found the following solution. Is there any other better way to do this comparison?.

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);


推荐答案

tl; dr



tl;dr


匹配两个日期,如果它们的月份/年份相同

match two dates and if their month/year are same

还有其他更好的方法可以进行此比较是

Is there any other better way to do this comparison?

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

Yes, the better way uses the modern java.time classes.

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.
)



详细信息



是的,有更好的选择

Details

Yes, there is a better way.

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

You are using old date-time classes bundled with the earliest versions of Java. These classes have proven to be poorly designed, confusing, and troublesome. Avoid them, including java.util.Date.

这些旧类已被构建的 java.time 框架Java 8及更高版本。

Those old classes have been supplanted by the java.time framework built into Java 8 and later.

将给定的java.util.Date对象转换为 即时 对象,在 UTC

Convert the given java.util.Date objects to Instant objects, a moment on the timeline in UTC.

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

我们的目标是访问 YearMonth 对象,因为您只关心年份和年份这个月。但是要到达那里,我们需要应用一个时区。年和月仅在特定时区中具有意义,除非您来自冰岛 I怀疑是否要使用UTC的年/月上下文。

We are aiming to get to 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 I doubt you want the year/month context of UTC.

因此,我们需要指定所需/预期的时区( ZoneId )。

So we need to specify the desired/expected time zone (ZoneId).

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



ZonedDateTime



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

ZonedDateTime

Apply that time zone to each Instant, producing ZonedDateTime objects.

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 对象,而不用重新计算或传递字符串。

By the way, you likely have other business logic involving the year and month. Remember that the java.time classes are built into Java now. So you can use and pass YearMonth objects throughout your code base rather than re-calculating or passing strings.

java.time 框架已内置到Java 8及更高版本中。这些类取代了麻烦的旧版日期时间类,例如 java.util.Date Calendar ,& SimpleDateFormat

The 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 项目,现在处于维护模式 ,建议迁移到 java.time 类。

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

要了解更多信息,请参见 Oracle教程 。并在Stack Overflow中搜索许多示例和说明。规范为 JSR 310

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

您可以直接与数据库交换 java.time 对象。使用符合 JDBC驱动程序 / jeps / 170 rel = nofollow noreferrer> JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql。* 类。

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

在哪里获取java.time类?

Where to obtain the java.time classes?


  • Java SE 8 Java SE 9 Java SE 10 Java SE 11 和更高版本-具有捆绑实现的标准Java API。


    • Java 9添加了一些次要功能和修复。

    • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

      ThreeTen-Extra 项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。您可能会在这里找到一些有用的类,例如 时间间隔 YearWeek YearQuarter 更多

      这篇关于java-比较月份和年份的两个日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆