如何从Android中的日期选择器选择的日期计算下一个生日的人剩余的天数? [英] How to calculate the remaining days left for a persons next birthday from the date pickers selected date in android?

查看:146
本文介绍了如何从Android中的日期选择器选择的日期计算下一个生日的人剩余的天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,该程序根据从日期选择器中选择的日期及其正常工作来计算一个人的年龄。
现在,我添加了另一种方法来仅显示剩余天数以及该人生日在其下一个生日的那一天

I have written a program to calculate the age of a person according to the date selected from the date picker , and its working fine. Now i added another method to display just the days remaining and the day on which the persons birthday falls on his next birthday

private void calculateNextBirthday() {
    // TODO Auto-generated method stub

    Date dt = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat(
                "yyyy/MM/dd");
    final String strBDay = sdf.format(dt);
    try {

        dt = sdf.parse(strBDay);
    } catch (final java.text.ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    final Calendar c = Calendar.getInstance();

    c.setTime(dt);
    // c.add(Calendar.DATE, value);
    final Calendar today = Calendar.getInstance();
    // Take your DOB Month and compare it to current
    // month
    final int BMonth = c.get(Calendar.MONTH);
    final int CMonth = today.get(Calendar.MONTH);
    c.set(Calendar.YEAR, today.get(Calendar.YEAR));
    c.set(Calendar.DAY_OF_WEEK,
                today.get(Calendar.DAY_OF_WEEK));
    if (BMonth <= CMonth) {
        c.set(Calendar.YEAR,
                    today.get(Calendar.YEAR) + 1);
    }
    // Result in millis

    final long millis = c.getTimeInMillis()
                        - today.getTimeInMillis();
    // Convert to days
    final long days = millis / 86400000; // Precalculated
                                                    // (24 *
                                                    // 60 *
                                                    // 60 *
                                                    // 1000)
    // final String dayOfTheWeek =
    // sdf.format(BDay.getTime());
    sdf = new SimpleDateFormat("EEEE");
    final String dayOfTheWeek = sdf.format(dt);
    txt8.setText("" + days + " days");
    txt10.setText("" + dayOfTheWeek);
}

该程序中的问题是,根据今天的情况,剩余的工作日日期,但不是从日期选择器中选择的日期,这意味着即使我从日期选择器中选择任何其他日期,剩余的天数仍保持不变,并且星期几也显示相同,我希望两者都根据选择的日期进行更改

The problem in this program is the remaining days left works on the basis of todays date but not from the dates selected from the date picker, means even if i select any other date from the date picker the remaining days remains the same and the dayoftheweek also shows the same , i want both to change according the dates selected

还附加了两个截屏,带有两个不同的生日,并且截屏的最后两行输出相同

also attaching the 2 screenshots with two different birthdates and the output is same on the last two rows of my screenshot

< img src = https://i.stack.imgur.com/I9AtW.png alt =在此处输入图片描述>


当从日期选择器中选择任何日期时,我的程序会正确显示人的年龄以同样的方式,我希望我的剩余天数以及该人生日的那天发生变化,到从日期选择器
中选择任何日期时,需要一些帮助p关于我需要在上面的代码
中进行哪些更改,任何建议将对您有所帮助
谢谢您

My program shows the age of the person correctly when selected any date from the date picker , in the same way i would want my remaining days and the day on which the persons birthday falls to change, to when selected any date from the date picker please need some help as to what changes i need to do in the above code any suggestions would be helpful thanking you

推荐答案

问题是:

您正在使用当前日期计算差异。

You are calculating difference using current date.

请参见修改后的功能:

See a little modified function:

private void calculateNextBirthday() {
    // TODO Auto-generated method stub

    /*Date dt = new Date();  //this is just current date, which was causing issue. It should be user selected date
    SimpleDateFormat sdf = new SimpleDateFormat(
                "yyyy/MM/dd");
    final String strBDay = sdf.format(dt);
    try {

        dt = sdf.parse(strBDay);
    } catch (final java.text.ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }*/

    final Calendar c = Calendar.getInstance();
    c.set(1990, 8, 03);  //this is birthdate. set it to date you get from date picker
    // c.add(Calendar.DATE, value);
    final Calendar today = Calendar.getInstance();
    // Take your DOB Month and compare it to current
    // month
    final int BMonth = c.get(Calendar.MONTH);
    final int CMonth = today.get(Calendar.MONTH);

    final int BDate = c.get(Calendar.DAY_OF_MONTH);
    final int CDate = today.get(Calendar.DAY_OF_MONTH);

    c.set(Calendar.YEAR, today.get(Calendar.YEAR));
    c.set(Calendar.DAY_OF_WEEK,
                today.get(Calendar.DAY_OF_WEEK));
    if (BMonth < CMonth) {
        c.set(Calendar.YEAR,
                    today.get(Calendar.YEAR) + 1);
    }
    //added this condition so that it doesn't add in case birthdate is greater than current date . i.e. yet to come in this month. 
    else if (BMonth == CMonth){
        if(BDate < CDate){
            c.set(Calendar.YEAR,
                    today.get(Calendar.YEAR) + 1);
        }
    }
    // Result in millis

    final long millis = c.getTimeInMillis()
                        - today.getTimeInMillis();
    // Convert to days
    final long days = millis / 86400000; // Precalculated
                                                    // (24 *
                                                    // 60 *
                                                    // 60 *
                                                    // 1000)
    // final String dayOfTheWeek =
    // sdf.format(BDay.getTime());
    /*sdf = new SimpleDateFormat("EEEE");
    final String dayOfTheWeek = sdf.format(dt);*/
    Toast.makeText(getApplicationContext(), String.valueOf(days), Toast.LENGTH_SHORT).show();

}

希望这会有所帮助。

这篇关于如何从Android中的日期选择器选择的日期计算下一个生日的人剩余的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆