Java获取一年有多少周,某周的第一天和最后一天

2022-08-09 教育 187阅读
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Test {

// 获取当前时间所在年的周数
public static int getWeekOfYear(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setMinimalDaysInFirstWeek(7);
c.setTime(date);

return c.get(Calendar.WEEK_OF_YEAR);
}

// 获取当前时间所在年的最大周数
public static int getMaxWeekNumOfYear(int year) {
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);

return getWeekOfYear(c.getTime());
}

// 获取某年的第几周的开始日期
public static Date getFirstDayOfWeek(int year, int week) {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);

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 = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);

Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);

return getLastDayOfWeek(cal.getTime());
}

// 获取当前时间所在周的开始日期
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();
}

public static void main(String[] args) {
int year = 2015;
int week = 27;

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date today = new Date();
Calendar c = new GregorianCalendar();
c.setTime(today);

System.out.println("当前时间:current date = " + sdf.format(today));
System.out.println("当前第几周:getWeekOfYear = " + getWeekOfYear(today));
System.out.println("年度总周数:getMaxWeekNumOfYear = " + getMaxWeekNumOfYear(year));
System.out.println("本周第一天:getFirstDayOfWeek = " + sdf.format(getFirstDayOfWeek(year, week)));
System.out.println("本周最后一天:getLastDayOfWeek = " + sdf.format(getLastDayOfWeek(year, week)));
System.out.println("本周第一天:getFirstDayOfWeek = " + sdf.format(getFirstDayOfWeek(today)));
System.out.println("本周最后一天:getLastDayOfWeek = " + sdf.format(getLastDayOfWeek(today)));
}
}

//输出结果:
// 当前时间:current date = 2015-07-07 16:35:07
// 当前第几周:getWeekOfYear = 27
// 年度总周数:getMaxWeekNumOfYear = 52
// getFirstDayOfWeek = 2015-07-06 16:35:07
// getLastDayOfWeek = 2015-07-12 16:35:07
// getFirstDayOfWeek = 2015-07-06 16:35:07
// getLastDayOfWeek = 2015-07-12 16:35:07
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com