2015-09-28 281 views
0

我創建一個項目來選擇日期並在edittext中加載它,然後選擇另一個編輯文本要計算的日期並給出當前30天的當前日期選定的日期例子..如何使用當前日期加上日期選擇器加上30天

我創建的第一個和加載成功地和我不知道如何在未來30天內加載另一個EDITTEXT This is What i Trying To Do/say

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 
private ImageButton ib; 
private Calendar cal; 
private int day; 
private int month; 
private int year; 
private EditText et; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ib = (ImageButton) findViewById(R.id.imageButton1); 
    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); 
    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) { 
     et.setText(selectedDay + "/" + (selectedMonth + 1) + "/" 
       + selectedYear); 
    } 
}; 
} 

回答

1

您可以設置這樣

壓延對象
public Calendar getDate(int year, int month, int date) { 
    Calendar calendar = new GregorianCalendar(); 
    calendar.set(Calendar.DAY_OF_MONTH, date); 
    calendar.set(Calendar.MONTH, month); // if Jan is 0 
    calendar.set(Calendar.YEAR, year); 
    calendar.add(Calendar.DATE, 30); 
    return calendar; 
} 

你可以得到日期,月份和年份從壓延對象

public String convertLongToDate(long date) { 
    Date convertedDate = new Date(date); 
    if (date < 86400000 * 1000L) { 
     convertedDate = new Date(date * 1000L); 
    } 
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
    simpleDateFormat.setTimeZone(TimeZone.getDefault()); 
    return simpleDateFormat.format(convertedDate); 
} 

調用上述方法

convertLongToDate(calendar.getTimeInMillis()); 
+0

我得到西隧我需要的日期是,當我選擇任何date..the未來(30或31天)的月份要在下一個editext中自動加載 – Karthick

+0

檢查我設置日期並將30天添加到當前日期的代碼。如果您通過2015年9月28日的日期,對象將分裂2015年10月28日。 –

+0

您可以將日曆對象中的值讀取爲calendar.get(Calendar.DAY_OF_MONTH); –