我有两个datepicker
From
和to
我希望今天有日期
和来自的7天返回日期
例如今天的日期为01/10/2012
它应该在to
,而七天后,即23/9/2012
应该在From
我所做的是,我能够在今天的日期获得约会,但我怎样才能在7天前获得约会 我所做的是
Calendar ToDateCal=Calendar.getInstance();
DatePickerDialog.OnDateSetListener td=new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view,int year,int monthOfYear,int dayOfMonth) {
ToDateCal.set(Calendar.YEAR, year);
ToDateCal.set(Calendar.MONTH, monthOfYear);
ToDateCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateToDate();
}
};
private void updateToDate(){
try{
int iDay;
int iMonth;
int iYear;
iDay=ToDateCal.get(Calendar.DATE);
iMonth=ToDateCal.get(Calendar.MONTH);
iMonth=iMonth+1;
iYear=ToDateCal.get(Calendar.YEAR);
String sTDate=iDay+"/"+iMonth+"/"+iYear;
ToDate.setText(sTDate);
}
catch(Exception ex){
sResponse=ex.toString();
}
}
单击Edittext上的事件
ToDate.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
new DatePickerDialog(DRSTClaimPage.this,td,ToDateCal.get(Calendar.YEAR),
ToDateCal.get(Calendar.MONTH),ToDateCal.get(Calendar.DAY_OF_MONTH)).show();
}
});
并设置日期
FromDate.setText(dt.getStDt());
public String getStDt(){
return ((dt.getDate())+"/"+(dt.getMonth()+1)+"/"+(dt.getYear()+1900));
}
喜欢这个
答案 0 :(得分:3)
这可能会对您有所帮助:
Calendar currDate=Calendar.getInstance();
Calendar weekBackDate=Calendar.getInstance();
weekBackDate.add(Calendar.DAY_OF_MONTH,-7);
编辑:
仔细观察您的问题,我想,您希望在7天之前在FromDate中设置文字。然后你可以使用上面这样的代码:
private void updateToDate(){
try{
int iDay;
int iMonth;
int iYear;
iDay=ToDateCal.get(Calendar.DATE);
iMonth=ToDateCal.get(Calendar.MONTH);
iMonth=iMonth+1;
iYear=ToDateCal.get(Calendar.YEAR);
String sTDate=iDay+"/"+iMonth+"/"+iYear;
ToDate.setText(sTDate);
setFromDate();
}
catch(Exception ex){
sResponse=ex.toString();
}
}
private void setFromDate(){
FromDate.setText(String.format("%1$td / %1$tm / %1$tY",ToDateCal));
}