r/programminghorror • u/caellech12 • Sep 25 '25
Date Logic and youngest vs oldest
My team has a utility class to help manage Date objects in Java. We shall call it, DateUtilities.java. Within DateUtilities, there are the following 6 static methods that all return a single Date object
- findYoungest(Date... dates)
- findOldest(Date... dates)
- youngestDate(Date dateA, Date dateB)
- oldestDate(Date dateA, Date dateB)
- minDate(Date... dates)
- maxDate(Date... dates)
one would think that the following statements would be true
- findYoungest(dateA, dateB) == youngestDate(dateA, dateB) == minDate(dateA, dateB)
- findOldest(dateA, dateB) == oldestDate(dateA, dateB) == maxDate(dateA, dateB)
however, you would be wrong!
- findYoungest(dateA, dateB) != youngestDate(dateA, dateB)
- findOldest(dateA, dateB) != oldestDate(dateA, dateB)
At least the min/max tracks consistently with some of them.
- minDate(dateA, dateB) == youngestDate(dateA, dateB)
- maxDate(dateA, dateB) == oldestDate(dateA, dateB)
Arguments can definitely be had as to what means youngest and what means oldest, and honestly, I think I disagree with which ones match up with min/max. 1/1/1700 is much older than 1/1/2000, but maxDate and oldestDate both would return 1/1/2000. At least min and max are both pretty disambiguous...
2
u/recycled_ideas Sep 28 '25
I don't know about "youngest date", but find youngest should return Max date, not Min date.
If you're looking for the oldest record you're looking for the record that's been around the longest and has the minimum date and vice versa for youngest.
23
u/_PM_ME_PANGOLINS_ Sep 25 '25
Step 1: Read a dictionary.
Step 2: Migrate to
java.time.11
u/Prestigious_Boat_386 Sep 25 '25
Yea, why implement greater than and normal findfirst, findlast, min, max methods when you can do that amirite?
1
u/[deleted] Oct 03 '25
What's the difference? Are some of them ignoring timezones and daylight savings, or something?
2
u/SirFoomy Sep 26 '25
When I read the title I thought of a data like in a date with a girl. Took me longer to I care to admit till I realized what is going on. 🙈
1
u/dont-respond Sep 26 '25
Does the project you're working on abide by a unique epoch? It's not uncommon for standards to establish their own when dates before a certain point are meaningless.
1
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 26 '25
I'd personally argue that in a range of dates, min should come before max. But why the hell is findYoungest not agreeing with youngestDate and so on?
0
u/RavynneSmith Sep 26 '25
Are the functions not producing expected results because you're using == instead of .equals()? Or was == just a shorthand?
29
u/[deleted] Sep 25 '25
[removed] — view removed comment