private void initTimePicker1() {//选择出生年月日 //控制时间范围(如果不设置范围,则使用默认时间1900-2100年,此段代码可注释) //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11 Date curDate = new Date(System.currentTimeMillis());//获取当前时间 SimpleDateFormat formatter_year = new SimpleDateFormat("yyyy "); String year_str = formatter_year.format(curDate); int year_int = (int) Double.parseDouble(year_str); SimpleDateFormat formatter_mouth = new SimpleDateFormat("MM "); String mouth_str = formatter_mouth.format(curDate); int mouth_int = (int) Double.parseDouble(mouth_str); SimpleDateFormat formatter_day = new SimpleDateFormat("dd "); String day_str = formatter_day.format(curDate); int day_int = (int) Double.parseDouble(day_str); //获取两个月前日期 SimpleDateFormat YYYY = new SimpleDateFormat("yyyy"); SimpleDateFormat MM = new SimpleDateFormat("MM"); SimpleDateFormat DD = new SimpleDateFormat("dd"); Calendar c = Calendar.getInstance(); c.setTime(new Date()); c.add(Calendar.MONTH, -2); Date m = c.getTime(); int towy = (int) Double.parseDouble(YYYY.format(m)); int towm = (int) Double.parseDouble(MM.format(m)); int towd = (int) Double.parseDouble(DD.format(m)); Log.d("zl","获取两月前:"+towy+towm+towd); Calendar selectedDate = Calendar.getInstance();//系统当前时间 Calendar startDate = Calendar.getInstance(); startDate.set(towy, towm-1, towd);//需要-1,此为下标的 Calendar endDate = Calendar.getInstance(); endDate.set(year_int, mouth_int - 1, day_int); //时间选择器 TimePickerView pvTime1 = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() { @Override public void onTimeSelect(Date date, View v) {//选中事件回调 // 这里回调过来的v,就是show()方法里面所添加的 View 参数,如果show的时候没有添加参数,v则为null /*btn_Time.setText(getTime(date));*/ // tvTime.setText(getTime(date)); String senddate = getTime(date); ActionResult result = new ActionResult(TransResult.SUCC_QUERY_ALL, senddate); finish(result); } }) .setTitleText("选择日期")//标题 .setCancelText("取消")//取消按钮文字 .setSubmitText("确认")//确认按钮文字 .setType(new boolean[]{true, true, true, false, false, false}) //年月日时分秒 的显示与否,不设置则默认全部显示 .setLabel("年", "月", "日", "", "", "")//默认设置为年月日时分秒 .isCenterLabel(false) .setTextColorCenter(Color.BLACK)//设置选中项的颜色 .setTextColorOut(Color.GRAY)//设置没有被选中项的颜色 .setContentSize(21) .setDate(selectedDate) .setLineSpacingMultiplier(1.2f) // .setTextXOffset(-10, 0,10, 0, 0, 0)//设置X轴倾斜角度[ -90 , 90°] .setRangDate(startDate, endDate) // .setBackgroundId(0x00FFFFFF) //设置外部遮罩颜色 .setDividerColor(Color.GRAY)//设置分割线的颜色 .isDialog(true)//是否显示为对话框样式,不设置则默认底部弹出 .setDecorView(null) .build(); // pvTime1.showDialog(); pvTime1.show(); } private String getTime(Date date) {//可根据需要自行截取数据显示 // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); return format.format(date); }