Friday, January 30, 2026
HomeLanguagesJavaGetting the Date of URL connection in Java

Getting the Date of URL connection in Java

HttpURLConnection class is the class in java which deals with all the operations related to URL connection. getDate() method of HttpURLConnection class is the method that is used to get the date and time of the URL Connection.

Syntax:

Obj.getDate()  // Obj is object of HttpUrlConnection class

Parameter: This method does not take any parameter. It is just used along with a HttpUrlConnection Object from which we want to fetch the date and time of the connection.

Return values: It returns the day, date and time of connection.

Below is the code implementation to get the date of the URL connection.

Java




// Java Program to get the date of the URL connection
  
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.concurrent.TimeUnit;
  
class Main {
    public static void main(String args[])
        throws IOException, InterruptedException
    {
        // getting the URL class object
        URL url = new URL("http://www.yahoo.com");
  
        // opening the connection
        HttpURLConnection httpCon
            = (HttpURLConnection)url.openConnection();
  
        // getting the date of URL connection
        long date = httpCon.getDate();
  
        /*
          Other working of program
        */
  
        // if date is 0,it means there is no 
        // information regarding date
        if (date == 0)
            System.out.println("No date information.");
        else {
            // print the date using object of Date class
            System.out.println("Date: " + new Date(date));
        }
    }
}


Output:- 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12065 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS