qala-io/datagen

Consider adding separate methods for generating day/month/year

Closed this issue · 3 comments

While it's possible to write between(1, 12) and between(1, 31), it's not so readable as month() or year(). I'd suggest to add such methods.

WDYT?

Depending on the purpose/API that you use month can actually be between(0, 11) and days could be between(0, 30).

Then if you generate month=2 and day=31 this is going to be invalid date so these numbers must depend on each other and therefore be generated simultaneously. Same is true for the year because of the leap years.

I think the safest way to work with date-related values is to generate a random date and then take days/months/years from that date:

import static io.qala.datagen.Java8RandomShortApi.*;

LocalDateTime date = beforeNow().localDateTime();
int day = date.getDayOfMonth();
int month = date.getMonthValue();

If you have month and day then you can generate a full object and gets the values from it. But if I have a form with a date only a year and I'm writing a unit test for DAO/service, then I need to generate only year. In such a case, a separated method would be useful, but it's up to you of course.

If it's a year - it's just an integer. And there is no sensible range that can be used widely. Some apps could potentially accept any date, others would accept only AE, but most of the apps would probably accept something like 1920-now years (e.g. for the year of birth or company foundation). So if it's an app-dependent, then it's better to write some internal utility for randomization e.g.

public static int randomYear() { return between(1920, now().getYear(); }

But again - such function is specific to your application and domain. So I wouldn't try to add it into datagen.