0

生年月日(dob)が与えられた場合、アンドロイドで現在の年齢を計算する方法:

    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での例を教えてください。

4

2 に答える 2

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()

于 2013-08-26T07:56:44.847 に答える
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;
}
于 2013-08-26T07:34:29.787 に答える