2015-09-28 286 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); –