5 ms·
I think the article gets at this, but the biggest "eureka" I had was when I realized that java.util.Date is just sugar around a long value (ms since the epoch).
by hn_throwaway_99 10y ago
I think the article gets at this, but the biggest "eureka" I had was when I realized that java.util.Date is just sugar around a long value (ms since the epoch). Thus, wherever you use a Date, if you would NOT consider using a long there, then Date is the wrong choice. E.g. timestamp for log entry records - yeah, makes sense. Days on a schedule app - definitely wrong choice.
Of course, as the article and other commenters have said, just use jodatime or the Java 8 equivalent.
- lsd5you 10y agoThe basic problem with this then being that all conversions between the two concepts are implicit. Dates are points on calendars and there is complexity to deal with and assumptions to be made when converting to points in time.
- adrianmsmith 10y agoWhat hn_throwaway_99 is saying is that there aren't two concepts. For java.util.Date there's only one concept, which is that it wraps a long number of milliseconds since the epoch. So it has nothing to do with dates on a calendar, despite its name.
- nullnilvoid 10y agoGood summary of the article.
- kpil 10y agoOr just use a long...
- Tharkun 10y agoYeah. Have fun working out date arithmetic, leap seconds and time zones on your own. What could possibly go wrong.
- kpil 10y agoNo, that would be stupid - but you can't use a Date for that either.
- Sharlin 10y agoYou can; there are getters and setters for the different components. And because Date is lenient by default, you can do eg. date.setMonth(date.getMonth() + 1); and it will do the Right Thing even if the month is December.
- jdmichal 10y agoThe `get` and `set` methods on `Date` have been deprecated since 1.1... In favor of `Calendar`, which is all `Date` is using under the hood to perform such calculations anyway.
- Sharlin 10y agoTrue that, somehow totally slipped from my mind. To my defense, the last few times I've used java.util.Date it has been with GWT which doesn't provide anything better at least out of the box.
- jdmichal 10y agoNo worries. The whole thing was a tire-fire from the get-go, and I certainly don't blame you.
- bluestreak 10y agoIt should be encouraged to use long. Not only it performs better, but it is also simpler to understand and use compared to obscurity of java.util.Date. I've written all of date arithmetic using only long values and I can share the code if anyone is interested
- adrianmsmith 10y agoBefore Java 8 and Jodatime, it was better to use java.util.Date than a long, for type-checking and self-documentation. If a function accepts a java.util.Date and you pass an id of some database object by mistake, you'll get a compile error. If the function accepts a long you won't. If a function accepts a java.util.Date and the parameter is called "start" you know it accepts an instant in time, if it accepts a long you might think it could be something else. Yes, the class is terribly named, but before there was Java 8 and Jodatime, it was better to use it than a long.
- phyzome 10y agoAnd rather importantly, with a dedicated type you're a bit less likely to encounter and off-by-a-thousand error when there's confusion about millis vs. seconds since Unix epoch.
- bluestreak 10y agoIt is not always a good idea to box primitives so that you can have compiler point out mistakes. Yes, it is good for developer once or twice, but end user will suffer performance hog forever.
- nradov 10y agoModern JVMs are pretty good at optimizing away the overhead of boxing primitives. So don't throw away the advantages of type checking unless you have hard evidence of an actual performance problem.
- adrianmsmith 10y agoThis is a trade-off between "should the program work" or "should the program be fast". The users would prefer a slow program that works, over a program that crashes or corrupts data.
- nrinaudo 10y agoBut java.util.Date is mutable where a long isn't, and that's a problem. People can mutate a Date from under your feet if they manage to get a reference to it. This has a lot of far reaching implications, but essentially, unless you protectively copy all values of type Date you get or give, you can't trust your own internal state. You can't write a legal equals or hashCode implementation that uses a date field. The list goes on. Don't do java.util.Date.
- hn_throwaway_99 10y agoIt would usually be better to use an Instant ( https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html https://docs.oracle.com/javase/8/docs/api/java/time/Instant.... ), which helps make your code self-documenting and provides a whole set of methods to deal with the fact that your long actually represents a point in time along a timeline.