1

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")); 
12
  • 2
    By "more than current hour", do you mean more than 1 hour, or the hour component just has to be more? In other words, would shipping time being 00:59:59 and the current time being 01:00:00 fulfil the condition? The same goes for the second condition. Does it need to be 24 hours more, or are you only looking at the calendar day? Commented Jan 10, 2023 at 4:06
  • 1
    And 2) current.toLocalDate().plusDays(1).isEqual(shipping.toLocalDate())? Commented Jan 10, 2023 at 4:54
  • 1
    For 1) the last part of the condition is shippingdate.isAfter(currentDateTime.plusHours(1)). I think. Also @Slaw Commented Jan 10, 2023 at 6:48
  • 1
    compareTo() only tells you which date-time is after which one, not by how much. Commented Jan 10, 2023 at 7:16
  • 1
    Is your shipping input meant to represent a date and time as seen with an offset from UTC of zero hours-minutes-seconds? Your code implies that, but you should say so explicitly. Commented Jan 10, 2023 at 7:23

1 Answer 1

1

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 } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.