Tbf, if you are a Junior and using Date instead of LocalDateTime, sooner or later you will stumble with a bizarre bug you can't explain, do a bizarre workaround that you can't even explain how it works, and have nightmares with that shit until someone tells you that LocalDateTime exists.
“No, we can’t use popup.js which is used by millions and ALSO supports IE11 because reasons and instead of spending an assload of developer time to validate the library, we spend an assload and a half on making our janky rip-off almost work”
Well, as far as I was told, LocalDateTime was created specifically to replace Date. It also supports timezones, and I think it has some comparison functions as well. Also, it's easy to integrate it in API contracts when using Spring thanks to Jackson. Maybe Instant can do all that as well, but I really don't know.
Actually, Date is kind of overloaded and used for too many different datetime concepts which is why the java.time classes where added in Java 8 in the first place. LocalDateTime, ZonedDateTime, Instant, etc. They all represent different concepts and thus are different distinct classes.
The TL;DR is that LocalDateTime has no concept of time zone whatsoever. You’ll probably want a ZonedDateTime if you want a timestamp in a certain time zone, or an Instant if you want a timestamp in UTC.
When I was first learning how to handle datetime stuff in Java 8, I’d constantly come back to this answer on StackOverflow (which can be overwhelming at first) but it made more and more sense to me each time I went back to it. I hope that helps.
Quite interesting. I had no idea LocalDateTime had no timezone. So when Jackson serializes it into a string, it serializes it in UTC by default? Or it serializes with the system timezone?
Here's the source code. It uses the ISO-8601 format without timezone modifier, e.g. "2021-10-25T00:23:12" – or in other words the same format as LocalDateTime.toString().
Good question. I haven’t used a modern version of Jackson with datetimes enough to know how LocalDateTimes get serialized. This StackOverflow link that I found when Googling might help. It looks like Jackson’s time zone handling for serialization/deserialization is configurable to some degree. But ultimately I’m kind of curious what the serialized datetime String looks like. I’m wondering if there is a time zone indicator in the String representation or not. My assumption is that there would (or at least should) be no time zone indicator.
There are definitely a few things not quite right about this, but to be fair the java.util.Date library is terrible and java.time.* can be really confusing if you haven't worked with it before.
Edit: just noticed java.time.* isn't used in this at all. Even better!
I would have stopped at line 1 and kicked it back.
compareDate ... Compare it to what? You can't tell what it's comparing to, or how, by the function name alone. And that's not a boolean returning naming convention.
WTF!
Damn. Name the function to isBeforeNow so no one has to read the javadocs or code, just to understand what it does.
Short of that, the actual code is awful. I'm going to have nightmares now. Thanks OP!. :)
This is the precise reason why codebases devolve into piles of shit.
100% yes, this function was irredeemable (which is why it’s unused after my refactoring). This was in a brand new microservice too. I think it’s pretty clear why every line of code they write goes through me before production.
135
u/VitorMM Oct 24 '21
Tbf, if you are a Junior and using Date instead of LocalDateTime, sooner or later you will stumble with a bizarre bug you can't explain, do a bizarre workaround that you can't even explain how it works, and have nightmares with that shit until someone tells you that LocalDateTime exists.
36
u/grknado Oct 24 '21
There are definitely a few things not quite right about this, but to be fair the
java.util.Datelibrary is terrible andjava.time.*can be really confusing if you haven't worked with it before.Edit: just noticed
java.time.*isn't used in this at all. Even better!13
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Oct 24 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
7
u/Bhurmurtuzanin Oct 24 '21
I mean it doesn't even compare dates, it checks whether date passed as an argument is before now().
2
u/m2thek Oct 25 '21
Even the name is awful; compare that date to what ??
4
u/[deleted] Oct 24 '21
I swear I have seen this more than once being done by senior devs.
At one job the company decided that everything will be EST,then the started having problems on Pacific time because all dates where a few hours off.
3
u/gravity_is_right Oct 24 '21
Check if you have similar interests, decide which one you find more attractive, check if there's more chemistry.
0
u/brainsto Oct 25 '21 edited Oct 25 '21
I would have stopped at line 1 and kicked it back.
compareDate... Compare it to what? You can't tell what it's comparing to, or how, by the function name alone. And that's not a boolean returning naming convention.WTF!
Damn. Name the function to
isBeforeNowso no one has to read the javadocs or code, just to understand what it does.Short of that, the actual code is awful. I'm going to have nightmares now. Thanks OP!. :)
This is the precise reason why codebases devolve into piles of shit.
1
u/KevinChopra2019 Oct 24 '21
It's not the same... Public static void main... Which is the application entry point...
1
u/Beentage Oct 24 '21
I had to use Date because our spring boot framework used Date in its Pojos
0
u/KevinChopra2019 Oct 24 '21
Public Boolean function returning a date format?...
0
u/Pangocciolo Oct 24 '21
The sad thing is that the violation of Pangocciolo's Principle isn't the worst part.