public static String getWeekCounterByEventStartDate(String event_s_date, String dailyDate) {
if (isSameWeek(event_s_date, dailyDate)) {
return "WK1";
}
Calendar c_base = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
Calendar tempC = Calendar.getInstance();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
int week = 0;
try {
c_base.setTime(df.parse(event_s_date));
c2.setTime(df.parse(dailyDate));
Long time1 = c2.getTimeInMillis();
Long time2 = c_base.getTimeInMillis();
Long dayDiffer = Math.abs(time1 - time2) / (1000 * 60 * 60 * 24);//毫秒*秒*分*小时
if (dayDiffer.intValue() == 0) {
week++;
return "WK" + week;
}
int curTodayWeek = c_base.get(Calendar.DAY_OF_WEEK) - 1;
int differIsWeek = 7 - curTodayWeek;
tempC = (Calendar) c_base.clone();
if (differIsWeek != 0) {
week++;
}
if (differIsWeek != 7) {
tempC.add(Calendar.DAY_OF_YEAR, differIsWeek);
}
time1 = c2.getTimeInMillis();
time2 = tempC.getTimeInMillis();
dayDiffer = Math.abs(time1 - time2) / (1000 * 60 * 60 * 24);//毫秒*秒*分*小时
if (dayDiffer.intValue() == 1) {//差一天
week++;
} else if (dayDiffer.intValue() < 7) {
week++;
} else {
DecimalFormat decimalFormat = new DecimalFormat("#.0");
String dayNum = decimalFormat.format((double) dayDiffer.intValue() / 7);
int day = Integer.parseInt(dayNum.substring(0, dayNum.indexOf(".")));
week += day;
int dec = Integer.parseInt(dayNum.substring(dayNum.indexOf(".") + 1, dayNum.length()));
if (dec != 0) {
week++;
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
return "";
}
return "WK" + week;
}
//第一种 isSameWeek
public static boolean isSameWeek(String date1, String date2) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(date1);
d2 = format.parse(date2);
} catch (Exception e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(d1);
cal2.setTime(d2);
int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
int subMonth = (cal1.get(Calendar.MONTH) + 1) - (cal2.get(Calendar.MONTH) + 1);
//同一年,同一月
if (subYear == 0 && subMonth == 0) {
int week1 = cal1.get(Calendar.WEEK_OF_MONTH);
int week2 = cal2.get(Calendar.WEEK_OF_MONTH);
int wd1 = cal1.get(Calendar.DAY_OF_WEEK);
int wd2 = cal2.get(Calendar.DAY_OF_WEEK);
if (wd1 == 1) {
week1 -= 1;
}
if (wd2 == 1) {
week2 -= 1;
}
return week1 == week2;
}
return false;
}
public static void main(String[] args) {
System.out.println(getWeekCounterByEventStartDate("2013-8-1", "2013-8-10"));
}
//第二种 isSameWeek
public static void main(String[] args) throws Exception {
DateFormat df = new SimpleDateFormat("yyyy-M-d");
System.out.println(isSameWeek(df.parse("2013-10-11"), df.parse("2013-10-12")));
System.out.println(isSameWeek(df.parse("2013-8-1"), df.parse("2013-8-5")));
}
public static boolean isSameWeek(Date d1, Date d2) {
SimpleDateFormat format = new SimpleDateFormat("yyyyww");//年周
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);//Calendar.DAY_OF_WEEK 是不被改变的
format.setCalendar(cal);
return format.format(d1).equals(format.format(d2));//format.format(d1) 年+一年的第几周字符串例如 2013-8-1 是一年的第31周
}