如何计算今日,昨日,本周,上周,本月,上月

2021-04-12 14:33
276
0
Date d1=null; Date d2=null; Calendar c = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf_MM01 = new SimpleDateFormat("yyyy-MM-01"); //今日 d1=c.getTime(); d2=c.getTime(); //昨日 c.add(Calendar.DATE,-1); d1=c.getTime(); d2=c.getTime(); //本周 int i = c.get(Calendar.DAY_OF_WEEK); //国外是周末为每周第一天,周六为每周第7天,这里调整一下 if(i==1) { i=8; } c.add(Calendar.DATE,-i+2); d1=c.getTime(); d2=new Date(); //上周 int i = c.get(Calendar.DAY_OF_WEEK); //国外是周末为每周第一天,周六为每周第7天,这里调整一下 if(i==1) { i=8; } c.add(Calendar.DATE,-i+1); d2=c.getTime(); c.add(Calendar.DATE,-6); d1=c.getTime(); //本月 int i = c.get(Calendar.DAY_OF_MONTH); c.add(Calendar.DATE,-i+1); d1=c.getTime(); d2=new Date(); //上月 int i = c.get(Calendar.DAY_OF_MONTH); c.add(Calendar.DATE,-i); d2=c.getTime(); String d1Str = sdf_MM01.format(d2); try { d1 = sdf_MM01.parse(d1Str); } catch (Exception e) { } String fromDate=sdf.format(d1)+" 00:00:00"; String toDate=sdf.format(d2)+" 23:59:59";

全部评论