8

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

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.

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



}
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Mukunth Rajendran
  • 143
  • 1
  • 2
  • 10
  • I think you have at least one bug in your code - the `;` at the end of `if(!(dt=str.nextToken()).equals("null"));` means that expiry is always assigned. If this is the intended behaviour, you can remove the comparison to `"null"` and the conditional to make it cleaner. – Andy Turner May 07 '15 at 10:11
  • 1
    Thanks. I corrected this as if(!(dt).equals("null")) { SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd hh:mm:ss"); expiry = sdf.parse(dt); – Mukunth Rajendran May 07 '15 at 12:34
  • When i run the script I leads to an another error that is org.openqa.selenium.InvalidCookieDomainException: invalid cookie domain: invalid domain:"/". please help me – Mukunth Rajendran May 07 '15 at 12:39
  • The 'dup' is not referring to the same constructor -- so those answers are not all that helpful for this question. – Brent Bradburn Mar 13 '18 at 13:51

3 Answers3

21

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.html mentions the following:

Deprecated. As of JDK version 1.1, replaced by 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.SimpleDateFormat class instead of java.text.DateFormat.

.

Priyesh
  • 2,041
  • 1
  • 17
  • 25
8

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);
Constantin
  • 1,506
  • 10
  • 16
  • You need to know the format of your raw Date String ofcourse. This will help you in that regard: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Constantin May 07 '15 at 09:36
  • When I run the script I leads to an another error that is org.openqa.selenium.InvalidCookieDomainException: invalid cookie domain: invalid domain:"/". – Mukunth Rajendran May 07 '15 at 12:40
  • Is this related to the same date issue? – Constantin May 07 '15 at 13:20
2

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);
Mukunth Rajendran
  • 143
  • 1
  • 2
  • 10