Skip to content

Commit

Permalink
Merge pull request #460 from BlankSergey/fix-birthday
Browse files Browse the repository at this point in the history
Fix birthday(): add currentMonth and currentDay
  • Loading branch information
codingricky authored Feb 10, 2020
2 parents 28d1ddf + c5fbba8 commit 9170081
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
10 changes: 8 additions & 2 deletions src/main/java/com/github/javafaker/DateAndTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public Date between(Date from, Date to) throws IllegalArgumentException {
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);
}
Expand All @@ -170,8 +174,10 @@ public Date birthday() {
*/
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());
}
Expand Down
18 changes: 11 additions & 7 deletions src/test/java/com/github/javafaker/DateAndTimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ public void testBetween() {
@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));
Expand All @@ -97,16 +99,18 @@ public void testBirthday() {
@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));
}
}
Expand Down

0 comments on commit 9170081

Please sign in to comment.