You can use the getPath() method of the URL object:
URL url = new URL("https://asd.com/somestuff/another.html"); System.out.println(url.getPath());
Now this leads only to the actual path. If you need additional information (anchor or parameters passed as get values), you need to call other accessors of the URL object:
URL url = new URL("https://asd.com/somestuff/another.html?param=value#anchor"); System.out.println(url.getPath()); // prints "/somestuff/another.html" System.out.println(url.getQuery()); // prints "param=value" System.out.println(url.getRef()); // prints "anchor"
Possible use for generating relative URLs without a lot of code based on Hiru's answer :
URL absolute = new URL(url, "/"); String relative = url.toString().substring(absolute.toString().length()); System.out.println(relative);
source share