Reputation: 129
I have enter the date from DatePicker in 1st EditText but but it get only current date , when i select the date from date picker in 1st EditText and when i insert the any int value in 2nd Edit text it will gate change the date in 3rd Edit Text. So how can do this I m trying since 5 hours but can't get proper solution.Can someone help me please. Thanks to appreciate.
For example:
Here is my Activity code .
// Get current date by calender
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
etReplacementDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
etReplacementDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(DATE_OF_REPLACEMENT);
}
});
String fixedDate = etReplacementDate.getText().toString().trim();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
convertedDate = new Date();
try
{
convertedDate = dateFormat.parse(fixedDate);
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Date convertedDate = " + convertedDate);
String intervalDays = etInterval_Days.getText().toString().trim();
if(intervalDays.trim().length()>0){
try
{
intConvertDays =Integer.parseInt(intervalDays);
}
catch(NumberFormatException ne){
System.out.println("could not parse :: " +ne);
}
}
System.out.println("strConvertDays in afterTextChanged: " + intConvertDays);
etInterval_Days.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable editable)
{
// TODO Auto-generated method stub
String intervalDays = etInterval_Days.getText().toString().trim();
if(intervalDays.trim().length()>0){
try
{
intConvertDays =Integer.parseInt(intervalDays);
}
catch(NumberFormatException ne){
System.out.println("could not parse :: " +ne);
}
}
cal2.setTime(convertedDate);
cal2.add(Calendar.MONTH, intConvertDays);
Date resultDate = cal2.getTime();
String strResultDate = new SimpleDateFormat("yyyy-MM-dd").format(resultDate);
System.out.println("After one days strResultDate : " + strResultDate);
SimpleDateFormat simpleDate = new SimpleDateFormat("dd/MM/yyyy");
String strConverted_Date = simpleDate.format(resultDate);
etNextReplanishmentDate.setText(strConverted_Date);
cal2.add(Calendar.DATE, -2);
Date beforDate = cal2.getTime();
String beforDate_String = new SimpleDateFormat("yyyy-MM-dd").format(beforDate);
System.out.println("beforDate_String: " + beforDate_String);
}
});
}
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DATE_OF_REPLACEMENT:return new DatePickerDialog(this, pickerListenerReplacement, year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener pickerListenerReplacement = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Show selected date
etReplacementDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};
Upvotes: 1
Views: 947
Reputation: 5786
Try something like this -
String finalDate = day + " " + month + " " + year;
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(finalDate));
} catch (ParseException e) {
e.printStackTrace();
}
int addDays = Integer.parseInt(editText2.getText().toString());
c.add(Calendar.DATE, addDays); // number of days to add
SimpleDateFormat sdf1 = new SimpleDateFormat("dd MMMM yyyy");
String output = sdf1.format(c.getTime());
To automatically change date in editText3, you can use TextWatcher -
TextWatcher tw = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
//get values in editText1 and 2 and add them
int val1 = Integer.parseInt(editText1.getText().toString());
int val2 = Integer.parseInt(editText2.getText().toString());
//add the above code here
String finalDate = day + " " + month + " " + year;
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(finalDate));
} catch (ParseException e) {
e.printStackTrace();
}
String addDays = Integer.parseInt(editText2.getText().toString());
c.add(Calendar.DATE, addDays); // number of days to add
SimpleDateFormat sdf1 = new SimpleDateFormat("dd MMMM yyyy");
String output = sdf1.format(c.getTime());
editText3.setText(output + "");
}
};
Upvotes: 1