I just want to output current and I wrote
import java.util.*;
at beginning, and
System.out.println(new Date());
in the main part.
But what I got was something like this:
Date@124bbbf
When I change the import to import java.util.Date;
the code works perfectly, why?
====================================
The problem was, OK, my source file was "Date.java", that's the cause.
Well, it is all my fault, I confused everybody around ;P
And thanks to everyone below. It's really NICE OF YOU ;)
The java. util. Date class represents a particular moment in time, with millisecond precision since the 1st of January 1970 00:00:00 GMT (the epoch time). The class is used to keep coordinated universal time (UTC).
so import java. util. *; imports all of those classes. java.io contains things like FileReader, InputStream, OutputStream etc.
Java util package contains collection framework, collection classes, classes related to date and time, event model, internationalization, and miscellaneous utility classes. On importing this package, you can access all these classes and methods.
reflect Provides classes and interfaces for obtaining reflective information about classes and objects. java. util Provides the collections framework, formatted printing and scanning, array manipulation utilities, event model, date and time facilities, internationalization, and miscellaneous utility classes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With