Frank Smith
Frank Smith

Reputation: 1743

Java Date.parse IllegalArgument

I'm writing a test for a method that invokes Date.parse(). The code looks like this

    public void someMethod(String s){

         Date date = new Date();

         date.setTime(Date.parse(s));

         //other methods
    }

in my test I did something like this

    public void test(){
          ...

          Date date = new Date();
          someMethod(date.toString());

          //assert statements
    }

It works well on my machine, I tried it in my eclipse and IntelliJ IDEA. However, when I push it to our server for build, the test fails. It throws IllegalArgumentException. My teammate receives the same exception also. We have are using the same sdk: jdk1.7.0 and IDE eclipse and IntelliJ. The only difference is I have a jre7 installed in my machine. I also tried a simple test:

    public void test(){
          Date d = new Date();
          String s = d.toString();

          Date e = new Date();
          e.setTime(Date.parse(s));

          sysout(e);
    } 

Works well in my machine, but receives IllegalArgumentException in his machine. I cannot change the method under test so I cannot perform other conversion from string to date. Thanks

EDIT - StackTrace

    java.lang.IllegalArgumentException
        at java.util.Date.parse(Date.java:598)
        at package.Class.Method(Class.java:135)
        at package.TestClass.TestMethod(TestMethod.java:192)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:312)
        at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
        at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:296)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:112)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:73)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:284)
        at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
        at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:209)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
        at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
        at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
        at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
        at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:42)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
        at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
        at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

Sorry I cannot edit it properly, the javascript is disabled in my machine

Upvotes: 0

Views: 2463

Answers (4)

JZH
JZH

Reputation: 47

Date.parse() is deprecated yet . Change the method as below and try it again .

public void someMethod(String s){
     Date date = new Date();
     try {
      date=DateFormat.getDateInstance().parse(s);
     } catch (ParseException e) {

     e.printStackTrace();
     }


     //other methods
}

Upvotes: 0

Although this may not resolve your problem, i think having a look at the DateFormatter class would be very helpful for your case.

Upvotes: 1

Sujay
Sujay

Reputation: 6783

As per the Javadocs Date.parse() was deprecated as of JDK 1.1

So you would be better of changing the following code:

Date d = new Date();
String s = d.toString();

Date e = new Date();
e.setTime(Date.parse(s));

with something like this:

Date d = new Date();

Date e = new Date(); 
e.setTime(d.getTime());

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

Date.toString() and Date.parse() are locale-dependent. As a result, it is possible for toString()'s output to not be unambiguously readable by parse(). It would be better to pass the date's timestamp, since that is just a number and is therefore unique.

Upvotes: 2

Related Questions