獲取指定日期所在一週的時間範圍(週一00:00:00---週日 23:59:59)
/**一週週期相關*/
public class MyWeekDate {
/**開始時間*/
public long mLong_begin;
/**開始日期*/
public String mStr_begin;
/**結束時間*/
public long mLong_end;
/**結束日期*/
public String mStr_end;
}
/**
* 獲取time所在日期一週的時間段(週一00:00:00---週日 23:59:59)
* @param time
* @return
*/
public static MyWeekDate getWeekDate(long time){
MyWeekDate weekDate = new MyWeekDate();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
SimpleDateFormat df = new SimpleDateFormat("MM月dd日");
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 獲取本週一的日期
//設定0點0分0秒
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
weekDate.mLong_begin = cal.getTimeInMillis();
weekDate.mStr_begin = df.format(cal.getTime());
//實際上Calendar.SUNDAY為本週第一天,在週一之前
//按照我們的習慣,週一為一週第一天,所以最後為下一週的星期天
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.add(Calendar.WEEK_OF_YEAR, 1);
//設定23:59:59
//Calendar.HOUR_OF_DAY--24小時制;Calendar.HOUR--12小時制
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.MILLISECOND, 999);
weekDate.mLong_end = cal.getTimeInMillis();
weekDate.mStr_end = df.format(cal.getTime());
return weekDate;
}
/**
* 判斷時間是否超出本週
* @param time
* @param myWeekDate
* @return
*/
public static boolean isOutOfWeek(long time,MyWeekDate myWeekDate){
if (time > myWeekDate.mLong_end || time < myWeekDate.mLong_begin)
return false;
return true;
}
---------------------