Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      CodeSOD: Functionally, a Date

      September 16, 2025

      Creating Elastic And Bounce Effects With Expressive Animator

      September 16, 2025

      Microsoft shares Insiders preview of Visual Studio 2026

      September 16, 2025

      From Data To Decisions: UX Strategies For Real-Time Dashboards

      September 13, 2025

      DistroWatch Weekly, Issue 1139

      September 14, 2025

      Building personal apps with open source and AI

      September 12, 2025

      What Can We Actually Do With corner-shape?

      September 12, 2025

      Craft, Clarity, and Care: The Story and Work of Mengchu Yao

      September 12, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Optimizely Mission Control – Part III

      September 14, 2025
      Recent

      Optimizely Mission Control – Part III

      September 14, 2025

      Learning from PHP Log to File Example

      September 13, 2025

      Online EMI Calculator using PHP – Calculate Loan EMI, Interest, and Amortization Schedule

      September 13, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      I Ran Local LLMs on My Android Phone

      September 16, 2025
      Recent

      I Ran Local LLMs on My Android Phone

      September 16, 2025

      DistroWatch Weekly, Issue 1139

      September 14, 2025

      sudo vs sudo-rs: What You Need to Know About the Rust Takeover of Classic Sudo Command

      September 14, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Tech & Work»CodeSOD: Functionally, a Date

    CodeSOD: Functionally, a Date

    September 16, 2025

    Dates are messy things, full of complicated edge cases and surprising ways for our assumptions to fail. They lack the pure mathematical beauty of other data types, like integers. But that absence doesn’t mean we can’t apply the beautiful, concise, and simple tools of functional programming to handling dates.

    I mean, you or I could. J Banana‘s co-worker seems to struggle a bit with it.

    <span class="hljs-comment">/**
     * compare two dates, rounding them to the day
     */</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">compareDates</span><span class="hljs-params">( LocalDateTime date1, LocalDateTime date2 )</span> {
        List<BiFunction<LocalDateTime,LocalDateTime,Integer>> criterias = Arrays.asList(
                (d1,d2) -> d1.getYear() - d2.getYear(),
                (d1,d2) -> d1.getMonthValue() - d2.getMonthValue(),
                (d1,d2) -> d1.getDayOfMonth() - d2.getDayOfMonth()
            );
        <span class="hljs-keyword">return</span> criterias.stream()
            .map( f -> f.apply(date1, date2) )
            .filter( r -> r != <span class="hljs-number">0</span> )
            .findFirst()
            .orElse( <span class="hljs-number">0</span> );
    }
    

    This Java code creates a list containing three Java functions. Each function will take two dates and returns an integer. It then streams that list, applying each function in turn to a pair of dates. It then filters through the list of resulting integers for the first non-zero value, and failing that, returns just zero.

    Why three functions? Well, because we have to check the year, the month, and the day. Obviously. The goal here is to return a negative value if date1 preceeds date2, zero if they’re equal, and positive if date1 is later. And on this metric… it does work. That it works is what makes me hate it, honestly. This not only shouldn’t work, but it should make the compiler so angry that the computer gets up and walks away until you’ve thought about what you’ve done.

    Our submitter replaced all of this with a simple:

    <span class="hljs-keyword">return</span> date1.toLocalDate().compareTo( date2.toLocalDate() );
    

    [Advertisement]
    ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.

    Source: Read More 

    news
    Facebook Twitter Reddit Email Copy Link
    Previous ArticleCreating Elastic And Bounce Effects With Expressive Animator

    Related Posts

    Tech & Work

    Creating Elastic And Bounce Effects With Expressive Animator

    September 16, 2025
    Tech & Work

    Microsoft shares Insiders preview of Visual Studio 2026

    September 16, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    CVE-2025-37989 – Linux Kernel Phy LED Trigger Memory Leak Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    The Super Weight in Large Language Models

    Machine Learning
    One of the best gaming chairs we’ve given a 5-star rating to is now available to purchase in the UK

    One of the best gaming chairs we’ve given a 5-star rating to is now available to purchase in the UK

    News & Updates

    The 30 UX Principles Every Designer Should Know

    Web Development

    Highlights

    Linux

    Debian 13 “Trixie” è Arrivata: Guida ai Primi Errata e Aggiornamenti Necessari

    August 11, 2025

    La distribuzione GNU/Linux Debian, celebre per la sua stabilità e solidità, ha appena annunciato il…

    CVE-2025-52553 – Authentik RAC Token Session Hijacking Vulnerability

    June 27, 2025

    CVE-2025-4902 – D-Link DI-7003GV2 Information Disclosure Vulnerability

    May 19, 2025

    Fox Sports not Working: 7 Quick Fixes to Stream Again

    August 31, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.