java 不推荐使用构造函数 Date(String)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30096749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The constructor Date(String) is deprecated
提问by Mukunth Rajendran
Well known deprecated issue causing me a problem. The following line "expiry = new Date(dt);" is the targeted script. To explain in detail I successfully used to
众所周知的弃用问题导致我出现问题。下面这行“expiry = new Date(dt);” 是目标脚本。详细解释我成功地习惯了
Date expiry = null;
String dt;
if(!(dt=str.nextToken()).equals("null"));
{
expiry = new Date(dt);
}
Using these lines in the scrips to read the cookies from the file. Yes, the "Date" is deprecated. I have read some solutions but still there are chain of errors while correcting it.
在脚本中使用这些行从文件中读取 cookie。是的,“日期”已被弃用。我已经阅读了一些解决方案,但在纠正它时仍然存在一系列错误。
What will be the correct one in the place of "date". Also I provide the full script below
什么是“日期”的正确位置。我还提供了下面的完整脚本
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");
}
}
回答by Priyesh
The javadoc for the deprecated method usually tells you what the method is replaced by. In this case, the javadoc for Date(String) at https://docs.oracle.com/javase/7/docs/api/java/util/Date.htmlmentions the following:
不推荐使用的方法的 javadoc 通常会告诉您该方法被替换的内容。在这种情况下,https: //docs.oracle.com/javase/7/docs/api/java/util/Date.html 中 Date(String) 的 javadoc提到了以下内容:
Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).
已弃用。从 JDK 1.1 版开始,由 DateFormat.parse(String s) 取代。
So, if you are using the default date format, you can replace your date construction code with the following;
因此,如果您使用默认日期格式,则可以使用以下内容替换您的日期构造代码;
expiry = java.text.DateFormat.getDateInstance().parse(dt);
If you have a custom date format, you will have to use the java.text.SimpleDateFormatclass instead of java.text.DateFormat.
如果您有自定义日期格式,则必须使用java.text.SimpleDateFormat类而不是java.text.DateFormat。
.
.
回答by Constantin
One way to deal with date strings is like so:
处理日期字符串的一种方法是这样的:
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);
回答by Mukunth Rajendran
I corrected using the following script. Thanks to all
我使用以下脚本进行了更正。谢谢大家
if(!(dt).equals("null"))
{
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd hh:mm:ss");
expiry = sdf.parse(dt);