• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Big doubt on date piker and show in to Main Activity

kin4o

Lurker
Hello i have a some problems whit an app that i m making, in date piker i wont show how many days left and what is my next birthday in the main Activity. i have a button on Main Activity so i press the button and goes to the date piker then i select my age and i won to show it My main activity layout.

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_data);


final DatePicker data =(DatePicker) findViewById(R.id.datePicker);
DatePicker.OnDateChangedListener onDateChangedListener=null;
data.init(1985,0,1, onDateChangedListener);

Button bt1 = (Button) findViewById(R.id.btndata);

bt1.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {

SharedPreferences dados =getSharedPreferences("info",0);
SharedPreferences.Editor editor= dados.edit();

final int dia =data.getDayOfMonth();
editor.putInt("dia",dia);

final int mes =data.getMonth();
editor.putInt("mes",mes);

final int ano =data.getYear();
editor.putInt("ano",ano);
editor.commit();

Intent intent=new Intent();
intent.setClass(getApplicationContext(),Dados_data.class);
startActivity(intent);

}
});
}
}


this is the activity date

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

TextView aniv =(TextView) findViewById(R.id.txtaniv);
TextView dias =(TextView) findViewById(R.id.diasfaltam);



// Resource Recovery
SharedPreferences dados=getSharedPreferences("info",0);
int dia = dados.getInt("dia",0);
//para mes ficar com a nomeração "normal"
int mes = dados.getInt("mes",0)+1;
int ano = dados.getInt("ano",0);

aniv.setText(dia + "/" + mes + "/" +ano);



// Perform account to determine how many days are left until the birthday
// Create two instances in the calendar
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();



// Let's get the current system date
int anoatual =cal2.get(Calendar.YEAR);
int mesatual =cal2.get(Calendar.MONTH);
int diaatual =cal2.get(Calendar.DAY_OF_MONTH);



// need to check if the birthday has already occurred or not this year
if(mesatual>mes-1) {
anoatual=anoatual+1;
}

if(mesatual==mes-1&&diaatual>dia){
anoatual=anoatual+1;
}


// set the next anniversary date, based on the retrieved data
cal1.set(anoatual, mes-1, dia);

// You need to represent the date in milliseconds to be able to make the difference between them
long milis1 = cal1.getTimeInMillis();
long milis2 = cal2.getTimeInMillis();

// Calculate the difference between the dates
long diff = milis1 - milis2;



// convert the difference in milliseconds to days
long diffDays = diff / (24 * 60 * 60 * 1000);


// Test the displayed result, to check that there are no errors in the calculations
Toast toast = Toast.makeText(getApplicationContext(),"number of days until anniversary
" +diffDays,Toast.LENGTH_LONG);
toast.show();
dias.setText(""+diffDays);
}
}

and this is the data
 
Last edited:
i found the answer it s to add this
cal1.set(ano,mes,dia);
aniv.setText((((cal2.getTimeInMillis() - cal1.getTimeInMillis())
/ (24 * 60 * 60 * 1000) / 365) +1) + "º");
 
Back
Top Bottom