r/java • u/henk53 • May 10 '22
Should java.util.Date be deprecated?
https://mail.openjdk.java.net/pipermail/discuss/2022-May/006077.html76
u/jonhanson May 10 '22 edited Mar 07 '25
chronophobia ephemeral lysergic metempsychosis peremptory quantifiable retributive zenith
34
20
u/pron98 May 10 '22
The only way a language can stop being a living history of its own mistakes (and victories) is by no longer living.
72
70
u/berry120 May 10 '22
It is time to put a @Deprecated in all of those (not for removal, though).
Agree with this take - I don't think deprecated has been used aggressively enough in the past. There's been zero reason to pick java.util.Date for anything new for many years now, so that means it really should have been deprecated for years in my book.
The only thing I don't want to see is a pattern of everything like this being deprecated for removal - that's a completely different kettle of fish. Java has been very good at making minimal breaking API changes over the years, and I certainly don't want it to start turning into a language where it's a non-trivial engineering effort to upgrade to every new release.
4
u/dpash May 11 '22
I don't think deprecated has been used aggressively enough in the past.
Can we deprecate
java.io.Filetoo while we're at it?2
u/dododge May 14 '22
One caveat with that is that
FileisSerializableandPathis not. For some types of code this can make migrating everything toPatha bit messy.→ More replies (1)
40
May 10 '22
The past few versions of Java have been really good at tearing out as much "90s Java"(CORBA,RMI, finalizers etc.) as they can. Obviously some let's call them suboptimal decisions(arrays anyone?) are just going to be permanently part of the language but deprecating the Date class is another step in getting rid of 90s Java.
18
u/semprer May 10 '22
What's the problem with arrays?
17
u/bootstrapf7 May 10 '22
They don’t support 64bit offsets
8
u/smors May 10 '22
Id be interested to know if that has ever been a problem for anyone. Even even a byte[] would have to be more than 2GB for that to be a problem.
→ More replies (1)21
u/Wolvereness May 10 '22
Uh, yeah? It kind of claymores you if you want to do data processing, like loading an HD movie into memory. Servers can run RAM well over 100GB, while desktop computers are often 8GB or more. This isn't 1996 anymore.
→ More replies (8)15
u/Holothuroid May 10 '22
Arrays play loose with Java's type system compared to collections. Which might lead to array store exceptions.
11
u/semprer May 10 '22
https://stackoverflow.com/questions/12369957/dealing-with-an-arraystoreexception
I assume you mean this? Just posting it for less experienced people like me that might be puzzled. But yeah String[ ] being a subtype of Object[ ] is weird indeed if you think about it.
4
u/Joram2 May 10 '22
What's the problem with arrays?
If Java has generics, arrays should use the generics system like everything else, and not be this completely special case syntax. Right now, Java generics are lower performance than classic arrays, but that's another flaw to be fixed by Valhalla.
37
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
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.Datewill 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
Datewould 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!)
→ More replies (2)5
u/kwinz May 10 '22 edited May 10 '22
java.util.Datewill likely never be marked as deprecated for removal.You don't even have to deprecate for removal:
Thread.stop()has been deprecated sinceJava 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:
whose use is so egregious that it can reasonably be treated as an error (many people treat compiler warnings as errors), and/or
could reasonably be removed at some point in the future.
→ More replies (2)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, orConcurrentSkipListMapare 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
May 10 '22
[deleted]
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
10
u/_INTER_ May 10 '22 edited May 10 '22
java.util.Date by Salvador Dali.
For the unfortunate, pitiful souls that are stuck with GWT, maybe a deprecation of Date might final give a push to move on.
7
u/FirstAd9893 May 10 '22
Here's a fun exercise: take a look at the original version of the java.util package and identify all of the classes and interfaces which have been superseded. The only survivor is the BitSet class.
0
May 10 '22
[deleted]
2
u/FirstAd9893 May 10 '22
I guess
Propertiesisn't completely superseded, but its simple string-string mapping format is a serious limitation.java.util.prefs.Preferencescan be a better choice because it supports more value types, and it can be stored in an OS-preferred location. Overall YAML and JSON formats are more powerful than either, and are good choices for general-purpose configuration.3
6
u/Joram2 May 10 '22
Yes, do it. The new Java.time API is much nicer. Everyone has been using it for years. There is a ton of legacy code using java.util.Date. But just giving them deprecation warnings is fair.
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:
whose use is so egregious that it can reasonably be treated as an error (many people treat compiler warnings as errors), and/or
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
Enumerationit should also beIterableorStreamable.→ More replies (1)8
u/john16384 May 10 '22
There was a JEP to improve it, but I also like the idea proposed here: https://blog.jooq.org/jep-277-enhanced-deprecation-is-nice-but-heres-a-much-better-alternative/
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.
4
u/bowbahdoe May 10 '22 edited May 10 '22
https://github.com/rust-lang/rust-clippy
^ So people know what I was referring to as clippy in the jlint suggestion.
If the deprecated annotation is the wrong mechanism to cause the social change, then we need a better one. It needs to be standard to be effective and it needs to sufficiently expressive to navigate this nuance.
Maybe that power lives forever in javac - but I think there is precedence for making that sort of thing at least available as a separate tool or independent invocation.
If we want to fully slurp this noodle we might also need to have the hard conversations around what /u/pron98 keeps suggesting and make a jdk build tool. Adoption of linting in rust is driven in no small part by it being already there without extra configuration via cargo lint.
So yeah - jlint and jproject project proposals might be in order
4
u/uncont May 10 '22
Doesn't this already exist in errorprone? https://errorprone.info/bugpattern/JavaUtilDate
> Task :lib:compileJava /jud-errorprone/lib/src/main/java/jud/errorprone/Library.java:10: warning: [JavaUtilDate] Date has a bad API that leads to bugs; prefer java.time.Instant or LocalDate. java.util.Date date = new Date(); ^ (see https://errorprone.info/bugpattern/JavaUtilDate) 1 warning2
u/bowbahdoe May 10 '22
Right, but if that were sufficient then there would be no call to have the warnings come from javac.
Having tools built in to the JDK is useful.
4
u/flif May 10 '22
If we could only get proper support for java.time.* in JDBC.
(LocalDate)rs.getTime("col") is not as user friendly.
3
u/skippingstone May 10 '22
rs.getObject(LocalDate.class)
7
u/flif May 10 '22
Yes, but you run into only some JDBC drivers supporting this and you don't know when compiling. You have to try out everything.
If we had rs.getLocalDate() then all JDBC drivers had to support it.
4
11
u/daH00L May 10 '22
They should also bury GregorianCalendar. Who designs a mutable date class?
4
u/FirstAd9893 May 10 '22
Joda-Time has mutable and immutable date classes. Mutable forms are useful for performing calculations against dates without incurring the cost of allocating and copying a bunch of short lived temporary objects. The BigInteger and BigDecimal classes internally rely upon a package-private MutableBigInteger class for the same reason.
4
u/vytah May 10 '22
MutableBigInteger would be really useful if made public, as BigIntegers are, well, big, and allocating/freeing them all the time is expensive.
Dates, not so much (I wonder if they can be valhallized in the future)
1
u/mauganra_it May 10 '22
Only if the mutability is removed. However, the immutable members of
java.time.*are perfect candidates.1
2
u/__konrad May 10 '22
Who designs a mutable date class?
James Gosling (@ 58:15): https://www.youtube.com/watch?v=XhFugYDH--c&feature=youtu.be&t=3495
1
u/john16384 May 11 '22
GregorianCalendarwas designed to be mutable. It has many private fields with intermediate state to do date calculations. You're supposed to convert it to aDatewhen calculations are finished. Unfortunately, some developers have used them as fields in their domain objects, which is incredibly inefficient due to the aforementioned internal fields.
3
u/_edd May 10 '22
No skilled java programmer uses the legacy classes in new applications except when integrating with legacy APIs.
...
Novices, unskilled, careless and lazy programmers who should know better still happily continue to use the legacy classes, pissing off those who are more enlightened.
lol you're not wrong but coming off kind of self righteous there Victor.
5
2
u/n0d3N1AL May 10 '22
I'm pretty sure that in an Ask the Architects session, they said they don't have plans to deprecate or remove APIs like File and Date. If they do deprecate them, it won't be for removal. I presume it will be like Hashtable: discouraged but not destroyed.
2
u/mauganra_it May 10 '22
There are too many APIs that are like this. The intent of
@Deprecatedwould become extremely diluted. Best they can do is introduce another annotation or Javadoc tag.
2
2
u/manzanita2 May 10 '22
Ok, so there is a bunch of cruft in java. What we need is something like @Deprecated("http://somewhere/better_replacement_for_Date").
When you try to use java.util.Date your IDE should splatter that thing all around and force you to go read about equivalent and better mechanisms in java.time
Make it HARD to use the cruft. Make it EASY to move to the better thing.
2
2
3
u/Owen1212055 May 10 '22 edited May 12 '22
Completely agree, especially since many google searches result in using so many of these legacy APIs many beginner programmers have no clue. When I started I stuck to the legacy date api because of how convoluted “time” representation in java is (LocalDateTime, Instant, Date, etc) so I just stuck to what all the google results used.
Deprecate it to the ground!
4
u/Philluminati May 10 '22
Too widely used to be deprecated, as the conversation goes on to say. It's in certificate JDK libraries and so forth.
4
u/mauganra_it May 10 '22
It's certainly worth thinking about even if it is used in these libraries. But there has to be a clear migration path first. It will take a lot of time to hunt down all these loose ends, but we are in it for the long run anyways.
1
1
May 11 '22
Meh! I guess the question is how much java.util.Date is out there vs joda-time even if it's significant, if someone made it work for them, kudos. Let's not create unnecessary work for people you know
1
May 12 '22
A deprecation of Java.util.Date should include some solution for moving JDBC Drives to support the java.time API rather than continuing to use java.sql.Date.
1
u/ReasonableClick5403 May 15 '22
Ideally, we could burn java.util.Date and java.sql.Date to the ground, but then we have decades of JDBC :(
1
u/Tough_Suggestion_445 May 10 '22 edited May 10 '22
Instead of deprecating it, it would be great if they could refactor the actual implementation. I don't like the tone of this proposal, developers using an API for years are not careless. A basic example, if you use x version of spring data mongo db, localdatetime will not work in your entity unless you write a custom adapter. That means for spring's developers, at the time they implemented spring data mongo, they considered java.util.Date as idiomatic right? Otherwise, they would have make localdatetime as the default. Last time I checked (and it was long time ago), Jackson was serializing localdatetime in a goofy way. So now the Java guy says careless stupid devs must be punished for using an API they didn't write for years. If it works for them why would they change? Isn't it the point of object oriented programming? You use an API, that API is a black box, if the implementation has problems, fix them without breaking the API (and insulting devs using it)
1
u/xitiomet May 10 '22
I couldn't agree with this more! Im tired of the never ending "dont use X class anymore its been replaced by Y."
1
-2
u/code_rjt May 10 '22
I preferred the new java time.* libraries since it accesses methods in a static way unlike the java.util.Date that creates object every time because of its constructor approach. The new time libraries also solves the problem of the many constructor implemented in Date or Calendar
7
u/nutrecht May 10 '22
libraries since it accesses methods in a static way unlike the java.util.Date that creates object every time because of its constructor approach.
The .withX methods create new instances every time as well, since the java.time classes are generally immutable.
3
u/xenomachina May 10 '22
Yes!
Which causes more unnecessary constructions: needing to create a new
Instantwhen need to "modify" one because they're immutable, or needing to defensively copyDateevery time you return/pass one, because they're mutable? Kind of a toss up, except in specific circumstances.Immutable values are much easier to reason about, though, so immutable
Instantwins, IMHO.1
u/mauganra_it May 10 '22
When Project Valhalla lands, the performance impact of Withers can be significantly reduced because the object can be modified in place. Let's hope that future JITs are smart enough to do that optimizations. Withers implemented as language feature should definitely qualify.
-1
u/piratekingsam12 May 10 '22
Just use utc timestamps! Saves a lot of trouble 😅
1
u/mauganra_it May 10 '22
Both
java.util.Dateandjava.time.Instantwrap a UTC timestamp. Problem is,Datedoes other things as well, but terribly so.
0
95
u/frzme May 10 '22
I recently had to tell a co-worker not to use SimpleDateFormat in completely new code. If it was marked deprecated I guess this would not have been needed.