//跟据年月日获取周
public static int getWeek(String date) {
Calendar cal = Calendar.getInstance();//这一句必须要设置,否则美国认为第一天是周日,而我国认为是周一,对计算当期日期是第几周会有错误
cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置每周的第一天为星期一
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 每周从周一开始
cal.setMinimalDaysInFirstWeek(7);// 设置每周最少为7天
cal.setTime(getDate(date));
int week = cal.get(Calendar.WEEK_OF_YEAR);
return week;
}
跟据年 周 获取 时间
private String getDateFromWeek(int yearc,int week,int isleft) {
Calendar cal = Calendar.getInstance();//这一句必须要设置,否则美国认为第一天是周日,而我国认为是周一,对计算当期日期是第几周会有错误
cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置每周的第一天为星期一
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 每周从周一开始
cal.setMinimalDaysInFirstWeek(7);// 设置每周最少为7天
//跟据周获取 年月日
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (isleft ==0)
cal.setWeekDate(yearc, week,2);//周第一天
else cal.setWeekDate(yearc, week,1);//周的最后一天
}
int year = cal.get(Calendar.YEAR);
int month = (cal.get(Calendar.MONTH)) +1;
int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
return year +"-" + getMonthOrDay(month +"") +"-" + getMonthOrDay(day_of_month +"");
}
判断 2段日期是否有重复部分
//判断时间是否有交叉
public static Boolean checkTimesHasOverlap(Date dynaStartTime, Date dynaEndTime, Date fixedStartTime, Date fixedEndTime) {
if (dynaStartTime.getTime() <= fixedStartTime.getTime() && dynaEndTime.getTime() >= fixedStartTime.getTime()) {
return true;
}else if (dynaStartTime.getTime() >= fixedStartTime.getTime() && dynaStartTime.getTime() <= fixedEndTime.getTime()) {
return true;
}else {
return false;
}
}
网友评论