gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:39:39 26 4
gpt4 key购买 nike

此测试失败。我传入当前日期并使用我编写的方法将日期转换为对象。当我返回它时,因为它是一个对象。当我写下我的断言时,它失败了。我在日期上调用 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())));
}

说日期不可解析

最佳答案

使用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));
}

关于java - 如何将日期从日期对象转换为字符串对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19277218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com