1. Are you ready for the Galaxy S20? Here is everything we know so far!

Big doubt on date piker and show in to Main Activity

Discussion in 'Android Development' started by kin4o, Apr 24, 2020.

  1. kin4o

    kin4o Lurker
    Thread Starter

    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
     


    #1 kin4o, Apr 24, 2020
    Last edited: Apr 26, 2020

    1. Download the Forums for Android™ app!


      Download

       
  2. Brian706

    Brian706 I like turtles!
    Moderator

    Any developers around that can look at this?
     
    kin4o likes this.
  3. kin4o

    kin4o Lurker
    Thread Starter

    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) + "º");
     
    Brian706 likes this.
  4. Brian706

    Brian706 I like turtles!
    Moderator

    Thank you for sharing your solution!
     
    kin4o likes this.
Loading...
Similar Threads - Big doubt date
  1. App Update
    Replies:
    0
    Views:
    285
  2. Leon Euler
    Replies:
    1
    Views:
    265
  3. Srapisar
    Replies:
    5
    Views:
    2,409
  4. ocnbrze
    Replies:
    8
    Views:
    1,068
  5. ironass
    Replies:
    5
    Views:
    997
  6. ironass
    Replies:
    5
    Views:
    1,326
  7. ironass
    Replies:
    6
    Views:
    1,610
  8. ironass
    Replies:
    2
    Views:
    1,031
  9. ironass
    Replies:
    2
    Views:
    786
  10. ironass
    Replies:
    2
    Views:
    1,069

Share This Page

Loading...