Java Date.parse IllegalArgument

时间:2012-08-23 01:18:33

标签: java date

我正在为调用Date.parse()的方法编写测试。代码看起来像这样

    public void someMethod(String s){

         Date date = new Date();

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

         //other methods
    }

在我的测试中,我做了类似的事情

    public void test(){
          ...

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

          //assert statements
    }

它在我的机器上运行良好,我在我的日食和IntelliJ IDEA中尝试过。但是,当我将它推送到我们的服务器进行构建时,测试失败。它抛出IllegalArgumentException。我的队友也收到同样的例外。我们使用相同的sdk:jdk1.7.0和IDE eclipse和IntelliJ。唯一的区别是我的机器上安装了jre7。我也尝试了一个简单的测试:

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

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

          sysout(e);
    } 

在我的机器上运行良好,但在他的机器中收到IllegalArgumentException。我无法更改测试中的方法,因此我无法执行从字符串到日期的其他转换。感谢

编辑 - 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)

抱歉我无法正确编辑,我的机器中禁用了javascript

4 个答案:

答案 0 :(得分:2)

Date.toString()Date.parse()与语言环境有关。因此,toString()可能无法明确地读取parse()的输出。传递日期的时间戳会更好,因为这只是一个数字,因此是唯一的。

答案 1 :(得分:1)

根据Javadocs Date.parse()自JDK 1.1起已被弃用

因此,您最好更改以下代码:

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

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

有这样的事情:

Date d = new Date();

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

答案 2 :(得分:1)

虽然这可能无法解决您的问题,但我认为查看DateFormatter课程对您的案例非常有帮助。

答案 3 :(得分:0)

Date.parse()已弃用。 更改方法如下,然后再试一次。

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

     e.printStackTrace();
     }


     //other methods
}