1

How can I set up that Android Studio always imports java.util.Date? It ALWAYS auto-imports java.sql.Date, which I never use, which then always gives me errors later if I don’t explicitly check the import statements (which of course nobody ever does). It happens each and every time I do something like

Date d = new Date();

Which is a valid constructor for both. Is there some kind of import priority?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
breakline
  • 5,776
  • 8
  • 45
  • 84
  • What do you call auto-import? – Eugen Pechanec Apr 22 '18 at 04:36
  • 2
    As an aside consider neither using `java.util.Date` nor `java.sql.Date`. Those classes are long outdated and have serious design problems. Today `Instant` and the other classes from [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) are a more appealing choice. Yes, you can use them on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Apr 22 '18 at 07:51

2 Answers2

2

I had the same problem and turned out java.util.Date was in "Exclude from Import" list in Settings (Settings- Auto Import - Exclude from Import). Clear this list and IDE will be asking which lib to import. Clear the list, add a java.SQL.Date and IDE will be importing java.util.Date asking nothing.

Ivan C
  • 116
  • 1
  • 4
0

First see if there's not any import of import java.sql.Date. Or any other Date class (Important)

Then type Date, you will see drop down list, let android studio find all classes named Date. Then choose the one you require in your class.

enter image description here

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • I don't think this answers the question. There is an IDE setting for "auto-import" that needs turned off – OneCricketeer Apr 22 '18 at 07:54
  • To be honest I like auto-import a lot. I can type fast and I like that I dont need t explicitly import something. Writing something like new Date() takes less than half a second. Manually importing it would take several seconds. If you are a professional developer these add up pretty fast and so I use a lot of shortcuts and auto-generators etc. My problem is that even though all my project files use java.util.Date Android Studio still insists on importing java.sql.Date in a new file... – breakline Apr 22 '18 at 08:25
  • Let google implement this intelligent sense for you :) – Khemraj Sharma Apr 22 '18 at 08:48