Java - URLConnection getHeaderFieldDate(String name, long Default)
Description
The Java URLConnection getHeaderFieldDate(String name, long Default) method returns the value of the named field parsed as date. The result is the number of milliseconds since January 1, 1970 GMT represented by the named field.
Declaration
Following is the declaration for java.net.URLConnection.getHeaderFieldDate(String name, long Default) method
public long getHeaderFieldDate(String name, long Default)
Parameters
name − the name of a header field.
Default − a default value.
Return Value
the value of the field, parsed as a date. The value of the Default argument is returned if the field is missing or malformed.
Exception
NA
Example 1
The following example shows the usage of Java URLConnection getHeaderFieldDate(String name, long Default) 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 getHeaderFieldDate(name), we're getting the value of an header field identified by name Expires 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();
long headerValue = urlConnection.getHeaderFieldDate("Expires", 1);
System.out.println("Expires: " + Date.from(Instant.ofEpochMilli(headerValue)));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
Expires: Fri Jan 05 11:03:55 IST 2024
Example 2
The following example shows the usage of Java URLConnection getHeaderFieldDate(String name, long Default) 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 getHeaderFieldDate(name), we're getting the value of an header field identified by name Date 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();
long headerValue = urlConnection.getHeaderFieldDate("Date", 1);
System.out.println("Date: " + Date.from(Instant.ofEpochMilli(headerValue)));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
Date: Wed Dec 06 11:04:19 IST 2023
Example 3
The following example shows the usage of Java URLConnection getHeaderFieldDate(String name, long Default) 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 getHeaderFieldDate(name), we're getting the value of an header field identified by name Expires 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();
long headerValue = urlConnection.getHeaderFieldDate("Expires", 1);
System.out.println("Expires: " + Date.from(Instant.ofEpochMilli(headerValue)));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
Expires: Thu Jan 01 05:30:00 IST 1970
java_urlconnection.htm