Merge pull request #460 from BlankSergey/fix-birthday
Fix birthday(): add currentMonth and currentDay
This commit is contained in:
@@ -144,6 +144,10 @@ public class DateAndTime {
|
||||
throw new IllegalArgumentException("Invalid date range, the upper bound date is before the lower bound.");
|
||||
}
|
||||
|
||||
if (from.equals(to)) {
|
||||
return from;
|
||||
}
|
||||
|
||||
long offsetMillis = faker.random().nextLong(to.getTime() - from.getTime());
|
||||
return new Date(from.getTime() + offsetMillis);
|
||||
}
|
||||
@@ -170,8 +174,10 @@ public class DateAndTime {
|
||||
*/
|
||||
public Date birthday(int minAge, int maxAge) {
|
||||
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
|
||||
Calendar from = new GregorianCalendar(currentYear - maxAge, 0, 1);
|
||||
Calendar to = new GregorianCalendar(currentYear - minAge, 11, 31);
|
||||
int currentMonth = Calendar.getInstance().get(Calendar.MONTH);
|
||||
int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
|
||||
Calendar from = new GregorianCalendar(currentYear - maxAge, currentMonth, currentDay);
|
||||
Calendar to = new GregorianCalendar(currentYear - minAge, currentMonth, currentDay);
|
||||
|
||||
return between(from.getTime(), to.getTime());
|
||||
}
|
||||
|
||||
@@ -84,10 +84,12 @@ public class DateAndTimeTest extends AbstractFakerTest {
|
||||
@Test
|
||||
public void testBirthday() {
|
||||
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
|
||||
long from = new GregorianCalendar(currentYear - 65, 0, 1).getTime().getTime();
|
||||
long to = new GregorianCalendar(currentYear - 18, 11, 31).getTime().getTime();
|
||||
int currentMonth = Calendar.getInstance().get(Calendar.MONTH);
|
||||
int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
|
||||
long from = new GregorianCalendar(currentYear - 65, currentMonth, currentDay).getTime().getTime();
|
||||
long to = new GregorianCalendar(currentYear - 18, currentMonth, currentDay).getTime().getTime();
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
for (int i = 0; i < 5000; i++) {
|
||||
Date birthday = faker.date().birthday();
|
||||
assertThat("birthday is after upper bound", birthday.getTime(), lessThan(to));
|
||||
assertThat("birthday is before lower bound", birthday.getTime(), greaterThanOrEqualTo(from));
|
||||
@@ -97,16 +99,18 @@ public class DateAndTimeTest extends AbstractFakerTest {
|
||||
@Test
|
||||
public void testBirthdayWithAges() {
|
||||
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
|
||||
int currentMonth = Calendar.getInstance().get(Calendar.MONTH);
|
||||
int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
for (int i = 0; i < 5000; i++) {
|
||||
int minAge = faker.number().numberBetween(1, 99);
|
||||
int maxAge = faker.number().numberBetween(minAge, 100);
|
||||
|
||||
long from = new GregorianCalendar(currentYear - maxAge, 0, 1).getTime().getTime();
|
||||
long to = new GregorianCalendar(currentYear - minAge, 11, 31).getTime().getTime();
|
||||
long from = new GregorianCalendar(currentYear - maxAge, currentMonth, currentDay).getTime().getTime();
|
||||
long to = new GregorianCalendar(currentYear - minAge, currentMonth, currentDay).getTime().getTime();
|
||||
|
||||
Date birthday = faker.date().birthday(minAge, maxAge);
|
||||
assertThat("birthday is after upper bound", birthday.getTime(), lessThan(to));
|
||||
assertThat("birthday is after upper bound", birthday.getTime(), lessThanOrEqualTo(to));
|
||||
assertThat("birthday is before lower bound", birthday.getTime(), greaterThanOrEqualTo(from));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user