我的规范是:
edittext
字段。26/DEC/2013
必须显示第一个edittext字段
该2/JAN/2014
today(first-edit-text)
直到下一个7 days(second-edit-text)
快照:
我只有基本的代码结构:
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:ems="10" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignLeft="@+id/editText1"
android:ems="10" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="From" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="68dp"
android:text="To" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
我的问题
注意:我不是在谈论使用日期选择器;我试图在没有它的情况下实现这一点。
答案 0 :(得分:3)
您只需将今天的日期转换为此特定格式:
Calendar c22 = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy");
String cur_date = sdf.format(c22.getTime());
editText1.setText(cur_date);
现在添加7天到今天的日期:
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 7); // Adding 7 days
SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy");
String nxt_date = sdf.format(c.getTime());
editText2.setText(nxt_date);
尝试这个解决方案。
答案 1 :(得分:2)
您可以执行以下操作:
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
edttext1.setText((cal.get(Calendar.DAY_OF_MONTH)+1)+"-"+cal.get(Calendar.MONTH)+"-"+cal.get(Calendar.YEAR));
//This will set the current date
//Now extend your date 7 days ahead
cal.add(Calendar.DAY_OF_MONTH, 7);
edttext2.setText((cal.get(Calendar.DAY_OF_MONTH)+1)+"-"+cal.get(Calendar.MONTH)+"-"+cal.get(Calendar.YEAR));
答案 2 :(得分:1)
您可以查看此答案this
并计算活动onStart方法的时间。
答案 3 :(得分:1)
在 MainActivity
中尝试此操作 public class MainActivity extends Activity {
EditText edt;
EditText edt2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt=(EditText)findViewById(R.id.editText1);
edt2=(EditText)findViewById(R.id.editText2);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
Calendar c1=Calendar.getInstance();
c1.setTime(new Date());
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 7); // Adding 7 days
String ToDate = sdf.format(c.getTime());
String FromDate = sdf.format(c1.getTime());
edt2.setText(FromDate);
edt.setText(ToDate);
}