public static final String DATEFORMAT_PATTERN_THREE = "yyyy-MM-dd";
/**
* 根据时间返回对应的周 传入时间格式 yyyy-MM-dd
*
* @param day 2019-07-01 2019-07-31
* @return 2019-07-w1 2019-07-w5
*/
public static String transDay(String day){
String num = day.substring(8,10);
String flag = "w1";
switch (num){
case "01":
case "02":
case "03":
case "04":
case "05":
case "06":
case "07":
flag = "w1";
break;
case "08":
case "09":
case "10":
case "11":
case "12":
case "13":
case "14":
flag = "w2";
break;
case "15":
case "16":
case "17":
case "18":
case "19":
case "20":
case "21":
flag = "w3";
break;
case "22":
case "23":
case "24":
case "25":
case "26":
case "27":
case "28":
flag = "w4";
break;
case "29":
case "30":
case "31":
flag = "w5";
break;
}
return day.substring(0,8)+flag;
}
public static String getLastWeekStart(Date now) {
String startYear = "";
String startMonth = "";
String startDay = "";
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_PATTERN_THREE);
String date = sdf.format(now);
Calendar cl = Calendar.getInstance();
cl.setTime(now);
String week = transDay(date).substring(8,10);
switch (week){
case "w1" :
cl.add(Calendar.MONTH,-1);
if((cl.get(Calendar.MONTH)+1)==2) {
if(isLeapYear(cl.get(Calendar.YEAR))){
startDay = "29";
}else{
startDay = "22";
}
}else {
startDay = "29";
}
break;
case "w2" :
startDay = "01";
break;
case "w3" :
startDay = "08";
break;
case "w4" :
startDay = "15";
break;
case "w5" :
startDay = "22";
break;
}
startYear = cl.get(Calendar.YEAR)+""; //获取年
if((cl.get(Calendar.MONTH) + 1)<10){
startMonth = "0"+(cl.get(Calendar.MONTH) + 1); //获取月份,0表示1月份
}else{
startMonth = ""+(cl.get(Calendar.MONTH) + 1);
}
return startYear +"-" +startMonth + "-" + startDay + " 00:00:000" ;
}
public static String getLastWeekEnd(Date now) {
String endYear = "";
String endMonth = "";
String endDay = "";
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_PATTERN_THREE);
String date = sdf.format(now);
Calendar cl = Calendar.getInstance();
cl.setTime(now);
String week = transDay(date).substring(8,10);
switch (week){
case "w1" :
cl.add(Calendar.MONTH,-1);
endDay = cl.getActualMaximum(Calendar.DAY_OF_MONTH)+"";
break;
case "w2" :
endDay = "07";
break;
case "w3" :
endDay = "14";
break;
case "w4" :
endDay = "21";
break;
case "w5" :
endDay = "28";
break;
}
endYear = cl.get(Calendar.YEAR)+""; //获取年
if((cl.get(Calendar.MONTH) + 1)<10){
endMonth = "0"+(cl.get(Calendar.MONTH) + 1); //获取月份,0表示1月份
}else{
endMonth = ""+(cl.get(Calendar.MONTH) + 1);
}
return endYear +"-" +endMonth + "-" + endDay + " 23:59:999" ;
}
public static boolean isLeapYear(int year ){
if(year % 4 != 0 || year % 100 == 0 && year % 400 != 0) {
return false;
}else {
return true;
}
}
/**
* 根据开始日期 结束日期 获取相关周
* @param startTime
* @param endTime
* @return
*/
public static List<String> getListTransDay(String startTime,String endTime){
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_PATTERN_THREE);
List<String> result = new ArrayList<>();
try {
Date sDate = sdf.parse(startTime);
Date eDate = sdf.parse(endTime);
Calendar cls = Calendar.getInstance();
cls.setTime(sDate);
Calendar cle = Calendar.getInstance();
cle.setTime(eDate);
String tempTime = null;
String trans = null;
while(!(cls.get(Calendar.YEAR)+ ""+cls.get(Calendar.MONTH) +""+ cls.get(Calendar.DATE)+"").equals
(cle.get(Calendar.YEAR) + ""+cle.get(Calendar.MONTH) +""+(cle.get(Calendar.DATE)+1)+"")
)
{
tempTime = sdf.format(cls.getTime());
trans = transDay(tempTime);
result.add(trans);
cls.add(Calendar.DATE,1);
}
} catch (ParseException e) {
e.printStackTrace();
}
result = result.stream().distinct().collect(Collectors.toList());
return result;
}