user3763183
user3763183

Reputation: 39

how compare localdatetime with current date including hours in java

My Local date Time(Shipping) in the format like "2023-01-11T01:25:59"

I need to compare with current time do the below conditions

  1. If both are in same date and Shipping hour more than current hour

  2. Shipping Date equals next Current date ( In this case Shipping Date = 2023-01-11 == Current date + 1 day

I did this but it's not working with hours

LocalDateTime shippingdate= (calling funtion)
System.out.println("shippingdate "+shippingdate);  //2023-01-11T01:25:59 

LocalDateTime currentDateTime = LocalDateTime.now(ZoneId.of("UTC"));
System.out.println("currentTime "+currentDateTime);   //2023-01-10T03:42:52.574994

int diff = shippingdate.compareTo(currentDateTime);

if (diff==1) {
}

if (diff > 1) {
}


LocalDateTime currentDateTime = LocalDateTime.now(ZoneId.of("UTC"));

Upvotes: 1

Views: 4803

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 340350

LocalDateTime cannot represent a moment

One problem is your inappropriate use of the LocalDateTime class. That class purposely lacks the context of a time zone or office from UTC. So that class cannot represent a moment, a specific point on the timeline. Yet you are trying to track moments, apparently.

If handed a date and time only, parse as a LocalDateTime.

LocalDateTime ldt = LocalDateTime.parse( "2023-01-11T01:25:59" ) ;

OffsetDateTime

But if you are certain that date and time was meant to represent a moment as seen with an offset of zero, immediately convert to an OffsetDateTime. Meanwhile, educate the publisher of your input data about their failure to communicate all three pieces of vital info (date, time, and offset/zone).

OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ; 

The idea here is to “tell the truth”. If the intention is a date with time as seen in UTC, then our code should represent a date with time as seen in UTC.

Apparently you want to compare this date-time in UTC against the current moment as seen in UTC.

OffsetDateTime now = OffsetDateTime.now( ZoneOffset.UTC ) ;

LocalDate

Your primary concern is the date.

LocalDate shippingDate = odt.toLocalDate() ;
LocalDate today = now.toLocalDate() ;
LocalDate tomorrow = today.plusDays( 1 ) ;

if ( shippingDate.isBefore( today ) { … is past }
else if ( shippingDate.isEqual( today ) { … is today }
else if ( shippingDate.isEqual( tomorrow ) { … is tomorrow }
else if ( shippingDate.isAfter( tomorrow ) { … is future }
else { … throw exception because we should never reach this point }

OffsetDateTime#getHour

Your secondary concern was apparently that you want to know, if today, is the target hour the same as the current hour. Your writing is not at all clear on this point, but that is my guess.

So add a nested test within the “is equal to today” test branch above.

if( odt.getHour() == now.getHour() ) { … same clock hour }

Or perhaps you want to test for a later, future hour.

if( odt.getHour() > now.getHour() ) { … later, future hour }

Upvotes: 1

Related Questions