Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

these dates are same date so I want to return true.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

java.time

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.

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

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading