构造函数Date(String)已被弃用。

8

我遇到了一个众所周知的已弃用的问题,这给我带来了麻烦。下面这行代码"expiry = new Date(dt);"就是目标脚本。详细解释一下,我曾经成功地使用过它。

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

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

在“date”的位置上应该使用哪个正确的代码?下面是完整的脚本:

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");



}
}

我认为你的代码中至少有一个bug - if(!(dt=str.nextToken()).equals("null")); 末尾的 ; 意味着 expiry 总是被赋值。如果这是预期行为,你可以删除与 "null" 的比较和条件语句,使其更加简洁。 - Andy Turner
1
谢谢。我已经更正了这个问题,代码如下:if(!(dt).equals("null")) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss"); expiry = sdf.parse(dt); } - Mukunth Rajendran
当我运行脚本时,出现了另一个错误,即org.openqa.selenium.InvalidCookieDomainException: invalid cookie domain: invalid domain:"/"。请帮助我。 - Mukunth Rajendran
“dup” 不是指相同的构造函数,因此这些答案对于这个问题并没有太大帮助。 - Brent Bradburn
3个回答

21

被废弃的方法的javadoc通常会告诉你该方法被替换成什么。在这种情况下,Date(String)的javadoc位于https://docs.oracle.com/javase/7/docs/api/java/util/Date.html,其中提到:

Deprecated. 随着JDK版本1.1的推出,已由DateFormat.parse(String s)替换。

因此,如果您使用默认日期格式,则可以使用以下代码替换日期构造代码:

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

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

.


这应该是正确的答案。解释得很好... - Constantin

9

处理日期字符串的一种方法是这样的:

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

你需要知道你的原始日期字符串的格式。这将帮助你了解更多相关信息:http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html - Constantin
当我运行脚本时,会导致另一个错误,即org.openqa.selenium.InvalidCookieDomainException: invalid cookie domain: invalid domain:"/"。 - Mukunth Rajendran
这与相同的日期问题有关吗? - Constantin

2
我使用以下脚本进行了更正。感谢大家。
if(!(dt).equals("null"))
    {           

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接