简体   繁体   English

如何在 java 中使用日历比较没有时间的日期?

[英]how to compare date without time using calendar in java?

I want to compare two date type only date not time.我只想比较两个日期类型的日期而不是时间。

date1 = 2022.10.10 16:30:40 date2 = 2022.10.10 13:30:40日期 1 = 2022.10.10 16:30:40 日期 2 = 2022.10.10 13:30:40

these dates are same date so I want to return true.这些日期是相同的日期,所以我想返回 true。

below is my code.下面是我的代码。 is there clean code?有干净的代码吗?

public Boolean a0160(HashMap<String, Object> params){
        Date accessRecord;
        Date now = new Date();
        accessRecord = userMapper.a0170(params);
        Calendar calAccessRecord = Calendar.getInstance();
        Calendar calOneHourBefore = Calendar.getInstance();
        calAccessRecord.setTime(accessRecord);
        calOneHourBefore.setTime(now);
        calOneHourBefore.add(Calendar.HOUR, -1);

        int calOneHourBeforeYear = calOneHourBefore.get(Calendar.YEAR);
        int calOneHourBeforeMonth = calOneHourBefore.get(Calendar.MONTH);
        int calOneHourBeforeDate = calOneHourBefore.get(Calendar.DATE);

        int calAccessRecordYear = calAccessRecord.get(Calendar.YEAR);
        int calAccessRecordMonth = calAccessRecord.get(Calendar.MONTH);
        int calAccessRecordDate = calAccessRecord.get(Calendar.DATE);

        if(calOneHourBeforeYear == calAccessRecordYear && calAccessRecordMonth == calOneHourBeforeMonth && calAccessRecordDate == calOneHourBeforeDate){
            return true;
        } else {
            return false;
        }
    }

tl;dr tl;博士

myJavaUtilDate
.toInstant() 
.atZone( 
    ZoneId.of( "Pacific/Auckland" )
)
.toLocalDate()
.isEqual(
    ZonedDateTime
    .now( ZoneId.of( "Pacific/Auckland" ) )
    .minusHours( 1 )
    .toLocalDate()
)

java.time java.时间

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.您正在使用多年前被 JSR 310 中定义的现代java.time类所取代的糟糕日期时间类。

Apparently you are handed a java.util.Date object. The first thing to do is convert from the flawed legacy to its modern replacement, java.time.Instant .显然,您收到了一个java.util.Date object。要做的第一件事就是将有缺陷的旧版转换为现代替代品java.time.Instant Use new conversion methods added to the old classes.使用添加到旧类的新转换方法。

Instant instant = myJavaUtilDate.toInstant() ;

Both the legacy and modern classes represent a moment as seen with an offset from UTC of zero hours-minutes-seconds.传统类和现代类都代表一个时刻,与 UTC 的偏移量为零小时-分钟-秒。

Understand that for any given moment, the date varies around the globe by time zone.了解在任何给定时刻,全球各地的日期因时区而异。 So determining a date requires the context of a time zone.因此确定日期需要时区的上下文。

ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

Extract the date portion.提取日期部分。

LocalDate ld = zdt.toLocalDate() ;

Apparently you want to compare that to the current date as of one hour ago.显然您想将其与一小时前的当前日期进行比较。

LocalDate dateAnHourAgo = ZonedDateTime.now( z ).minusHours( 1 ).toLocalDate() ;

Compare with isEqual , isBefore , and isAfter methods.isEqualisBeforeisAfter方法进行比较。

They aren't dates;它们不是约会对象; they're strings:它们是字符串:

return date2.startsWith(date1.substring(0, 10));

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

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