1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-26 01:43:59 +02:00

Move age calculation to DateTimeHelpers

Also add unit test and make sure that leap years are handled correctly.
This commit is contained in:
Erik Johansson
2018-01-16 19:28:38 +01:00
parent 6da946ac0f
commit 8e917cc6c2
3 changed files with 53 additions and 12 deletions

View File

@@ -20,6 +20,8 @@ import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import com.health.openscale.core.utils.DateTimeHelpers;
import java.util.Calendar;
import java.util.Date;
@@ -124,8 +126,7 @@ public class ScaleUser {
this.goalDate = goalDate;
}
public boolean isMale()
{
public boolean isMale() {
if (gender == 0)
return true;
@@ -133,19 +134,17 @@ public class ScaleUser {
}
public int getAge(Date todayDate) {
Calendar cal_today = Calendar.getInstance();
cal_today.setTime(todayDate);
Calendar cal_birthday = Calendar.getInstance();
cal_birthday.setTime(birthday);
int userAge = cal_today.get(Calendar.YEAR) - cal_birthday.get(Calendar.YEAR);
if (cal_today.get(Calendar.DAY_OF_YEAR) < cal_birthday.get(Calendar.DAY_OF_YEAR)) userAge--;
Calendar calToday = Calendar.getInstance();
calToday.setTime(todayDate);
return userAge;
Calendar calBirthday = Calendar.getInstance();
calBirthday.setTime(birthday);
return DateTimeHelpers.yearsBetween(calBirthday, calToday);
}
public void setInitialWeight(float weight) {
this.initialWeight = weight;
}
public void setConvertedInitialWeight(float weight) {

View File

@@ -1,4 +1,4 @@
/* Copyright (C) 2017 Erik Johansson <erik@ejohansson.se>
/* Copyright (C) 2017-2018 Erik Johansson <erik@ejohansson.se>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -39,4 +39,17 @@ public final class DateTimeHelpers {
return days;
}
static public int yearsBetween(Calendar start, Calendar end) {
int years = end.get(Calendar.YEAR) - start.get(Calendar.YEAR);
final int startMonth = start.get(Calendar.MONTH);
final int endMonth = end.get(Calendar.MONTH);
if (endMonth < startMonth
|| (endMonth == startMonth
&& end.get(Calendar.DAY_OF_MONTH) < start.get(Calendar.DAY_OF_MONTH))) {
years -= 1;
}
return years;
}
}

View File

@@ -1,4 +1,4 @@
/* Copyright (C) 2017 Erik Johansson <erik@ejohansson.se>
/* Copyright (C) 2017-2018 Erik Johansson <erik@ejohansson.se>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -77,4 +77,33 @@ public class DateTimeHelpersTest {
getDate(2014, 12, 31),
getDate(2017, 1, 1)));
}
@Test
public void yearsBetween() throws Exception {
assertEquals(19,
DateTimeHelpers.yearsBetween(
getDate(1980, 3, 26),
getDate(2000, 3, 25)));
assertEquals(20,
DateTimeHelpers.yearsBetween(
getDate(1980, 3, 26),
getDate(2000, 3, 26)));
assertEquals(20,
DateTimeHelpers.yearsBetween(
getDate(1980, 3, 26),
getDate(2000, 3, 27)));
assertEquals(0,
DateTimeHelpers.yearsBetween(
getDate(2000, 3, 1),
getDate(2001, 2, 28)));
assertEquals(1,
DateTimeHelpers.yearsBetween(
getDate(2000, 3, 1),
getDate(2001, 3, 1)));
assertEquals(1,
DateTimeHelpers.yearsBetween(
getDate(2000, 3, 1),
getDate(2001, 3, 2)));
}
}