Sometimes you need to run your unit tests using DB2 TIMESTAMP values in your test data to match specific records. You can’t just use a Date() objekt because you have to specify nanoseconds.
As I couldn’t find any code snippet via Google, this might be helpful for you, dear reader.
Example: You want to check for a timestamp of ‘2006-02-15 12:05:19.861555’ (within quotes, as shown by one of my favourite SQL clients, SQuirrel SQL).
DateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); Date theMilliDate = formatter.parse( "2006-02-15 12:05:19" ); Timestamp timestamp = new Timestamp(theMilliDate.getTime()); // Note the trailing zeros! timestamp.setNanos(861555000); // validFrom would be of type java.util.Date myEntityObjekt.setValidFrom(timestamp);
This – at least – does the trick, if you know of a better solution, please share it 🙂