先放一个错误的例子。。。
虽然结果是对的,但不推荐这么写,也是自己辛苦敲出来的,就放出来记录一下
String yesterday =DateTime.now().day-1==0||DateTime.now().month-1==0?
DateTime.now().day-1==0?
DateTime.now().month-1==0?
DateTime.now().year.toString()+"."+"12"+"."+"31":
DateTime.now().month-1==0?
DateTime.now().year.toString()+"."+DateTime.now().month.toString()+"."+(DateTime.now().day-1).toString():
DateTime.now().year.toString()+"."+"12"+"."+(DateTime.now().day).toString():
DateTime.now().year.toString()+"."+DateTime.now().month.toString()+"."+(DateTime.now().day-1).toString():
DateTime.now().year.toString()+"."+(DateTime.now().month-1).toString()+"."+(DateTime.now().day-1).toString();
刚开始不知道DateTime提供了返回的方法,就自己写了一大堆(全部三木运算符);
在网上偶然看到一篇博客,发现了人本来就提供了方法,为啥还要自己写呢?!!!!
不废话了,直接把整个类展示出来吧,也没啥原理,直接上代码
String calendarBack(int pos){
String today =DateTime.now().year.toString()+"."+DateTime.now().month.toString()+"."+DateTime.now().day.toString();//2021.8.24 获取今天的日期
String year=today.substring(0,4);//2021 今年
String month=today.substring(5,7);//08 这个月
// String day=today.substring(8,10);//24 今天
int nowyear= DateTime.now().year;//今年
int nowmonth= DateTime.now().month;//这个月是几月
int nowday= DateTime.now().day;//今天
int weekday=DateTime.now().weekday;//今天是本周的第几天
String weekstart=new DateTime(nowyear, nowmonth, nowday-weekday+1).toString();//本周一的时间
String weekend=new DateTime(nowyear, nowmonth, nowday-weekday+7).toString();//本周日的时间
String lastweekstart=new DateTime(nowyear, nowmonth, nowday-weekday-8).toString();//本周一的时间
String lastweekend=new DateTime(nowyear, nowmonth, nowday-weekday).toString();//本周日的时间
if(pos==0){//如果等于0 则返回今天日期
return DateTime.now().year.toString()+"."+DateTime.now().month.toString()+"."+DateTime.now().day.toString()+"-"+DateTime.now().year.toString()+"."+DateTime.now().month.toString()+"."+DateTime.now().day.toString();
}else if(pos==1){//如果等于1 则返回昨天日期
return (DateTime(nowyear,nowmonth,nowday-1).year).toString()+"."+(DateTime(nowyear,nowmonth,nowday-1).month).toString()+"."+(DateTime(nowyear,nowmonth,nowday-1).day).toString()+"-"+(DateTime(nowyear,nowmonth,nowday-1).year).toString()+"."+(DateTime(nowyear,nowmonth,nowday-1).month).toString()+"."+(DateTime(nowyear,nowmonth-1,nowday-1).day).toString();
}else if(pos==2){//如果等于2 则返回本周日期
return weekstart.substring(0,4)+"."+weekstart.substring(5,7)+"."+weekstart.substring(8,10)+"-"+weekend.substring(0,4)+"."+weekend.substring(5,7)+"."+weekend.substring(8,10);
}else if(pos==3){//如果等于3 则返回上周日期
return lastweekstart.substring(0,4)+"."+lastweekstart.substring(5,7)+"."+lastweekstart.substring(8,10)+"-"+lastweekend.substring(0,4)+"."+lastweekend.substring(5,7)+"."+lastweekend.substring(8,10);
}else if(pos==4){//如果等于4则返回本月日期
return year+"."+month+"1"+"-"+year+"."+month+DateTime(2021,nowmonth+1,0).day.toString();
}else if(pos==5){//如果等于5 则返回上月日期
return (DateTime(nowyear,nowmonth-1,nowday).year).toString()+"."+(DateTime(nowyear,nowmonth-1,nowday).month).toString()+"."+"1"+"-"+(DateTime(nowyear,nowmonth-1,nowday).year).toString()+"."+(DateTime(nowyear,nowmonth-1,nowday).month).toString()+"."+(DateTime(nowyear,nowmonth,0).day).toString();
}
}
可以放在DateUtil里面 直接copy就行