You probably have some other "Date" class imported somewhere (or you have a Date class in you package, which does not need to be imported). With "import java.util.*" you are using the "other" Date. In this case it's best to explicitly specify java.util.Date in the code.
Or better, try to avoid naming your classes "Date".
Answer from sleske on Stack OverflowWhat's the difference between import java.util.*; and import java.util.Date; ? - Stack Overflow
What does import java.util.* mean?
Should I use import java.util.* for bringing in just one class? - Software Engineering Stack Exchange
In Java, what does the import java.util.*; statement do?
Question 1Answer
a.
Imports all classes from the java.util package.
b.
Imports only the java package.
c.
Imports all classes from the java package.
d.
Imports nothing.
You probably have some other "Date" class imported somewhere (or you have a Date class in you package, which does not need to be imported). With "import java.util.*" you are using the "other" Date. In this case it's best to explicitly specify java.util.Date in the code.
Or better, try to avoid naming your classes "Date".
The toString() implementation of java.util.Date does not depend on the way the class is imported. It always returns a nice formatted date.
The toString() you see comes from another class.
Specific import have precedence over wildcard imports.
in this case
import other.Date
import java.util.*
new Date();
refers to other.Date and not java.util.Date.
The odd thing is that
import other.*
import java.util.*
Should give you a compiler error stating that the reference to Date is ambiguous because both other.Date and java.util.Date matches.
I'm new to java, I've been seeing import java.util.* across the web on forums and other sites, I'm curious what does it mean? Is it anything like import java.util.Scanner or Random, etc?