试图建立一个年龄计算器

时间:2013-08-26 07:24:59

标签: android calculator

如果我给出出生日期(dob),那么我们 计算当前年龄 在android中:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
        textView_cage = (TextView) findViewById(R.id.textView_cage);
        textView_currentagediff = (TextView) findViewById(R.id.textView_currentagediff);
        textView4 = (TextView) findViewById(R.id.textView4);
        mPickDate = (Button) findViewById(R.id.pickDate);

        mPickDate.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ValidFragment")
            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            public void onClick(View v) {

                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getFragmentManager(), "datePicker");
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        public Dialog onCreateDialog(Bundle savedInstanceState) {

            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
            System.out.println("Current Setdate");
            System.out.println("Year" + year);
            System.out.println("Month" + month);
            System.out.println("Date" + day);

            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {

            mDateDisplay.setText(String.valueOf(day) + "/" + String.valueOf(month + 1) + "/" + String.valueOf(year));
            Date d = new Date(year, month, day);
            System.out.println("Current Setdate");

            Date d1 = new Date();
            int year2 = d1.getYear();
            int month2 = d1.getMonth();
            int day2 = d1.getDate();

            System.out.println("Current aaj ki date");
            System.out.println("Year" + year2);
            System.out.println("Month" + month2);
            System.out.println("Date" + day2);

            int year3 = year2 - year;
            int month3 = (month2 + 1) - month;
            int day3 = day2 - day;
            System.out.println("Current aaj kitnhesal k ho gaye ");
            System.out.println("Year" + year3);
            System.out.println("Month" + month3);
            System.out.println("Date" + day3);
            setnewage(year3, month3, day3);

        }

        private void setnewage(int year4, int month4, int day4) {
            Date d5 = new Date(year4, month4, day4);

            CharSequence day = DateFormat.format("d", d5);
            CharSequence month = DateFormat.format("M", d5);
            CharSequence year = DateFormat.format("yyyy", d5);

            textView_currentagediff.setText(year + " Years " + month + " Months and " + day + " days");
            Date d1 = new Date();
            int year2 = d1.getYear();
            int month2 = d1.getMonth();
            int day2 = d1.getDate();


    }

    }

}

我想通过Datepickerdialog选择出生日期,在选择日期后,我希望获得当前年龄。 请在Android中给我一些示例。

2 个答案:

答案 0 :(得分:2)

你可以试试这个:

// get the current date in format specified.
    public String getCurrentDate(){

    SimpleDateFormat df = new SimpleDateFormat("dd-MMMM-yyyy hh:mm aa");
    return  df.format(Calendar.getInstance().getTime());
}

public void getDateDifference(String dob){

            String dateOfBirth = dob;
        String dateCurrent = getCurrentDate();

        //HH converts hour in 24 hours format (0-23), day calculation
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

        Date d1 = null;
        Date d2 = null;

        try {
            d1 = format.parse(dateOfBirth);
            d2 = format.parse(dateCurrent);

            //in milliseconds
            long diff = d2.getTime() - d1.getTime();

            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);               

        } catch (Exception e) {
            e.printStackTrace();
        }
}

第1步:使用DatePickerDialog获取输入的出生日期。调用方法getDateDifference并将dob的值作为参数传递。

第2步:使用setText()方法将获得的差异设置为文本视图。

答案 1 :(得分:1)

这是一个简单的java代码,您必须提供您的出生日期和当前日期,然后您的最终结果将是您的年龄。

public static int getDiffBetYears(Date first, Date last) {
    Calendar firstDate = getCalendar(first);
    Calendar secDate = getCalendar(last);
    int diff = secDate.get(YEAR) - firstDate.get(YEAR);
    if (firstDate.get(MONTH) > secDate.get(MONTH) || 
        (firstDate.get(MONTH) == secDate.get(MONTH) && a.get(DATE) > secDate.get(DATE))) {
        diff--;
    }
    return diff;
}

public static Calendar getCalendar(Date date) {
    Calendar cal = Calendar.getInstance(Locale.US);
    cal.setTime(date);
    return cal;
}