code: public static String getLocalHostName() throws SocketException, UnknownHostException {
          try {
              final String canonicalHostName = InetAddress.getLocalHost().getCanonicalHostName();
              if (isFQDN(canonicalHostName)) {
                  return canonicalHostName;
              } else {
                  return InetAddress.getLocalHost().getHostName();
              }
          } catch (UnknownHostException e) {
              NetworkInterface networkInterface = NetworkInterface.getNetworkInterfaces().nextElement();
              if (networkInterface == null)
                  throw e;
              InetAddress ipAddress = networkInterface.getInetAddresses().nextElement();
              if (ipAddress == null)
                  throw e;
              return ipAddress.getHostAddress();
          }
      }
docs: Retrieves the local host's hostname. If found, the fully qualified domain name (FQDN) will be returned,
  otherwise it will fallback to the unqualified domain name. E.g prefer guerrero.moocar.me over guerrero.