import java.net.URL; public class Main { public static void main(String[] args) throws Exception { URL url = new URL("https://www.example.com/index.html"); String hostname = url.getHost(); System.out.println("Hostname: " + hostname); } }
import java.net.URL; public class Main { public static void main(String[] args) throws Exception { String urlString = "https://www.example.com/index.html"; URL url = new URL(urlString); String hostname = url.getHost(); System.out.println("Hostname: " + hostname); } }Both examples use the java.net package library.