• Rob (unregistered)

    It's even worse. Date.getTime() returns the number of milliseconds since the epoch. That gets converted into a Timestamp with the same timestamp. Timestamp extends Date so its getTime() does the same. The entire block can be rewritten to just Date date = new Date();

    To get the time part of a Date or Timestamp there are methods to get separate fields but those work half-baked as best. Using a Calendar was the old way to go, but since Java 8 any code using Date, Timestamp or Calendar should preferably be rewritten to use classes from the java.time package.

  • Rob (unregistered)

    These methods I mentioned are so half-baked they've been deprecated since Java 1.1, way back in 1997.

  • (nodebb) in reply to Rob

    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.

  • (nodebb)

    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.

  • (nodebb)

    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?)

  • Officer Johnny Holzkopf (unregistered) in reply to Melissa U

    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!

  • Loren Pechtel (unregistered)

    Maybe it used to also use the date field.

  • SG (unregistered) in reply to Rob

    @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.

  • (nodebb) in reply to Michael Wheeler

    I feel every release of Java has something reinvented that doesn't need reinventing. The time and date libraries are no exception.

    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.

Leave a comment on “The Date and Time and Time”

Log In or post as a guest

Replying to comment #:

« Return to Article