1

This is my code for selecting the date from a date picker.

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

public class CurrentDateActivity extends Activity {
/** Called when the activity is first created. */

private EditText mDateDisplay,mDateSelect;
private int mYear,mMonth,mDay;
private int year,month,day;  

Button selectBtn = null;
Calendar c = null;
static final int DATE_DIALOG_ID = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.date_select);

    selectBtn = (Button)findViewById(R.id.BtnSubmit);
    mDateDisplay = (EditText) findViewById(R.id.currentDate);
    mDateSelect = (EditText)findViewById(R.id.dateofBirth);

    // add a click listener to the button
    mDateSelect.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    getCurrentDate();//get the current date


    // display the current date 
    mDateDisplay.setText(getString(R.string.strSelectedDate,
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mDay).append("/")
                    .append(mMonth + 1).append("/")
                    .append(mYear).append(" "))
                    );
}

private void getCurrentDate(){
     // get the current date
    c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
}

// updates the date in the EditText
private void updateDisplay() {
    mDateSelect.setText(getString(R.string.strSelectedDate,
            new StringBuilder()

            .append(mDay).append("/")
            .append(mMonth + 1).append("/")
            .append(mYear).append(" "))
            );  
}

// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
     public void onDateSet(DatePicker view, int year,  int monthOfYear, int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplay();
     }
};



@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:

        return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                mDay);
    }
    return null;
}   

}

Everything is fine but now i want to get both the current date and the date entered by the user in another activity in order to calculate the difference between them. But how to pass the date in intent i am not able to understand.Please help

XylemRaj
  • 752
  • 4
  • 12
  • 28
  • Refer to [this](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) question – Prashant Borde Dec 05 '13 at 13:11
  • I'm not a android pro but maybe this post can help you: [How to pass object from one activity to another in Android][1] [1]: http://stackoverflow.com/a/2736612/1751277 – floriangosse Dec 05 '13 at 13:11

1 Answers1

1

What if you were to use shared preferences?

    //get access to the shared preferences
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = preferences.edit();


    //get the current time
    Date now =new Date();

    //create timestamp from the time the user picked
    @SuppressWarnings("deprecation")
    @Deprecated
    Timestamp timestamp = new Timestamp(mYear, mMonth, mDay, now.getHour, now.getMinute,   0, 0);

    //store the timestamp in your shared preferences
    editor.putString(getResources().getString(R.string.calcDate),  timestamp.toString );
    editor.apply();



     //And then in your next activity, you can pull the value back out like 
     //Pull the long timestamp back out from the shared preferences
     long pickedTs = Long.parseLong(preferences.getString(getResources().getString(R.string.calcDate),   ""));
     //Then create a new Date object using the timestamp
     Date pickedDate = new Date(pickedTs);
     //Then clear that preference for the next time
     editor.putString(getResources().getString(R.string.calcDate),  "");
     editor.apply();

That's similar to the system I'm currently using in my application. http://developer.android.com/reference/android/content/SharedPreferences.html http://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html There may be a sexier way to do this, but that's just my recommendation. Hope it helps

AndrewSmiley
  • 1,785
  • 17
  • 31