2014-12-03 44 views
-1

我有這個代碼的遠程日期。任何人都可以幫助我理解如何讓它在Windows XP中運行,或向我展示代碼以實現在Windows XP中運行的相同目的?遠程日期的其他服務器的Java代碼不運行在Windows XP中(它運行在Windows 8中)

非常感謝。

try { 
    Date remoteDate = null; 
    URL url = new URL("https://www.google.gr/?gws_rd=ssl"); 
    URLConnection urlConn = url.openConnection(); 
    HttpURLConnection conn1 = (HttpURLConnection) urlConn; 
    conn1.setConnectTimeout(10000); 
    conn1.setReadTimeout(10000); 
    conn1.setInstanceFollowRedirects(true); 
    conn1.setRequestProperty("User-agent", "spider"); 
    conn1.connect(); 
    Map<String,List<String>> header = conn1.getHeaderFields(); 
    for (String key : header.keySet()) { 
     if (key != null && "Date".equals(key)) { 
      List<String> data = header.get(key); 
      String dateString = data.get(0); 
      SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss"); 
      try { 
       remoteDate = sdf.parse(dateString); 
       System.out.print("hi"+remoteDate); 
      } catch (ParseException ex) { 
       Logger.getLogger(JFrame0.class.getName()).log(Level.SEVERE, null, ex); 
      } 
      break; 
     } 
    } 
} catch (IOException ex) { 
    Logger.getLogger(JFrame0.class.getName()).log(Level.SEVERE, null, ex); 
} 

我剛剛安裝Netbeans和錯誤是:

SEVERE: null 
java.text.ParseException: Unparseable date: "Wed, 03 Dec 2014 10:18:09 GMT" 
     at java.text.DateFormat.parseenter code here(DateFormat.java:357) 
     at ast_v1.JFrame0.<init>(JFrame0.java:215) 
     at ast_v1.JFrame0$18.run(JFrame0.java:1596) 
     at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312) 
     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733) 
     at java.awt.EventQueue.access$200(EventQueue.java:103) 
     at java.awt.EventQueue$3.run(EventQueue.java:694) 
     at java.awt.EventQueue$3.run(EventQueue.java:692) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:703) 
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 
     at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
     at java.util.Calendar.setTime(Calendar.java:1106) 
     at java.text.SimpleDateFormat.format(SimpleDateFormat.java:955) 
     at java.text.SimpleDateFormat.format(SimpleDateFormat.java:948) 
     at java.text.DateFormat.format(DateFormat.java:336) 
     at ast_v1.JFrame0.<init>(JFrame0.java:224) 
     at ast_v1.JFrame0$18.run(JFrame0.java:1596) 
     at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312) 
     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733) 
     at java.awt.EventQueue.access$200(EventQueue.java:103) 
     at java.awt.EventQueue$3.run(EventQueue.java:694) 
     at java.awt.EventQueue$3.run(EventQueue.java:692) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:703) 
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 
     at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 
+0

你是說它適用於其他操作系統?如果是的話哪一個? – weston 2014-12-03 09:07:52

+0

我已經在Windows 8中檢查了它,例如,它工作。但它不能在XP中工作。 – nmantas84 2014-12-03 09:10:26

+0

你會得到哪個錯誤?爲什麼它不起作用? – XikiryoX 2014-12-03 09:13:49

回答

0

我看到你的源代碼中有兩處possbile問題,既涉及到格式定義:

  1. 你不要指定區域設置。您正在檢索英文本​​地化日期Wed。在例如運行程序時德國PC格式化程序將不會檢測到Wed爲一週中的某一天。
  2. 您不要在格式化程序模式中指定時區。如果收到的時間標記的時區與PC不同,那麼這是相關的。

所以不是

SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss"); 

使用

SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH); 
+0

這是否回答你的問題?在這種情況下,請將您的問題標記爲已解決。謝謝! – Uli 2015-02-04 08:40:59

相關問題