欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

日期选择前后一周,日,年,月

程序员文章站 2022-03-11 08:26:34
之前做了一个小Demo,选择一个日期后,按前进向前一周,按后退向后一周,同理还有日,月,年的,自己在那算了好几个小时,在网上也没找到好一点的demo,今天贴出来,以后开发粘贴复制即可。上货!!!需要用到的三方依赖:implementation 'com.contrarywind:Android-PickerView:4.1.9'代码MainActivity:public class Main2Activity extends AppCompatActivity { priv...

之前做了一个小Demo,选择一个日期后,按前进向前一周,按后退向后一周,同理还有日,月,年的,自己在那算了好几个小时,在网上也没找到好一点的demo,今天贴出来,以后开发粘贴复制即可。

上货!!!

需要用到的三方依赖:

implementation 'com.contrarywind:Android-PickerView:4.1.9'

代码MainActivity:

public class Main2Activity extends AppCompatActivity {

    private TextView tvDate1;
    private TextView tvDate2;
    private TextView tvRight;
    private ImageView ivUp;
    private ImageView ivDown;
    private LinearLayout dialogLayout;
//    private PopupWindow selectPopup;

    private int index = 0;

    private Calendar selectedDate;
    private Calendar startDate;
    private Calendar endDate;
    boolean[] week_type = {true, true, true, false, false, false};
    boolean[] month_type = {true, true, false, false, false, false};
    boolean[] year_type = {true, false, false, false, false, false};
    private Date nowWeek;
    private Date nowWeekSub;
    private Date nowDay;
    private Date nowDaySub;
    private Date nowMonth;
    private int nowYear;
    private int nowYearSub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //左边的
        ivUp = (ImageView) findViewById(R.id.ivUp);
        tvDate1 = (TextView) findViewById(R.id.tvDateOne);
        //右边的
        ivDown = (ImageView) findViewById(R.id.ivDown);
        tvDate2 = (TextView)findViewById(R.id.tvDateTwo);
        //控制年,月,日,周账单的
        tvRight = (TextView)findViewById(R.id.tvRight);
        dialogLayout = findViewById(R.id.llselectDate);
        initView();
        tvDate1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = tvRight.getText().toString();
                if (s != null) {
                    if (s.equals("日账单")){
                        selectDate(1,week_type);
                    }else  if (s.equals("周账单")){
                        selectDate(2,week_type);
                    }else  if (s.equals("月账单")){
                        selectDate(3,month_type);
                    }else  if (s.equals("年账单")){
                        selectDate(4,year_type);
                    }
                }
            }
        });
        ivUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1 = tvRight.getText().toString();
                if (s1 != null) {
                    if (s1.equals("日账单")){
                        upDate(1);
                    }else  if (s1.equals("周账单")){
                        upDate(2);
                    }else  if (s1.equals("月账单")){
                        upDate(3);
                    }else  if (s1.equals("年账单")){
                        upDate(4);
                    }
                }
            }
        });
        ivDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s2 = tvRight.getText().toString();
                if (s2 != null) {
                    if (s2.equals("日账单")){
                        downDate(1);
                    }else  if (s2.equals("周账单")){
                        downDate(2);
                    }else  if (s2.equals("月账单")){
                        downDate(3);
                    }else  if (s2.equals("年账单")){
                        downDate(4);
                    }

                }
            }
        });
    }

    //选择日期
    private void selectDate(final int i,boolean []type) {
        //添加标题
        TimePickerView timePickerView = new TimePickerBuilder(Main2Activity.this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                if (i==1){
                    nowDay = date;
                    String time = new SimpleDateFormat("yyyy-MM-dd").format(date);
                    //现在选择的时间,往后一天
                    String nextTime = dayAdd(date);
                    try {
                        nowDaySub = new SimpleDateFormat("yyyy-MM-dd").parse(nextTime);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    tvDate1.setText(time);
                    tvDate2.setText(nextTime);
                }else if (i==2){
                    nowWeek = date;
                    String time = new SimpleDateFormat("yyyy-MM-dd").format(date);
                    //现在选择的时间,往后七天
                    String nextTime = weekAdd(date);
                    try {
                        nowWeekSub = new SimpleDateFormat("yyyy-MM-dd").parse(nextTime);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    tvDate1.setText(time);
                    tvDate2.setText(nextTime);
                }else if (i==3){
                    nowMonth = date;
                    String time = new SimpleDateFormat("yyyy-MM").format(date);
                    //现在选择的时间,往后一月
                    String nextTime = monthAdd(date);
                    tvDate1.setText(time);
                    tvDate2.setText(nextTime);
                }
                else if (i==4){
                    String time = new SimpleDateFormat("yyyy").format(date);
                    nowYear = Integer.parseInt(time);
                    //现在选择的时间,往后一年
                    String nextTime = yearAdd(nowYear);
                    nowYearSub = Integer.parseInt(nextTime);
                    tvDate1.setText(time);
                    tvDate2.setText(nextTime);
                }

            }
        })
                //添加标题
                .setTitleText("请选择")
                .setTitleSize(16)
                .setCancelColor(Color.parseColor("#656565"))
                .setSubmitColor(Color.parseColor("#1E90FF"))
                .setContentTextSize(13)
                //是否为Dialog样式
                .isDialog(true)
                //显示添加的控件位置
                .setDecorView(dialogLayout)
                //设置时间,数组代表年月日时分秒,
                .setType(type)
                //开始和结束年份
                .setRangDate(startDate,endDate)
                //当前选择年份
                .setDate(selectedDate)
                .setItemVisibleCount(4)//可见行数
                .setLineSpacingMultiplier(3)//行间距
                .setDividerColor(Color.parseColor("#1E90FF"))
                .build();
        timePickerView.show();

    }
    //上一周,日,月,年
    private void upDate(final int i){
        if (i==1){
            try {
                String right = daySub(nowDaySub);
                nowDaySub = new SimpleDateFormat("yyyy-MM-dd").parse(right);
                String left = daySub(nowDaySub);
                tvDate1.setText(left);
                tvDate2.setText(right);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else if (i==2){
            try {
                String right = weekSub(nowWeekSub);
                nowWeekSub = new SimpleDateFormat("yyyy-MM-dd").parse(right);
                String left = weekSub(nowWeekSub);
                tvDate1.setText(left);
                tvDate2.setText(right);

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else if (i==3){
            try {
                int a = 0;
                if (a == 0) {
                    String left = monthSub(nowMonth);
                    nowMonth = new SimpleDateFormat("yyyy-MM").parse(left);
                    String right = monthAdd(nowMonth);
//                    showLog("tagaaa","   "+left+"    "+right);
                    tvDate1.setText(left);
                    tvDate2.setText(right);
                    a = 1;
                } else {
                    String right = monthSub(nowMonth);
                    nowMonth = new SimpleDateFormat("yyyy-MM").parse(right);
                    String left = monthSub(nowMonth);
//                    showLog("tagaaa","   "+left+"    "+right);
                    tvDate1.setText(left);
                    tvDate2.setText(right);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else if (i==4){
            String right = yearSub(nowYearSub);
            nowYearSub = Integer.parseInt(right);
            String left = yearSub(nowYearSub);
            tvDate1.setText(left);
            tvDate2.setText(right);
        }
    }
    //下一周,日,月,年
    private void downDate(final int i){
        if (i==1){
            try {
                String left = dayAdd(nowDay);
                nowDay = new SimpleDateFormat("yyyy-MM-dd").parse(left);
                String right = dayAdd(nowDay);
                tvDate1.setText(left);
                tvDate2.setText(right);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else if (i==2){
            try {
                String left = weekAdd(nowWeek);
                nowWeek = new SimpleDateFormat("yyyy-MM-dd").parse(left);
                String right = weekAdd(nowWeek);
                tvDate1.setText(left);
                tvDate2.setText(right);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else if (i==3){
            try {
                String left = monthAdd(nowMonth);
                nowMonth = new SimpleDateFormat("yyyy-MM").parse(left);
                String right = monthAdd(nowMonth);
                tvDate1.setText(left);
                tvDate2.setText(right);

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else if (i==4){
            String left = yearAdd(nowYear);
            nowYear = Integer.parseInt(left);
            String right = yearAdd(nowYear);
            tvDate1.setText(left);
            tvDate2.setText(right);
        }
    }

    //初始化控件
    protected void initView() {
        tvRight.setVisibility(View.VISIBLE);
        //这边直接写死了是时间类型
        tvRight.setText("周账单");
        tvRight.setTextSize(14);

        //设置当前日期
        initDtate();
    }

    private void initDtate() {
        //当前日期
        selectedDate = Calendar.getInstance();
        //获取当前年份
        int year = selectedDate.get(Calendar.YEAR);
        //开始日期
        startDate = Calendar.getInstance();
        //结束日期
        endDate = Calendar.getInstance();
        //月份是从0-11,日是1-31的
        int startYear = year - 1;
        startDate.set(startYear, 0, 1);
        endDate.set(year, 11, 31);

        nowWeek=new Date(System.currentTimeMillis());
        nowWeekSub=new Date(System.currentTimeMillis());
        tvDate2.setText(new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()));
        String nextTime = weekSub(new Date(System.currentTimeMillis()));
        tvDate1.setText(nextTime);
    }

    public String weekAdd(Date date) {
        Calendar select_week = Calendar.getInstance();
        select_week.setTime(date);
        int i = select_week.get(Calendar.DAY_OF_MONTH) + 7;
        select_week.set(Calendar.DAY_OF_MONTH, i);
        Date nextWeek = select_week.getTime();
        String nextTime = new SimpleDateFormat("yyyy-MM-dd").format(nextWeek);
        return nextTime;
    }

    public String weekSub(Date date) {
        Calendar select_week = Calendar.getInstance();
        select_week.setTime(date);
        int i = select_week.get(Calendar.DAY_OF_MONTH) - 7;
        select_week.set(Calendar.DAY_OF_MONTH, i);
        Date nextWeek = select_week.getTime();
        String nextTime = new SimpleDateFormat("yyyy-MM-dd").format(nextWeek);
        return nextTime;
    }

    public String dayAdd(Date date) {
        Calendar select_week = Calendar.getInstance();
        select_week.setTime(date);
        int i = select_week.get(Calendar.DAY_OF_MONTH) + 1;
        select_week.set(Calendar.DAY_OF_MONTH, i);
        Date nextWeek = select_week.getTime();
        String nextTime = new SimpleDateFormat("yyyy-MM-dd").format(nextWeek);
        return nextTime;
    }

    public String daySub(Date date) {
        Calendar select_week = Calendar.getInstance();
        select_week.setTime(date);
        int i = select_week.get(Calendar.DAY_OF_MONTH) - 1;
        select_week.set(Calendar.DAY_OF_MONTH, i);
        Date nextWeek = select_week.getTime();
        String nextTime = new SimpleDateFormat("yyyy-MM-dd").format(nextWeek);
        return nextTime;
    }

    public String monthAdd(Date date) {
        Calendar select_week = Calendar.getInstance();
        select_week.setTime(date);
        int i = select_week.get(Calendar.MONTH);
        int year = select_week.get(Calendar.YEAR);
        if (i == 12) {
            year = select_week.get(Calendar.YEAR) + 1;
            i = 1;
        }
        i++;
        select_week.set(Calendar.MONTH, i);
        select_week.set(Calendar.YEAR, year);
        Date nextWeek = select_week.getTime();
        String nextTime = new SimpleDateFormat("yyyy-MM").format(nextWeek);
        return nextTime;
    }

    public String monthSub(Date date) {
        Calendar select_week = Calendar.getInstance();
        select_week.setTime(date);
        int i = select_week.get(Calendar.MONTH);
        int year = select_week.get(Calendar.YEAR);
        if (i == 0) {
            year = select_week.get(Calendar.YEAR) - 1;
            i = 12;
        }
        i--;
        select_week.set(Calendar.MONTH, i);
        select_week.set(Calendar.YEAR, year);
        Date nextWeek = select_week.getTime();
        String nextTime = new SimpleDateFormat("yyyy-MM").format(nextWeek);
        return nextTime;
    }

    public String yearAdd(int now) {
        now++;
        return now + "";
    }

    public String yearSub(int now) {
        now--;
        return now + "";
    }
}

XML部分:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.mdj.acer.dxtest.Main2Activity">

    <TextView
        android:id="@+id/tvRight"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:text="666"
        />

    <LinearLayout
        android:id="@+id/llselectDate"
        android:layout_width="343dp"
        android:layout_height="48dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="16dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/ivUp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/tvDateOne"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:drawablePadding="11.5dp"
            android:text="2020.05.12" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="11dp"
            android:layout_marginRight="11dp"
            android:text=" - -" />

        <TextView
            android:id="@+id/tvDateTwo"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:drawablePadding="11.5dp"
            android:text="2020.05.12" />

        <ImageView
            android:id="@+id/ivDown"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@mipmap/ic_launcher" />
    </LinearLayout>
</LinearLayout>

 

本文地址:https://blog.csdn.net/Motejia/article/details/107182717