0

I have a java application where in the User can not modify the order after a specific date and time .eg.the user can not modify the order after the 3rd day by 12:00 PM ,say if the order was placed on Nov 9th ,the user will not be able to modify the oder after Nov 12th ,12:00 PM .The Date is Dynamic but the time is very much static.

I was trying to use the below logic to calculate this and i am unable to figure out how to extract the current time from LocalDateTime.now() for comparison.

final LocalDate orderDate  =orderData.getOrderDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
final LocalDate currentDate = LocalDate.now();
final LocalDateTime currentDateTime = LocalDateTime.now();
final LocalDate orderCancellationCutOffDate = 
orderDate.minusDays(orderCancellationCutOffDays);

if (currentDate.equals(orderCancellationCutOffDays) && 
currentDateTime.isBefore(<12:00 PM>)){

<Business rule>
    }   

Can anyone help me with an efficient way to do this comparison.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
user964819
  • 343
  • 3
  • 6
  • 24

2 Answers2

2

Suppose if you have the oder date in LocalDate as today

LocalDate orderDate //2019-11-09

Now create the deadline date by adding 3 days to it

LocalDateTime deadLineDate =orderDate.plusDays(3).atStartOfDay(); //2019-11-12T00:00

Even if you want a particular time you can use atTime methods

LocalDateTime deadLineDate =orderDate.plusDays(3).atTime(12,0);

So if currentDateTime is before the deadLineDate customer can modify the order

if(currentDateTime.isBefore(deadLineDate)) {
    // can modify the order

    }
else {
   //user can not modify the order
}
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • 1
    @downvoter can you please explain what's wrong with my logic? – Ryuzaki L Nov 09 '19 at 19:20
  • I’m guessing in the dark here because I’d never dream of downvoting this answer myself. The downvote could be just because the question asked for 12 noon and you’re giving 12 midnight. I agree that a critical comment is always a lot more helpful to everyone than a downvote. – Ole V.V. Nov 09 '19 at 20:31
  • I'm in 12 hours format and thought `1` AM after `12` PM, but thanks for correcting me and i updated my answer. But still why they have to downvote, at least they can tell me the reason @OleV.V. – Ryuzaki L Nov 09 '19 at 20:42
  • 1
    I agree completely. Unfortunately they don’t do very often. Inexplicable downvotes are part of life on Stack Overflow according to my experience. I’m still a happy user. – Ole V.V. Nov 09 '19 at 21:09
2

Only if you know for sure that your program will never be used outside your own time zone, is it safe to use LocalDateTime. I receommend you use ZonedDateTime just in case.

In any case, using that LocalDateTime, the code for your logic as I have understood it is:

final int orderCancellationCutOffDays = 3;
final LocalTime orderCancellationCutOffTime = LocalTime.of(12, 0);

LocalDate orderDate = LocalDate.of(2019, Month.NOVEMBER, 6);
LocalDateTime orderCancellationCutOffDateTime
        = orderDate.plusDays(orderCancellationCutOffDays)
                .atTime(orderCancellationCutOffTime);
final LocalDateTime currentDateTime = LocalDateTime.now(ZoneId.of("America/Punta_Arenas"));
if (currentDateTime.isAfter(orderCancellationCutOffDateTime)) {
    System.out.println("This order can no longer be modified.");
} else {
    System.out.println("You can still modify this order,");
}

Of course substitute your own time zone where I put America/Punta_Arenas.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161