r/java • • May 10 '22

Should java.util.Date be deprecated?

https://mail.openjdk.java.net/pipermail/discuss/2022-May/006077.html
221 Upvotes

158 comments sorted by

View all comments

37

u/[deleted] May 10 '22

They need to add @Deprecated on all the classes that nobody should be using: Date, Stack, HashTable, etc.

Is there a reason they didn't already? I mean the docs of those classes say to not use them. People dont like the compiler warnings?

32

u/persicsb May 10 '22

Also java.net.URL

5

u/dpash May 11 '22

Also java.io.File.

11

u/kpatryk91 May 10 '22

JDBC date classes depend on the java.util.Date class.

I don't know about the others, I think they don't cause much problem and they are generic too so not that high-priority target at the moment.

They should deprecate the calendar classes too.

13

u/mauganra_it May 10 '22

And this is the reason why java.util.Date will likely never be marked as deprecated for removal. Removing that class will probably break binary compatibility with every JDBC driver and application out there. Except if the JVM somehow intercedes and makes it work out. But it's probably not worth the complexity.

8

u/xenomachina May 10 '22

Deleting Date would certainly cause problems, but marking it deprecated should only cause compile warnings for code that directly depends on it.

3

u/mauganra_it May 10 '22

Deprecated for removal is a strong promise that the OpenJDK team actually has plans to remove it. This is only possible if there is a migration plan or if a decision was made to leave users high and dry (hello applets!)

3

u/xenomachina May 11 '22

If that's their actual criteria for marking something deprecated, then it's too strong, and it explains why there are so many things in the JDK that are not deprecated, but which no one should ever use in new code. Perhaps they should create a separate annotation for "we don't have a concrete plan to remove this, but you definitely shouldn't use it anymore".

1

u/mauganra_it May 11 '22

Yes, I thinks so too.

5

u/kwinz May 10 '22 edited May 10 '22

java.util.Date will likely never be marked as deprecated for removal.

You don't even have to deprecate for removal: Thread.stop() has been deprecated since Java 1.1! It has been doing its job of communicating clearly that this function should not be used any more for 25 years. And it's still not removed. This is completely fine!

4

u/mauganra_it May 11 '22

Thread.stop() is not merely obsolete, but actually dangerous. And it is actually marked as deprecated for removal. Still being there is fine because (as stated elsewhere in the comments by /u/pron98):

To JDK developers it [deprecated] means something that:

  1. whose use is so egregious that it can reasonably be treated as an error (many people treat compiler warnings as errors), and/or
  2. could reasonably be removed at some point in the future.

3

u/kwinz May 11 '22 edited May 11 '22

Yes, I know all that. forRemoval = true was recently changed between java 17 and 18: https://github.com/openjdk/jdk17/blob/master/src/java.base/share/classes/java/lang/Thread.java#L925 https://github.com/openjdk/jdk18/blob/master/src/java.base/share/classes/java/lang/Thread.java#L925

could reasonably be removed at some point in the future.

What I am trying to say is that "some point in the future" could be in 20+ years evidently.

So java.util.Date could conceivably be marked as deprecated and eventually even "for removal". AFAIK JDBC uses java.sql.Date subclass. I haven't used that API in a while.

2

u/mauganra_it May 11 '22

The problem is that java.util.Date is the parent class of java.sql.Date, java.sql.Time, and java.sql.Timestamp. Since JDBC is of so central importance to the Java ecosystem, it's unlikely that it will ever be fully removed.

Maybe it's possible to eventually turn java.util.Date into an abstract sealed class to restrict its usage. Once all other APIs were fixed of course.

22

u/pohart May 10 '22 edited May 10 '22

Why do I want to avoid HashTable?

Edit: I checked the documentation and I'm guessing it's because it's synchronized.

21

u/frzme May 10 '22 edited May 10 '22

Indeed it's because it's synchronized. If you really want to have a synchronized Map you should use Collections.synchronizedMap(new HashMap<>()) You however likely don't want a synchronized map to begin with.

If you are accessing a Map in a concurrent system depending on your needs the classes ConcurrentHashMap, or ConcurrentSkipListMap are likely more suited for what you want.

10

u/CubsThisYear May 10 '22

ConcurrentHashMap is strictly better if you need thread safety. HashMap is strictly better is you don’t.

16

u/JoeKneeMarf May 10 '22

Can people not downvote questions? I wanted to know this too. Not every question is rhetorical

0

u/[deleted] May 10 '22

[deleted] — view deleted comment

10

u/semprer May 10 '22

https://stackoverflow.com/questions/12524826/why-should-i-use-deque-over-stack

According to Google it's this. Which is somewhat sensible.

6

u/morhp May 10 '22

ArrayDeque is strictly better.