- Feature Articles
- CodeSOD
-
Error'd
- Most Recent Articles
- It's About Time
- Scrollpocalypse
- Fast Quick
- 12345
- Said Different
- Ai Dios Mio
- Unrewarding
- Good Time
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It's even worse.
Date.getTime()returns the number of milliseconds since the epoch. That gets converted into aTimestampwith the same timestamp.TimestampextendsDateso itsgetTime()does the same. The entire block can be rewritten to justDate date = new Date();To get the time part of a
DateorTimestampthere are methods to get separate fields but those work half-baked as best. Using aCalendarwas the old way to go, but since Java 8 any code usingDate,TimestamporCalendarshould preferably be rewritten to use classes from thejava.timepackage.Admin
These methods I mentioned are so half-baked they've been deprecated since Java 1.1, way back in 1997.
Admin
It's even more worse. The Timestamp class is in java.sql package and therefore meant to be used for accessing SQL databases via JDBC. It's not a general purpose class.
And it adds a nanosecond value to it, which doesn't make any sense here.
Admin
I feel every release of Java has something reinvented that doesn't need reinventing. The time and date libraries are no exception. I sympathize with the original coder.
Admin
You have to respect code which comes with warnings about not exposing it to bright light, not getting it wet, and never ever running it at exactly midnight.
("Respect" is the word that means "Run away from something as fast as you can", right?)
Admin
To make it even worser, why not just use this comfortable one-liner?
Date date = new Date((new Timestamp(new Date().getTime())).getTime());
You could even make a song from that!
Admin
Maybe it used to also use the date field.
Admin
@Rob That also depends on whether "Date" is java.uil.Date or java.sql.Date. Because having two classes in the core language which have the same name (but one subclasses the other) never gets old.
Admin
You're welcome to that feeling, but the old Date and Calendar classes were really dreadful (and anyone who needed to do anything serious with times and dates used the Joda-Time library). The redesign in Java 8 was very much for the best.