Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,227 questions

56,129 answers

573 users

How to compare two dates in Groovy

2 Answers

0 votes
Date d1 = new Date("06/10/2020")
Date d2 = new Date("12/10/2020")

println d2.compareTo(d1)
println d1.compareTo(d2)

Date d3 = new Date("06/10/2020")
println d1.compareTo(d3)



    
/*
run:
     
1
-1
0
     
*/

 



answered Oct 6, 2020 by avibootz
0 votes
/*
    Compare two dates in Groovy
    ---------------------------
    This program demonstrates how to compare two dates using Groovy's
    built‑in java.time API. It uses a modular design, clear comments,
    and a full test suite.

    Concepts:
        - Parsing dates safely
        - Comparing LocalDate objects
        - Handling invalid formats
        - Edge‑case testing

    Architecture notes:
        - A dedicated function handles parsing.
        - Another function performs comparison.
        - Main runs multiple predefined test cases.
        - No external dependencies; only standard library.

    Performance notes:
        - LocalDate comparisons are O(1).
        - Parsing is fast and predictable.
        - Memory usage is minimal.

    Pitfalls:
        - Invalid date strings must be handled.
        - Comparing raw strings is unsafe; always convert to LocalDate.
        - Timezones are not used here; LocalDate is date‑only.
*/

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeParseException

/**
 * Safely parse a date string into a LocalDate.
 *
 * Supported format: YYYY-MM-DD
 *
 * Error handling:
 *   - Returns null if the date is invalid.
 */
LocalDate parseDate(String dateStr) {
    try {
        return LocalDate.parse(dateStr, DateTimeFormatter.ISO_LOCAL_DATE)
    } catch (DateTimeParseException ignored) {
        return null
    }
}

/**
 * Compare two LocalDate objects.
 *
 * Returns:
 *   - "earlier"
 *   - "later"
 *   - "equal"
 */
String compareDates(LocalDate d1, LocalDate d2) {
    if (d1 < d2) return "earlier"
    if (d1 > d2) return "later"
    return "equal"
}

/**
 * Run a single test case:
 *   - Parse both dates
 *   - Handle invalid input
 *   - Compare if valid
 */
Map runTestCase(String date1, String date2) {
    def d1 = parseDate(date1)
    def d2 = parseDate(date2)

    if (!d1 || !d2) {
        return [date1: date1, date2: date2, result: "invalid date format"]
    }

    return [date1: date1, date2: date2, result: compareDates(d1, d2)]
}

/**
 * Main test suite:
 *   - Multiple test cases
 *   - Includes edge cases
 *   - Prints results cleanly
 */
void main() {
    def tests = [
        ["2024-01-01", "2024-01-02"],   // earlier
        ["2024-01-02", "2024-01-01"],   // later
        ["2024-01-01", "2024-01-01"],   // equal
        ["1999-12-31", "2000-01-01"],   // millennium boundary
        ["2024-02-29", "2024-03-01"],   // leap year
        ["2024-02-29", "2023-02-28"],   // leap vs non-leap
        ["2024-13-01", "2024-01-01"],   // invalid month
        ["2024-00-10", "2024-01-01"],   // invalid month
        ["2024-01-32", "2024-01-01"],   // invalid day
        ["abcd-ef-gh", "2024-01-01"],   // invalid format
        ["2024-01-01", "abcd-ef-gh"],   // invalid format
    ]

    println "Date comparison tests:\n"

    tests.each { t ->
        def result = runTestCase(t[0], t[1])
        println "Compare '${result.date1}' vs '${result.date2}' → ${result.result}"
    }
}

main()



/*
run:

Date comparison tests:

Compare '2024-01-01' vs '2024-01-02' → earlier
Compare '2024-01-02' vs '2024-01-01' → later
Compare '2024-01-01' vs '2024-01-01' → equal
Compare '1999-12-31' vs '2000-01-01' → earlier
Compare '2024-02-29' vs '2024-03-01' → earlier
Compare '2024-02-29' vs '2023-02-28' → later
Compare '2024-13-01' vs '2024-01-01' → invalid date format
Compare '2024-00-10' vs '2024-01-01' → invalid date format
Compare '2024-01-32' vs '2024-01-01' → invalid date format
Compare 'abcd-ef-gh' vs '2024-01-01' → invalid date format
Compare '2024-01-01' vs 'abcd-ef-gh' → invalid date format

*/

 



answered 16 hours ago by avibootz
...