Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中创建年龄计算方法_Java_Android - Fatal编程技术网

Java 如何在android中创建年龄计算方法

Java 如何在android中创建年龄计算方法,java,android,Java,Android,我想写一个从出生日期开始计算年龄的方法,逻辑是否正确,以及如何在android Java中编写: public int calculateAge(String birthday){ // consider that birthday format is ddmmyyyy; String today = SystemDate(ddmmyyyy); int bDay = birthday(1,2).toInteger; int bMonth = birthday(3,4).toInteg

我想写一个从出生日期开始计算年龄的方法,逻辑是否正确,以及如何在android Java中编写:

public int calculateAge(String birthday){ 
 // consider that birthday format is ddmmyyyy;

 String today = SystemDate(ddmmyyyy);
 int bDay = birthday(1,2).toInteger;
 int bMonth = birthday(3,4).toInteger;
 int bYear = birhtday(5,8).toInteger;

 int tDay = today(1,2).toInteger;
 int tMonth = today(3,4).toInteger;
 int tYear = today(5,8).toInteger;

 if (tMonth == bMonth){
     if (tday>= bDay){ 
        age = tYear - bYear;
      else 
        age = tYear - bYear - 1;}
 else
   if (tMonth > bMonth) {
       age = tYear - bYear;
   else 
       age = tYear - bYear - 1;}
 }
 return age;
}

下面是一个android年龄计算示例,您应该能够从中进行计算:

    public int getAge (int _year, int _month, int _day) {

            GregorianCalendar cal = new GregorianCalendar();
            int y, m, d, a;         

            y = cal.get(Calendar.YEAR);
            m = cal.get(Calendar.MONTH);
            d = cal.get(Calendar.DAY_OF_MONTH);
            cal.set(_year, _month, _day);
            a = y - cal.get(Calendar.YEAR);
            if ((m < cal.get(Calendar.MONTH))
                            || ((m == cal.get(Calendar.MONTH)) && (d < cal
                                            .get(Calendar.DAY_OF_MONTH)))) {
                    --a;
            }
            if(a < 0)
                    throw new IllegalArgumentException("Age < 0");
            return a;
    }
public int getAge(int年、int月、int日){
GregorianCalendar cal=新的GregorianCalendar();
int y,m,d,a;
y=cal.get(日历年);
m=校准获取(日历月);
d=校准获取(日历日,每月);
校准设置(年、月、日);
a=y-校准获取(日历年);
如果((m
以下是我的问题解决方案:

/**
 * Method to extract the user's age from the entered Date of Birth.
 * 
 * @param DoB String The user's date of birth.
 * 
 * @return ageS String The user's age in years based on the supplied DoB.
 */
private String getAge(int year, int month, int day){
    Calendar dob = Calendar.getInstance();
    Calendar today = Calendar.getInstance();

    dob.set(year, month, day); 

    int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

    if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
        age--; 
    }

    Integer ageInt = new Integer(age);
    String ageS = ageInt.toString();

    return ageS;  
}
/**
*方法从输入的出生日期中提取用户的年龄。
* 
*@param DoB String用户的出生日期。
* 
*@return ageS字符串根据提供的DoB以年为单位的用户年龄。
*/
私有字符串获取(整数年、整数月、整数天){
Calendar dob=Calendar.getInstance();
Calendar today=Calendar.getInstance();
设定日期(年、月、日);
int age=today.get(Calendar.YEAR)-dob.get(Calendar.YEAR);
如果(今天获取(日历年中的日期)

我使用了一个函数来获取此处所需的输入值。此方法与日期选择器一起,专门用于获取用户的DoB并计算其年龄。根据您的具体实现,可以稍微修改以允许用户DoB的字符串输入。字符串的返回类型用于更新文本视图,可以稍微修改以允许类型
int
输出。

以下是经过充分测试的工作代码

下面是一个使用日期选择器对话框的示例

@Override
    public void onDateSet(DatePicker view, int mBirthYear, int mMonthOfYear, int mDayOfMonth) 
    {
            mAge = year - mBirthYear;

            if( (mMonthOfYear == month && day < mDayOfMonth) || ( month < mMonthOfYear) ) 
            {
                mAge--;
            }

            String years = getString(R.string.years);

            etAge.setText(mAge+years);      

    }
@覆盖
public void onDateSet(日期选择器视图,int-mBirthYear,int-mMonthOfYear,int-mDayOfMonth)
{
mAge=年份-年份;
如果((月日<月日)| |(月日<月日))
{
法师--;
}
String years=getString(R.String.years);
setText(法师+年);
}

这非常有效

public int getAge(int DOByear, int DOBmonth, int DOBday) {

        int age;

        final Calendar calenderToday = Calendar.getInstance();
        int currentYear = calenderToday.get(Calendar.YEAR);
        int currentMonth = 1 + calenderToday.get(Calendar.MONTH);
        int todayDay = calenderToday.get(Calendar.DAY_OF_MONTH);

        age = currentYear - DOByear;

        if(DOBmonth > currentMonth) {
            --age;
        } else if(DOBmonth == currentMonth) {
            if(DOBday > todayDay){
                --age;
            }
        }
        return age;
    }

所有发布的解决方案都不适合我。所以,我用我自己的逻辑得出了这个,对我来说100%有效:

 Integer getAge(Integer year, Integer month, Integer day) {
     Calendar dob = Calendar.getInstance();
     Calendar today = Calendar.getInstance();

     dob.set(year, month, day);
     int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

     if(month == (today.get(Calendar.MONTH)+1) && day > today.get(Calendar.DAY_OF_MONTH)) {
         age--;
     }

     return age;
 }
私有字符串获取(整数年、整数月、整数天){
Calendar dob=Calendar.getInstance();
Calendar today=Calendar.getInstance();
设定日期(年、月、日);
今天。设置(今天。获取(日历。年),今天。获取(日历。月)+1,
today.get(Calendar.DATE));
int age=today.get(Calendar.YEAR)-dob.get(Calendar.YEAR);
如果(今天获取(日历年中的日期)
以下内容将尽可能准确地提供您的年龄

public static String getAgeFromLong(String birthday) {

    SimpleDateFormat sdf= new SimpleDateFormat("ddMMyyyy",Locale.ENGLISH);
    Calendar dobCal=Calendar.getInstance();
    dobCal.setTime(sdf.parse(birthday));

    Calendar diffCal = Calendar.getInstance();
    diffCal.setTimeInMillis(Calendar.getInstance().getTimeInMillis() - dobCal.getInstance().getTimeInMillis());

    String ageS = "";

    int age = diffCal.get(Calendar.YEAR) - 1970;
    //Check if less than a year
    if (age == 0) {
        age = diffCal.get(Calendar.MONTH);
        //Check if less than a month
        if (age == 0) {
            age = diffCal.get(Calendar.WEEK_OF_YEAR);
            //Check if less than a week
            if (age == 1) {
                age = diffCal.get(Calendar.DAY_OF_YEAR);
                ageS = (age - 1) + " Days";
            } else {
                ageS = (age - 1) + " Weeks";
            }
        } else {
            ageS = age + " Months";
        }
    } else {
        ageS = age + " Years";
    }

    return ageS;
}

我喜欢你使用年中的日期的方式,smart:)我也使用了日期选择器来获取DOB,但我将其转换为字符串以存储在数据库(SQLite)中,每次需要计算年龄时,我都将DOB作为参数返回,因此我添加了substring方法来获取年份,月和日。我使用一些零碎的答案构建了这个解决方案,因此,我相信
日历。年中的每一天
来自指向www.coderanch.com的答案。享受吧!很高兴能回报我所得到的帮助!好吧,我明白你的意思了。但是,您所描述的情况就是为什么我选择使用
Calendar.DAY\u OF_YEAR
字段的原因。只是对同一个解决方案采用不同的方法而已。最佳办法应部分由执行情况决定。这和个人偏好和风格有关。@Jon Egerton链接是“禁用”或“更改”。这很好……最重要的是,当你输入27-11-1989时,我的年龄是27岁,但显示为26岁……是的。其他答案在当前出生月份是错误的。年龄变化只影响一个月以上的婴儿。但是这段代码是从当天开始的。这必须是可接受的答案…即使在本月B日也能完美工作。当B日在本月B日时,以上所有答案都不能给我准确的答案。
public static String getAgeFromLong(String birthday) {

    SimpleDateFormat sdf= new SimpleDateFormat("ddMMyyyy",Locale.ENGLISH);
    Calendar dobCal=Calendar.getInstance();
    dobCal.setTime(sdf.parse(birthday));

    Calendar diffCal = Calendar.getInstance();
    diffCal.setTimeInMillis(Calendar.getInstance().getTimeInMillis() - dobCal.getInstance().getTimeInMillis());

    String ageS = "";

    int age = diffCal.get(Calendar.YEAR) - 1970;
    //Check if less than a year
    if (age == 0) {
        age = diffCal.get(Calendar.MONTH);
        //Check if less than a month
        if (age == 0) {
            age = diffCal.get(Calendar.WEEK_OF_YEAR);
            //Check if less than a week
            if (age == 1) {
                age = diffCal.get(Calendar.DAY_OF_YEAR);
                ageS = (age - 1) + " Days";
            } else {
                ageS = (age - 1) + " Weeks";
            }
        } else {
            ageS = age + " Months";
        }
    } else {
        ageS = age + " Years";
    }

    return ageS;
}