Java - URLConnection getDate() Method with Examples
Description
The Java URLConnection getDate() method returns the value of the date header field.
Declaration
Following is the declaration for java.net.URLConnection.getDate() method
public long getDate()
Parameters
NA
Return Value
the sending date of the resource that the URL references, or 0 if not known. The value returned is the number of milliseconds since January 1, 1970 GMT.
Exception
NA
Example 1
The following example shows the usage of Java URLConnection getDate() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getDate(), we're getting the value of date header field of URLConnection instance and printing the same −
package com.tutorialspoint;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.time.Instant;
import java.util.Date;
public class UrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com");
URLConnection urlConnection = url.openConnection();
System.out.println(Date.from(Instant.ofEpochMilli(urlConnection.getDate())));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
Tue Dec 05 12:31:57 IST 2023
Example 2
The following example shows the usage of Java URLConnection getDate() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getDate(), we're getting the value of date header field of URLConnection instance and printing the same −
package com.tutorialspoint;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.time.Instant;
import java.util.Date;
public class UrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("http://www.tutorialspoint.com");
URLConnection urlConnection = url.openConnection();
System.out.println(Date.from(Instant.ofEpochMilli(urlConnection.getDate())));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
Tue Dec 05 12:32:42 IST 2023
Example 3
The following example shows the usage of Java URLConnection getDate() method for a url of google with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getDate(), we're getting the value of date header field of URLConnection instance and printing the same −
package com.tutorialspoint;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.time.Instant;
import java.util.Date;
public class UrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("http://www.google.com");
URLConnection urlConnection = url.openConnection();
System.out.println(Date.from(Instant.ofEpochMilli(urlConnection.getDate())));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
Tue Dec 05 12:32:59 IST 2023
java_urlconnection.htm