With the following code:
try {
System.out.println(new Date());
InetAddress hostName = InetAddress.getLocalHost();
System.out.println(new Date());
} cat
I suspect the delay here was due to a failed attempt at DNS resolution. Perhaps your DNS servers were not configured correctly. The 30 seconds probably represents the timeout on the DNS resolution.
The reason your solution improved the speed is that adding the entry to the hosts file allowed the hostname to be resolved locally and thus skip the attempt to resolve the hostname against an actual (remote) DNS server.
EDIT: You may wonder why this method does any host resolution at all. Apparently, it is part of an anti-spoofing mechanism built in to the Java networking library. See the accepted answer of this post for more details: InetAddress.getCanonicalHostName() returns IP instead of Hostname