import java.util.Calendar; import java.util.Date; import java.util.TimeZone; public class DateUtil { /*根据年份和星期的序号,获取该星期从哪一天到哪一天*/ public static String getWeekGap(int year, int weekNum) { Calendar c = Calendar.getInstance(); c.set(year, 0, 01); // 月份是从零开始 int weekDay = c.get(Calendar.DAY_OF_WEEK) == 1 ? 8 : c .get(Calendar.DAY_OF_WEEK); c.add(Calendar.DATE, 7 * weekNum); c.add(Calendar.DATE, Calendar.MONDAY - weekDay); Date start = c.getTime(); c.add(Calendar.DATE, 6); Date end = c.getTime(); String datestr = String.format("%1$tY-%1$tm-%1$td", start) + "至" + String.format("%1$tY-%1$tm-%1$td", end); return datestr; } /*获取当前星期从哪一天到哪一天*/ public static String getWeekGap_Default() { Calendar c = Calendar.getInstance(); c.getTime(); int weekDay = c.get(Calendar.DAY_OF_WEEK) == 1 ? 8 : c .get(Calendar.DAY_OF_WEEK); c.add(Calendar.DATE, Calendar.MONDAY - weekDay); Date start = c.getTime(); c.add(Calendar.DATE, 6); Date end = c.getTime(); String datestr = String.format("%1$tY-%1$tm-%1$td", start) + "至" + String.format("%1$tY-%1$tm-%1$td", end); return datestr; } /*获取当前星期是一年中第几个星期*/ public static int getWeekNumOfYear(){ TimeZone zone=TimeZone.getTimeZone("Asia/Shanghai"); Calendar cal = Calendar.getInstance(zone); int c = cal.get(Calendar.WEEK_OF_YEAR); System.out.println(c); return c-1; } public static int getYear(){ Calendar ca = Calendar.getInstance(); ca.setTime(new java.util.Date()); return ca.get(Calendar.YEAR); } }