title: Get domain without subdomains in Java?
body: <p>The below code prints <code>www.sub.google.com</code>.</p>
  
  <pre><code>import java.net.MalformedURLException;
  import java.net.URL;
  public class GetDomainNameFromURL {
      public static void main(String[] args) throws MalformedURLException {
          String s = "http://www.sub.google.com/main?&amp;t=20&amp;f=52";
          URL u = new URL(s);
          String hostName = u.getHost();
          System.out.println(hostName);
      }
  }
  </code></pre>
  
  <p>How to print <code>google.com</code>? Need to use plain <code>Java</code>, no <code>Guava</code> libraries.</p>