【问题标题】:The constructor Date(String) is deprecated [duplicate]构造函数 Date(String) 已弃用 [重复]
【发布时间】:2015-07-17 18:30:28
【问题描述】:

众所周知的已弃用问题导致我出现问题。以下行“到期=新日期(dt);”是目标脚本。详细解释一下我成功习惯了

Date expiry = null;
String dt;
if(!(dt=str.nextToken()).equals("null"));
{
  expiry = new Date(dt);
}

使用脚本中的这些行从文件中读取 cookie。是的,“日期”已被弃用。我已经阅读了一些解决方案,但在纠正它时仍然存在一系列错误。

什么是正确的代替“日期”。我还提供了下面的完整脚本

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Date;
import java.util.StringTokenizer;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Reader {

public static void main(String[] args) {
System.setProperty ("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.gmail.com");

try{
    File f = new File("browser.data");
    FileReader fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);
String line;

while ((line = br.readLine())!=null){
StringTokenizer str = new StringTokenizer (line, ";");

while (str.hasMoreTokens()) {


    String name = str.nextToken();
    String value = str.nextToken();
    String domain = str.nextToken();
    String path = str.nextToken();

    Date expiry = null;
    String dt;

    if(!(dt=str.nextToken()).equals("null"));
    {
        expiry = new Date(dt);
    }

    boolean isSecure = new Boolean(str.nextToken()).booleanValue();

    Cookie ck = new Cookie (name,value,domain,path,expiry,isSecure);

    driver.manage().addCookie(ck);
    br.close();
}

}
}

catch (Exception ex)
{
    ex.printStackTrace();
}

driver.get("http://gmail.com");



}
}

【问题讨论】:

  • 我认为您的代码中至少有一个错误 - ; 末尾的 if(!(dt=str.nextToken()).equals("null")); 表示总是分配到期。如果这是预期的行为,您可以删除与"null" 的比较以及使其更清晰的条件。
  • 谢谢。我将其更正为 if(!(dt).equals("null")) { SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd hh:mm:ss");到期 = sdf.parse(dt);
  • 当我运行脚本时,我导致另一个错误是 org.openqa.selenium.InvalidCookieDomainException: invalid cookie domain: invalid domain:"/"。请帮帮我
  • “dup”指的不是同一个构造函数——所以这些答案对这个问题并没有太大帮助。

标签: java cookies selenium-webdriver webdriver deprecated


【解决方案1】:

不推荐使用的方法的 javadoc 通常会告诉您该方法被替换为什么。在这种情况下,https://docs.oracle.com/javase/7/docs/api/java/util/Date.html 的 Date(String) 的 javadoc 提到了以下内容:

已弃用。从 JDK 1.1 版开始,替换为 DateFormat.parse(String s)。

因此,如果您使用的是默认日期格式,则可以将日期构造代码替换为以下内容;

expiry = java.text.DateFormat.getDateInstance().parse(dt);

如果您有自定义日期格式,则必须使用 java.text.SimpleDateFormat 类而不是 java.text.DateFormat

.

【讨论】:

  • 这应该是正确的答案。很好的解释...
【解决方案2】:

我使用以下脚本进行了更正。谢谢大家

if(!(dt).equals("null"))
    {           

        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd hh:mm:ss"); 
        expiry = sdf.parse(dt);

【讨论】:

    【解决方案3】:

    处理日期字符串的一种方法如下:

    String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
    
    String yourDateString = "2015-01-01 12:00:00";
    DateFormat formatter = new SimpleDateFormat(DEFAULT_PATTERN);
    Date myDate = formatter.parse(yourDateString);
    

    【讨论】:

    • 您当然需要知道原始日期字符串的格式。这将在这方面为您提供帮助:docs.oracle.com/javase/7/docs/api/java/text/…
    • 当我运行脚本时,我导致另一个错误是 org.openqa.selenium.InvalidCookieDomainException: invalid cookie domain: invalid domain:"/"。
    • 这是否与同日问题有关?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 2013-10-28
    • 2021-09-10
    • 2017-12-04
    相关资源
    最近更新 更多