I just added a chapter on Date and Time to the “Java For Testers” book.
In the production environment in the main application, we very often use the Joda-Time library. But I’m trying to keep coverage of ‘libraries’ out of scope for this book, to make it easier for people to get started, and so that they build knowledge and experience with the inbuilt features.
Relying too much on external libraries often means adding another library into the code-base when all that is really required is a quick wrapper around existing core Java.
The chapter covers basic examples of:
- timing how long a set of code takes to execute
- creating unique ids and names for files
- formatting dates
- date arithmetic and manipulation
I frequently have to format dates in different ways, when I’m generating test data for application testing.
I time the how long code runs, when I’m writing simple performance tests. I often use nanoTime to do this.
I very often create unique file-names using the value returned by currentTimeMillis. In the book I provide examples of simple ways to convert the numeric file names into alphabetic characters. I sometimes generate unique usernames for test data using currentTimeMillis.
We also cover basic date time arithmetic in the chapter. A very useful thing to be able to do, when generating random data.
I think I’ve covered the basics of DateTime for the core Java classes well enough for people to start using it in their tests.
I’ve only ever had to drop down to Joda-Time once or twice in my career. I encourage you to experiment with the in-built Date Time functionality, before bringing in an external library. You might be surprised how much you can do.