工作是遇到的需要推算选择时间之后的7天日期,网上找了很多方法借鉴一下,写了一个简单的demo,希望对大家有帮助。两个月之前写的如果发现是抄袭的请告诉我 我马上删除,下面附上源码:
只有一个MainActivity类:
public class MainActivity extends Activity {
TextView tv1, tv2, tv3, tv4, tv5, tv6, tv7, tv8;
Button btButton;
List<String> dates = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
tv5 = (TextView) findViewById(R.id.textView5);
tv6 = (TextView) findViewById(R.id.textView6);
tv7 = (TextView) findViewById(R.id.textView7);
tv8 = (TextView) findViewById(R.id.textView8);
btButton = (Button) findViewById(R.id.button);
btButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dates.removeAll(dates);
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
tv8.setText(year + "-" + (monthOfYear + 1)+ "-" + dayOfMonth);//选择的时间
//循环7次得到选择时间之后的7天日期
for (int i = 0; i < 7; i++) {
final Calendar calendar = Calendar.getInstance();
calendar.set(year, (monthOfYear),dayOfMonth);
calendar.setTimeInMillis(calendar.getTimeInMillis()+ i* 24* 60* 60 * 1000);
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
SimpleDateFormat sdDateFormat = new SimpleDateFormat("MM/dd");
dates.add(sdDateFormat.format(calendar.getTime()));
}
//时间输出
tv1.setText(dates.get(0) + "");
tv2.setText(dates.get(1) + "");
tv3.setText(dates.get(2) + "");
tv4.setText(dates.get(3) + "");
tv5.setText(dates.get(4) + "");
tv6.setText(dates.get(5) + "");
tv7.setText(dates.get(6) + "");
}
}, 2016, 2, 25);
dialog.show();
}
});
}
}
布局文件:
<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="com.example.zhexiantu.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="15dp"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="16dp"
android:text="TextView" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="19dp"
android:text="TextView" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="24dp"
android:text="TextView" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView5"
android:layout_centerVertical="true"
android:text="TextView" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView6"
android:layout_below="@+id/textView6"
android:layout_marginTop="22dp"
android:text="TextView" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView7"
android:layout_below="@+id/textView7"
android:layout_marginTop="22dp"
android:text="TextView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView8"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="选时间" />
</RelativeLayout>