roshanpeter
roshanpeter

Reputation: 1364

Android Datepicker date add and display in single digit

i am developing an android application in which a datepicker is there to select the date of birth ... I need to display this date in a text view and this date has to be added into a single digit... and display in a textview.. i did code where the date is displyed in a textview but i need to display the total sum of it..

for example if date of birth is 04 02 1984 then 0+4 0+2 1+9+8+4 so answer is 4, 2, 4 and this has to be displayed separate in a textview.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


     final DatePicker date = (DatePicker) findViewById(R.id.datePicker1);
     final TextView tvDate = (TextView) findViewById(R.id.textView2);


     date.init(date.getYear(), date.getMonth(), date.getDayOfMonth(),new OnDateChangedListener()
     {


    @Override
    public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        int sum = 0;
         String date=Integer.toString(arg3);
         String month=Integer.toString(arg2);
         String year=Integer.toString(arg1);

         tvDate.setText(month+date+year);

    }


     }  );
}
}

Upvotes: 0

Views: 1241

Answers (3)

vaisakh mv
vaisakh mv

Reputation: 71

First you check the validation:

String day = "";
if (dayOfMonth < 10) {
    day = "0" + dayOfMonth;
}else {
    day = String.valueOf(dayOfMonth);
}

String month = "";
int get_month = monthOfYear + 1;
if (get_month < 10) {
    month = "0" + get_month;
}else {
    month = String.valueOf(get_month);
}

String date = day + "-" + month + "-" + year;

Upvotes: 1

Satyam Koyani
Satyam Koyani

Reputation: 4274

CALL "sum" function for each of Day,Month and Year variables

int daySum=sum(Integer.parseInt(day));
int monthSum=sum(Integer.parseInt(month));
int yearSum=sum(Integer.parseInt(year));
tvDay.setText(daySum);
tvMonth.setText(monthSum);
tvYear.setText(yearSum);

int sum(int number){
        int remainder = number % 9;
        if (number == 0) {
            return number;
        } else {
            if (remainder == 0) {
                return number;
            }else{
                return remainder;
            }
        }
}

Upvotes: 1

dbkoren
dbkoren

Reputation: 1475

If i understand you correctly then what you are looking for is casting the String to ints in your onDateChanged method , so may b something like this :

int i = 0 ;
while(i<2 ) // 2 for day and month 4 for year ... or what ever fits u can also run until the length of the string
 dateDigit += Integer.parseInt.subString(i++,i++);
//this is just an idea , havent tested the code or anything

keep on doing that until u have a one digit number ( Link to post for this) or mayb some recursive method that will keep on doing that until u have only one digit

hope this fits

Upvotes: 0

Related Questions