从网上找来的一篇仁兄的文章。。。借来引用下。放到了我的lib当中,描述了一些一周之内的日期查询的一些相关code。。
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class WeekUtil {
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 = 2006;
int week = 1;
Calendar c = new GregorianCalendar();
c.set(2007, Calendar.FEBRUARY, 2);
Date d = c.getTime();
System.out.println("current date = " + d);
System.out.println("getWeekOfYear = " + getWeekOfYear(d));
System.out.println("getMaxWeekNumOfYear = " + getMaxWeekNumOfYear(year));
System.out.println("getFirstDayOfWeek = " + getFirstDayOfWeek(year, week));
System.out.println("getLastDayOfWeek = " + getLastDayOfWeek(year, week));
System.out.println ("getFirstDayOfWeek = " + getFirstDayOfWeek(d));
System.out.println("getLastDayOfWeek = " + getLastDayOfWeek(d));
}
}
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class WeekUtil {
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 = 2006;
int week = 1;
Calendar c = new GregorianCalendar();
c.set(2007, Calendar.FEBRUARY, 2);
Date d = c.getTime();
System.out.println("current date = " + d);
System.out.println("getWeekOfYear = " + getWeekOfYear(d));
System.out.println("getMaxWeekNumOfYear = " + getMaxWeekNumOfYear(year));
System.out.println("getFirstDayOfWeek = " + getFirstDayOfWeek(year, week));
System.out.println("getLastDayOfWeek = " + getLastDayOfWeek(year, week));
System.out.println ("getFirstDayOfWeek = " + getFirstDayOfWeek(d));
System.out.println("getLastDayOfWeek = " + getLastDayOfWeek(d));
}
}