如何将日期从日期对象转换为字符串对象

时间:2013-10-09 16:10:13

标签: java junit

此测试失败。我在当前日期传递并使用我编写的方法将日期转换为对象。当我返回它,因为它是一个对象。当我写断言时,它失败了。我在日期调用tostring方法,因为返回对象时它就像一个字符串,因为字符串是一个对象。

@Test
   public void testOrder5() throws Exception {
      Phone input = new DefaultPhone();
      Date date = new Date();
      input.setCreatedDate(date); // passing in date object
      PhoneObj output = Book.change(input); //pases input to change method to convert date to phone object
      assertThat(output.getCreatedDate(), is(date.toString()));
   }

这是我失败的错误

java.lang.AssertionError: 
Expected: is "Wed Oct 09 12:03:17 EDT 2013"
     got: "2013-10-09T12:03:17-04:00"

at org.junit.Assert.assertThat(Assert.java:780)
at org.junit.Assert.assertThat(Assert.java:738)
at ca.on.oicr.DtosTest.testOrder5(DtosTest.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

在我的转换更改方法中,我有

public static PhoneObj change(Order from) {
     PhoneObj phoneobj = new OrderDto();
    if (from.getCreatedDate() != null) {
             phoneobj.setCreatedDate(dateTimeFormatter.print(from.getCreatedDate().getTime()));
          }
return phoneobj;
}

我尝试了以下建议但是没有用

   @Test
   public void testOrder5() throws Exception {
      Phone input = new Defaultphone();
      SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
      Date date = new Date();
      String str = date.toString();
      input.setCreatedDate(date);
      Date date1 = sf.parse(str);
      PhoneObj output = Book.change(input);
      assertThat(output.getCreatedDate(), is((date1.toString())));
   }

说日期不可解析

2 个答案:

答案 0 :(得分:1)

您有未显示的代码,但我建议您使用相同的格式比较Date对象或使用String生成的SimpleDateFormat对象。

您的问题是,您要比较从同一String获得但格式不同的两个Date

答案 1 :(得分:1)

使用SimpleDateFormat以相同格式比较两个Date。在下面的测试中,我正在比较格式化程序生成的字符串。

@Test
   public void testOrder5() throws Exception {
      SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
      Phone input = new DefaultPhone();
      Date date = new Date();
      input.setCreatedDate(date); // passing in date object
      PhoneObj output = Book.change(input); //pases input to change method to convert date to phone object
      assertThat(sf.format((Date)(output.getCreatedDate())), sf.format(date));
   }