title: testing current java timestamp in junit
body: <p>I'm trying to write a Junit for testing an object that has the current timestamp. the problem is when I first create the current timestamp as the following:</p>
  
  <pre><code>Date date = new Date();
  Timestamp timestamp = new Timestamp(date.getTime());
  </code></pre>
  
  <p>It creates a timestamp object with current time including the milliseconds. so when I run my tests they're never equal because there is some time elapsed in milliseconds between the time I created the timestamp object and the time of testing:</p>
  
  <pre><code>x.setTimeToMyMethod(timestamp);
  assertEquals(timestamp, x.getTimeFromMyMethod());
  </code></pre>
  
  <p>I tried to use SimpleDateFormat but it converts the time to a string and my setter method only accept Timestamp objects.
  Is there anyway that I can overcome this problem?
  Thanks in advance!</p>
  
  <p>Thanks for the answers and here is my code and hopefully it will clarify what I'm trying to do:</p>
  
  <pre><code>public class myTest {
  
      Date date = new Date();
  
      Timestamp timestamp = new Timestamp(date.getTime());
  
      myTestingClass testClass = new myTestingClass();
  
      @BeforeClass
      public static void setUpBeforeClass() throws Exception {
      }
  
      @AfterClass
      public static void tearDownAfterClass() throws Exception {
      }
  
      @Before
      public void setUp() throws Exception {
  
  
          testClass.setCreateDate(timestamp); 
  
      }
  
      @After
      public void tearDown() throws Exception {
      }
  
      @Test
      public void test() {
          try {
  
  
              assertEquals(timestamp, testClass.getCreateDate());
  
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
  
      }
  </code></pre>
  
  <p>when I try to run it it gives me the following error:</p>
  
  <blockquote>
    <p>java.lang.AssertionError: expected:&lt;2015-02-11 09:09:40.038> but
    was:&lt;2015-02-11 09:09:40.04></p>
  </blockquote>