getHostAddress outputs the IP address, so if the IP address resolves to 127.0.0.1 OR InetAddress.getLocalHost().getHostAddress() then its the local host
For example:
// change 192.168.154.169 to ip of your machine to test
String[] hosts = { "192.168.154.169", "localhost", "google.com" };
for (String host : hosts) {
InetAddress address = InetAddress.getByName(host);
boolean isLocalhost = "127.0.0.1".equals(address.getHostAddress())
|| InetAddress.getLocalHost().getHostAddress().equals(address.getHostAddress());
System.out.println("Hostname: " + host + ", is local host: " + isLocalhost);
}
Output:
Hostname: 192.168.154.169, is local host: true
Hostname: localhost, is local host: true
Hostname: google.com, is local host: false