版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoshuxgh/article/details/88049282
会员到期提醒:还有一天、当天,一个月(七天一提醒) 等。
//会员提醒 19.1.15
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
// Date date = sdf.parse(bean.getData().get(0).getBeginDate());
Date date = new Date();
Date date1 = sdf.parse(bean.getData().get(0).getEndDate());
long month = getDifferMonth(date, date1);
int days = (int) ((date1.getTime() - date.getTime()) / (1000 * 3600 * 24));
if (month < 1 ){
long currentClickTime = System.currentTimeMillis();
if (date1.getDate() - date.getDate() == 1) {//还有一天会员到期
if (SPUtils.getPrefLong(getActivity().getApplicationContext(), "TIME_1_DAYStoday", 0) != date.getDate()) {
showMemberTime("会员还有" + (date1.getDate() - date.getDate()) + "天到期,请尽快续费",(date1.getDate() - date.getDate()));// 2018-03-29
}
SPUtils.setSettingLong(getActivity().getApplicationContext(), "TIME_1_DAYSNew", currentClickTime);
} else if (date1.getDate() - date.getDate() == 0) {//会员到期当天
if (SPUtils.getPrefLong(getActivity().getApplicationContext(), "TIME_0_DAYStoday", 0) != date.getDate()) {
showMemberTime("会员今天到期,请尽快续费",0);// 2019-1-15
}
SPUtils.setSettingLong(getActivity().getApplicationContext(), "TIME_0_DAYSNew", currentClickTime);
} else {
//判断是否是已经间隔了7天以后,再显示该dialog
long lastClickTime = SPUtils.getPrefLong(getActivity().getApplicationContext(), "TIME_7_DAYSNew", 0);
if (isSevenDayClick(lastClickTime)) {
} else {
if (SPUtils.getPrefLong(getActivity().getApplicationContext(), "TIME_7_DAYStoday", 0) != date.getDate()) {
showMemberTime("会员还有" + (date1.getDate() - date.getDate()) + "天到期,请尽快续费",(date1.getDate() - date.getDate()));// 2018-03-29
}
// long currentClickTime = System.currentTimeMillis();
SPUtils.setSettingLong(getActivity().getApplicationContext(), "TIME_7_DAYSNew", currentClickTime);
}
}
} else if (month == 1) {
if (SPUtils.getPrefLong(getActivity().getApplicationContext(), "TIME_7_DAYStoday", 0) != date.getDate()) {
showMemberTime("会员还有" + ((getCurrentMonthLastDay()- date.getDate())+date1.getDate()) + "天到期,请尽快续费",((getCurrentMonthLastDay()- date.getDate())+date1.getDate()) );// 2018-03-29
}
}
Log.e("====-=--",(getCurrentMonthLastDay()- date.getDate())+date1.getDate()+"");
} catch (ParseException e) {
e.printStackTrace();
}
/**
* 取得当月天数
* */
public int getCurrentMonthLastDay()
{
Calendar a = Calendar.getInstance();
a.set(Calendar.DATE, 1);//把日期设置为当月第一天
a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
private void showMemberTime( String time,int days) {
mDialogTime = new Dialog(getActivity());
mDialogTime.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialogTime.setCancelable(true);//点击返回不消失
mDialogTime.setCanceledOnTouchOutside(true);//点击外部不消失
Window window = mDialogTime.getWindow();
window.setContentView(R.layout.dialog_to_dai_li);
TextView tvContent = (TextView) window.findViewById(R.id.dialog_text);
TextView tvConfirm = window.findViewById(R.id.dialog_sure);
View tvCancel = window.findViewById(R.id.dialog_cancel);
tvContent.setText(time);
mDialogTime.show();
if(days == 0){
SPUtils.setSettingLong(getActivity().getApplicationContext(), "TIME_0_DAYStoday", new Date().getDate());
}else if(days == 1){
SPUtils.setSettingLong(getActivity().getApplicationContext(), "TIME_1_DAYStoday", new Date().getDate());
}else{
SPUtils.setSettingLong(getActivity().getApplicationContext(), "TIME_7_DAYStoday", new Date().getDate());
}
tvConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialogTime.dismiss();
}
});
tvCancel.setVisibility(View.GONE);
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialogTime.dismiss();
}
});
}
boolean isSevenDayClick(long lastClickTime) {
boolean flag = true;
long currentClickTime = System.currentTimeMillis();
if ((currentClickTime - lastClickTime) >= MIN_DELAY_TIME) {
flag = false;
}
return flag;
}
/**
* 获取两个日期的月数差
*
* @param fromDate
* @param toDate
* @return
*/
public long getDifferMonth(Date fromDate, Date toDate) {
Calendar fromDateCal = Calendar.getInstance();
Calendar toDateCal = Calendar.getInstance();
fromDateCal.setTime(fromDate);
toDateCal.setTime(toDate);
int fromYear = fromDateCal.get(Calendar.YEAR);
int toYear = toDateCal.get((Calendar.YEAR));
if (fromYear == toYear) {
return Math.abs(fromDateCal.get(Calendar.MONTH) - toDateCal.get(Calendar.MONTH));
} else {
int fromMonth = 12 - (fromDateCal.get(Calendar.MONTH) + 1);
int toMonth = toDateCal.get(Calendar.MONTH) + 1;
return Math.abs(toYear - fromYear - 1) * 12 + fromMonth + toMonth;
}
}