选择时间自动算出之后几天的日期

       工作是遇到的需要推算选择时间之后的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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值