#Check if the given date is bigger than todays date in a java stream

1 messages · Page 1 of 1 (latest)

faint yew
#

Hello,
I have a stream of XmlGregorianCalendar dates and I want to see if it is possible to check if the newest date is bigger that todays day

LocalDate findnewestDateFronList(List<XmlGregorianCalendar> xmldates) {
      return Optional.ofNullable(xmldates)
      .orElseGet(Collections::emptyList)
      .stream()
      .max(XMLGregorianCalendar::compare)
      .map(this::parseToLocalDate)
       .orElse(null)    
}

is it possible to check if the date is bigger than todays date in the above code or should i do it in the parseToLocalDate() method. I whould like to throw an exception if the date is bigger than todays date.

cheers,

es
P.S first time using discord and I closed the post the previous time

dreamy cosmosBOT
#

This post has been reserved for your question.

Hey @faint yew! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

stable kayak
north cave
#

Yes, it is possible to check if the newest date in the list is bigger than today's date in the findnewestDateFronList method, before parsing it to a LocalDate. You can add an if statement before the map operation to check if the newest date is after today's date, and if it is, throw an exception.

Here's an example implementation:

LocalDate findNewestDateFromList(List<XmlGregorianCalendar> xmlDates) {
    XmlGregorianCalendar newestDate = Optional.ofNullable(xmlDates)
            .orElseGet(Collections::emptyList)
            .stream()
            .max(XMLGregorianCalendar::compare)
            .orElse(null);
    if (newestDate != null) {
        LocalDate newestLocalDate = parseToLocalDate(newestDate);
        LocalDate today = LocalDate.now();
        if (newestLocalDate.isAfter(today)) {
            throw new RuntimeException("Newest date is after today's date!");
        }
        return newestLocalDate;
    }
    return null;
}
#

This implementation first retrieves the newest date from the list and parses it to a LocalDate. Then it compares it with today's date using the isAfter method. If the newest date is after today's date, it throws a RuntimeException with an appropriate message. If the comparison is successful, it returns the newest LocalDate.

faint yew
north cave
# faint yew if I add the if block to a function is it possible to directly call it in the la...

No, it's not possible to call an if block inside a lambda function directly. The lambda function is a shorthand for a function that accepts an argument and returns a value. It's used to simplify code and make it more concise, but it still has to adhere to the basic principles of programming.

However, If you want to add the if block to a separate function and call it inside the lambda function, you can define a method and pass it as an argument to the max method.

north cave
north cave
#

You're welcome 😇

north cave
timber hazel
scarlet prism
#

/run

Runnable fn = () -> {
  if (1 > 0) {
    System.out.println("works");
  }
};
fn.run();
serene havenBOT
#

Here is your java(15.0.2) output @scarlet prism

works
scarlet prism
#

the lambda function is shorthand for any method.

scarlet prism
#

also i like how that doesn't even answer the question. the contents of the function don't matter for a method reference, just the signature