按照公司某项目的需求,定义周模式:每周从周一开始,至少四天在当年的才算第一周 。
定义周实体类:
public class Week {
private String weekNum;
private int beginYear;
private int endYear;
// 周开始日期 格式: MM/dd
private String weekBegin;
// 周结束日期 格式: MM/dd
private String weekEnd;
/**
* 获取周开始日期 (yyyy/MM/dd)
* @return
*/
public Date getWeekBeginDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
String strDate = beginYear + "/" + weekBegin;
Date date = new Date();
try {
date = formatter.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 获取周结束日期 (yyyy/MM/dd)
* @return
*/
public Date getWeekEndDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
String strDate = endYear + "/" + weekEnd;
Date date = new Date();
try {
date = formatter.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public String getWeekNum() {
return weekNum;
}
public void setWeekNum(String weekNum) {
this.weekNum = weekNum;
}
public int getBeginYear() {
return beginYear;
}
public void setBeginYear(int beginYear) {
this.beginYear = beginYear;
}
public int getEndYear() {
return endYear;
}
public void setEndYear(int endYear) {
this.endYear = endYear;
}
public String getWeekBegin() {
return weekBegin;
}
public void setWeekBegin(String weekBegin) {
this.weekBegin = weekBegin;
}
public String getWeekEnd() {
return weekEnd;
}
public void setWeekEnd(String weekEnd) {
this.weekEnd = weekEnd;
}
@Override
public String toString() {
return beginYear + "/" + weekBegin + " ~ " + endYear + "/" + weekEnd + "(第" + weekNum + "周)";
}
}
周模式帮助类WeekHelper:
public class WeekHelper {
private static SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
/**
* 获取某一年所有周
* @param currentYear
* @return
*/
public static List<Week> getWeeksByYear(int currentYear) {
ArrayList<Week> weekList = new ArrayList<>();
Date lastDay = DateUtil.getLastDayOfYear(currentYear);
boolean result = DateUtil.isLastDayInYearLastWeek(lastDay);
// 一年最多有53个星期
for(int i = 0;i < 53 ; i++){
Date firstDayOfWeek = DateUtil.getFirstDayOfWeek(currentYear, i);
Date lastDayOfWeek = DateUtil.getLastDayOfWeek(currentYear, i);
Calendar startCal = Calendar.getInstance();
startCal.setTime(firstDayOfWeek);
Calendar endCal = Calendar.getInstance();
endCal.setTime(lastDayOfWeek);
String WeekNumStr = String.valueOf(i + 1);
if (i < 9) {
WeekNumStr = "0" + (i + 1);
}
Week everyWeek = new Week();
everyWeek.setBeginYear(startCal.get(Calendar.YEAR));
everyWeek.setEndYear(endCal.get(Calendar.YEAR));
everyWeek.setWeekBegin(sdf.format(startCal.getTime()));
everyWeek.setWeekEnd(sdf.format(endCal.getTime()));
everyWeek.setWeekNum(WeekNumStr);
if(i <= 50){
weekList.add(everyWeek);
}
if(i > 50){
// 判断最后一天是否在这区间内
if(DateUtil.isEffectiveDate(lastDay,firstDayOfWeek,lastDayOfWeek)){
// 判断该年的最后一天(xxxx年12月31日)所在的周是不是当年的最后一周
if(result){
weekList.add(everyWeek);
}
break;
}else{
weekList.add(everyWeek);
}
}
}
return weekList;
}
/**
* 获取当前时间所在的年份
* @param nowCal
* @return
*/
public static int getSelectYear(Calendar nowCal){
int currentYear = nowCal.get(Calendar.YEAR);
int currentYear1 = currentYear + 1;
int currentYear2 = currentYear - 1;
Date[] resultDate = getYearMinDayAndMasDay(currentYear);
if(DateUtil.isEffectiveDate(nowCal.getTime(),resultDate[0],resultDate[1])){
return currentYear;
}else{
resultDate = getYearMinDayAndMasDay(currentYear1);
if(DateUtil.isEffectiveDate(nowCal.getTime(),resultDate[0],resultDate[1])){
return currentYear1;
}else{
resultDate = getYearMinDayAndMasDay(currentYear2);
if(DateUtil.isEffectiveDate(nowCal.getTime(),resultDate[0],resultDate[1])){
return currentYear2;
}
}
}
return 0;
}
/**
* 获取一年第一周的第一天 和 最后一周的最后一天
* @param currentYear
* @return
*/
public static Date[] getYearMinDayAndMasDay(int currentYear){
Date[] resultDate = new Date[2];
List<Week> weeks = getWeeksByYear(currentYear);
Week week0 = weeks.get(0);
Week week1 = weeks.get(weeks.size()-1);
Date minDate = week0.getWeekBeginDate();
Date maxDate = week1.getWeekEndDate();
resultDate[0] = minDate;
resultDate[1] = maxDate;
return resultDate;
}
/**
* 根据当前日期算出所在周的开始时间和结束时间
* @param currentDate
* @return
*/
public static String[] getWeekStartAndEndDay(Date currentDate) {
String[] weekDay = new String[2];
Date firstDayOfWeek = DateUtil.getFirstDayOfWeek(currentDate);
Date lastDayOfWeek = DateUtil.getLastDayOfWeek(currentDate);
weekDay[0] = sdf2.format(firstDayOfWeek.getTime());
weekDay[1] = sdf2.format(lastDayOfWeek.getTime());
return weekDay;
}
}
日期帮助类DateUtil :
public class DateUtil {
public static final long SEC_MILL_SECONDS = 1000;
public static final long MINUTE_MILL_SECONDS = 60 * SEC_MILL_SECONDS;
public static final long HOUR_MILL_SECONDS = 60 * MINUTE_MILL_SECONDS;
public static final long DAY_MILL_SECONDS = 24 * HOUR_MILL_SECONDS;
// 获取某年的第几周的开始日期
public static Date getFirstDayOfWeek(int year, int week) {
Calendar c = getFirstDayOfWeek(year);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getFirstDayOfWeek(cal.getTime());
}
// 获取某年的第几周的结束日期
public static Date getLastDayOfWeek(int year, int week) {
Calendar c = getFirstDayOfWeek(year);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getLastDayOfWeek(cal.getTime());
}
/** 获取一年开始的第一周的周一
* 第一周如果有四天或以上就算是该年的第一周,
* 否则就是上一年的最后一周(用于跨年判断第一周开始时间)
* @param year
* @return
*/
public static Calendar getFirstDayOfWeek(int year){
Calendar c = new GregorianCalendar();
c.set(year, Calendar.JANUARY, 1, 0, 0, 0);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
// 如果1月1日是周五,周六或者周日,那么这一周是上一年最后一周,重新计算今年的第一周开始的周一
if(dayOfWeek == Calendar.FRIDAY || dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY){
while(c.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY){
c.add(Calendar.DAY_OF_YEAR, 1);
}
}
return c;
}
// 获取当前时间所在周的开始日期
public static Date getFirstDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
return c.getTime();
}
// 获取当前时间所在周的结束日期
public static Date getLastDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
return c.getTime();
}
/**
* 判断该年的最后一天(xxxx年12月31日)所在的周是不是当年的最后一周,
* 否则就是下一年的第一周
*/
public static boolean isLastDayInYearLastWeek(Date lastDate){
boolean result = false;
Calendar c = new GregorianCalendar();
c.setTime(lastDate);
int lastDay = c.get(Calendar.DAY_OF_WEEK);
// 当lastDay是周四、五、六、日, 它所在周就是是该年的最后一周
if(lastDay == Calendar.THURSDAY || lastDay == Calendar.FRIDAY || lastDay == Calendar.SATURDAY || lastDay == Calendar.SUNDAY){
result = true;
}
return result;
}
// 获取一年的最后一天
public static Date getLastDayOfYear(int year) {
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 0, 0, 0);
return c.getTime();
}
/**
* 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
*
* @param nowTime 当前时间
* @param startTime 开始时间
* @param endTime 结束时间
* @return
*/
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if(Math.abs(nowTime.getTime() - startTime.getTime()) < 1000 ||
Math.abs(nowTime.getTime() - endTime.getTime()) < 1000){
return true;
}
Calendar date = new GregorianCalendar();
date.setTime(nowTime);
Calendar begin = new GregorianCalendar();
begin.setTime(startTime);
Calendar end = new GregorianCalendar();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
}
Main方法入口:
public class Main {
public static void main(String[] args) {
int currentYear = 2019;
List<Week> weeks = WeekHelper.getWeeksByYear(currentYear);
System.out.println("当前选择的年份:" + currentYear);
for(Week week:weeks){
System.out.println(week.toString());
}
}
}
打印的结果如下:
当前选择的年份:2019
2018/12/31 ~ 2019/01/06(第01周)
2019/01/07 ~ 2019/01/13(第02周)
2019/01/14 ~ 2019/01/20(第03周)
2019/01/21 ~ 2019/01/27(第04周)
2019/01/28 ~ 2019/02/03(第05周)
2019/02/04 ~ 2019/02/10(第06周)
2019/02/11 ~ 2019/02/17(第07周)
2019/02/18 ~ 2019/02/24(第08周)
2019/02/25 ~ 2019/03/03(第09周)
2019/03/04 ~ 2019/03/10(第10周)
2019/03/11 ~ 2019/03/17(第11周)
2019/03/18 ~ 2019/03/24(第12周)
2019/03/25 ~ 2019/03/31(第13周)
2019/04/01 ~ 2019/04/07(第14周)
2019/04/08 ~ 2019/04/14(第15周)
2019/04/15 ~ 2019/04/21(第16周)
2019/04/22 ~ 2019/04/28(第17周)
2019/04/29 ~ 2019/05/05(第18周)
2019/05/06 ~ 2019/05/12(第19周)
2019/05/13 ~ 2019/05/19(第20周)
2019/05/20 ~ 2019/05/26(第21周)
2019/05/27 ~ 2019/06/02(第22周)
2019/06/03 ~ 2019/06/09(第23周)
2019/06/10 ~ 2019/06/16(第24周)
2019/06/17 ~ 2019/06/23(第25周)
2019/06/24 ~ 2019/06/30(第26周)
2019/07/01 ~ 2019/07/07(第27周)
2019/07/08 ~ 2019/07/14(第28周)
2019/07/15 ~ 2019/07/21(第29周)
2019/07/22 ~ 2019/07/28(第30周)
2019/07/29 ~ 2019/08/04(第31周)
2019/08/05 ~ 2019/08/11(第32周)
2019/08/12 ~ 2019/08/18(第33周)
2019/08/19 ~ 2019/08/25(第34周)
2019/08/26 ~ 2019/09/01(第35周)
2019/09/02 ~ 2019/09/08(第36周)
2019/09/09 ~ 2019/09/15(第37周)
2019/09/16 ~ 2019/09/22(第38周)
2019/09/23 ~ 2019/09/29(第39周)
2019/09/30 ~ 2019/10/06(第40周)
2019/10/07 ~ 2019/10/13(第41周)
2019/10/14 ~ 2019/10/20(第42周)
2019/10/21 ~ 2019/10/27(第43周)
2019/10/28 ~ 2019/11/03(第44周)
2019/11/04 ~ 2019/11/10(第45周)
2019/11/11 ~ 2019/11/17(第46周)
2019/11/18 ~ 2019/11/24(第47周)
2019/11/25 ~ 2019/12/01(第48周)
2019/12/02 ~ 2019/12/08(第49周)
2019/12/09 ~ 2019/12/15(第50周)
2019/12/16 ~ 2019/12/22(第51周)
2019/12/23 ~ 2019/12/29(第52周)