我想在获取当前日期后获取用户的当前日期文本视图想要自动显示100天的日期
即如果我选择 15-12-2015 文字视图想要显示 24-03-2016
这是我的代码:
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
private Button ib;
private Calendar cal;
private int day;
private int month;
private int year;
private EditText et;
private TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib = (Button) findViewById(R.id.button);
cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
et = (EditText) findViewById(R.id.editText);
display = (TextView) findViewById(R.id.textView3);
ib.setOnClickListener((View.OnClickListener) this);
}
@Override
public void onClick(View v) {
showDialog(0);
}
@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
return new DatePickerDialog(this, datePickerListener, year, month, day);
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
display.setText(selectedDay + 100) + " / " + (selectedMonth ) + " / "
+ selectedYear;
}
};
答案 0 :(得分:1)
使用Calender
类将号码添加到日期中。确保日期格式良好。
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
String dtStartDate = selectedYear+"-"+selectedMonth+"-"+selectedDay+"00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar c = Calendar.getInstance();
c.setTime(dtStartDate);
c.add(Calendar.DATE, 100); // number of days to add
String dt = sdf.format(c.getTime()); // dt is now the new date
display.setText(dt);
}
};
答案 1 :(得分:1)
尝试这样的事情
Date dtStartDate=//User inputs start date.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar c = Calendar.getInstance();
c.setTime(dtStartDate);
c.add(Calendar.DATE, 100); // number of days to add
String datePlus100 = sdf.format(c.getTime()); // datePlus100 shows date inputted + 100 days!
答案 2 :(得分:0)
此代码有效。我无法理解听众无法处理解决方案中的操作
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
@Override
public void onClick(View v) {
showDialog(0);
}
@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
if (id == 0) {
return new DatePickerDialog(MainActivity.this, datePickerListener, year, month, day);
}
return super.onCreateDialog(id);
}
OnDateSetListener myCallBack = new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
display.setText("set date from calendar as answered upper");
}
};