title: Hostname of URL using URL class
body: <p>I used the URL class in Java to retrieve the hostname portion of a URL as follows.</p>
  
  <p>IE, if the URL is <a href="https://hostname.mycompany.com" rel="nofollow">https://hostname.mycompany.com</a>, I want the output to just be 'hostname'</p>
  
  <pre><code>URL host = new URL(url);
  String hostname = site.getHost().split("\\.")[0];
  </code></pre>
  
  <p>Is there an easier way to do this? Thanks.</p>
  
  <p>The getHost function returns 'hostname.company.com' instead of 'hostname'</p>