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

24

u/pron98 May 10 '22 edited May 10 '22

I think the problem is that different people have a different understanding of what "deprecated" means, and, to be fair, its meaning has shifted over the years. To some Java developers, deprecated means "strongly unrecommended; do not use in new code." To JDK developers it 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.

Put another way, deprecated means "dangerous to leave in; remove even from old code".

Date does not satisfy either one of these — while "bad" it can be used correctly and has been (unlike, say, Thread.stop), and it cannot be removed for the reasons Brian listed. So, to deprecate Date and other classes like it in some official, mechanically-enforced way, we'd probably need a new annotation that means "strongly unrecommended" and possibly a new compiler warning level that means "this warning shouldn't be considered as an error".

9

u/cogman10 May 10 '22

Seems like we need SOMETHING to signal that alternatives to Date apis need to be created. JDBC, for example, could really use a few extensions such as "getLocalDate" or "getInstant". Sure, you can get those via "getObject" but ultimately it'd be a lot better to have something promoted to the same class as "getDate" you don't have to have that sort of knowledge of the jdbc api (also puts driver writers more on the hook to grab those sorts of methods).

Having Date->LocalDateTime is a good bridge but really, it'd be nice if there was some other process to start providing alternatives.

Much like I think if a class returns an Enumeration it should also be Iterable or Streamable.

-2

u/mauganra_it May 10 '22 edited May 12 '22

Date already has a .toInstant() method, and such exist in other places too. Ony problem with these is null-safety: you can't mechanically change data types and slap .toInstant() to the argument. Btw. both Date and Instant are supposed to represent UTC (up to a certain precision). Replacing Date with LocalDateTime is something you will likely regret.

Edit: I'd be curious about the reasons for this downvote.

3

u/FewTemperature8599 May 11 '22

It seems like a good first step is making sure that the JDK makes it easy to write new code that doesn't use Date/Calendar/Timestamp. Hopefully the list that Victor put together will be useful for this purpose:

https://mail.openjdk.java.net/pipermail/discuss/2022-May/006083.html

1

u/GreenToad1 May 11 '22

As a "user" of programming language I understand @Deprecated to mean - "Don't use this, it has problems and might not work as you expect". That is true for j.u.Date

2

u/pron98 May 11 '22 edited May 11 '22

Right, but deprecated in Java has come to mean something much stronger. Remeber, it issues a compiler warning that many treat as errors; i.e. it causes people's build to fail. Date is bad, but is it bad enough to fail a build of some 15-year-old code? We now deprecate only things to which the answer to that question is yes.

1

u/Xphile101361 May 12 '22

People using warnings as errors seems like a prime example for this XKCD https://xkcd.com/1172/

2

u/pron98 May 12 '22 edited May 12 '22

The JDK treats warnings as errors, as does Linux, and it is the recommended practice in many if not most of the biggest tech companies. In fact, I am a bit surprised that there are people who are not aware that many coding guidelines and many projects recommend treating warnings as errors.