Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤和異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 其他

Java API 和框架

Java 類引用

Java 有用資源

Java - URLConnection 的 getDate() 方法及示例



描述

Java URLConnection 的 getDate() 方法返回日期頭欄位的值。

宣告

以下是 java.net.URLConnection.getDate() 方法的宣告

public long getDate()

引數

返回值

URL 引用的資源的傳送日期,如果未知則為 0。返回的值是從格林威治標準時間 1970 年 1 月 1 日以來的毫秒數。

異常

示例 1

以下示例演示了針對具有 https 協議的有效 url 使用 Java URLConnection getDate() 方法。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲取 URLConnection 例項。使用 getDate(),我們獲取 URLConnection 例項的日期頭欄位的值並列印它。

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://tutorialspoint.tw");
         URLConnection urlConnection = url.openConnection();

         System.out.println(Date.from(Instant.ofEpochMilli(urlConnection.getDate())));

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

輸出

Tue Dec 05 12:31:57 IST 2023

示例 2

以下示例演示了針對具有 http 協議的有效 url 使用 Java URLConnection getDate() 方法。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲取 URLConnection 例項。使用 getDate(),我們獲取 URLConnection 例項的日期頭欄位的值並列印它。

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://tutorialspoint.tw");
         URLConnection urlConnection = url.openConnection();

         System.out.println(Date.from(Instant.ofEpochMilli(urlConnection.getDate())));

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

輸出

Tue Dec 05 12:32:42 IST 2023

示例 3

以下示例演示了針對具有 http 協議的 Google url 使用 Java URLConnection getDate() 方法。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲取 URLConnection 例項。使用 getDate(),我們獲取 URLConnection 例項的日期頭欄位的值並列印它。

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();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

輸出

Tue Dec 05 12:32:59 IST 2023
java_urlconnection.htm
廣告
© . All rights reserved.