靠!最近有这个需求,把我郁闷的要死!!花了好几天了还是没有满意的结果,看来还得研究JDK自己的东东啊,没有根基,哪来的大厦?
终于还是看到了
tag的文章,很感谢他!
shit!我真的费了好大的劲,结果写出的还是不尽如人意,唉!
http://blog.csdn.net/xdy3008/archive/2006/11/15/1385247.aspxJava中用Calendar类计算周和周的起始日期
在网上翻阅了好多资料,好多代码还是很有用的,但是不完整,不符合我目前的需要
而且文档也不够完整,因此自己就整理了一下。
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* <p>Title: 周计算类,星期一为一周的开始,星期日为一周的结束</p>
* <p>Description: 在两年的交接地带还有疑问。</p>
* <p>比如2006-12-29到2009-01-04,属于2008年的最后一周,</p>
* <p>2009-01-05位2009年第一周的开始。</p>
* <p>db2种的week_iso也是这样计算的</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>DateTime: 2006-4-11 23:36:39</p>
*
* @author gumpgz
* @version 1.0
*/
public class WeekUtil {
/**
* 取得当前日期是多少周
*
* @param date
* @return
*/
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);
}
/**
* 得到某一年周的总数
*
* @param year
* @return
*/
public static int getMaxWeekNumOfYear(int year) {
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
return getWeekOfYear(c.getTime());
}
/**
* 得到某年某周的第一天
*
* @param year
* @param week
* @return
*/
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 ());
}
/**
* 得到某年某周的最后一天
*
* @param year
* @param week
* @return
*/
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());
}
/**
* 取得当前日期所在周的第一天
*
* @param date
* @return
*/
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 ();
}
/**
* 取得当前日期所在周的最后一天
*
* @param date
* @return
*/
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;
// 以2006-01-02位例
Calendar c = new GregorianCalendar();
c.set(2006, Calendar.JANUARY, 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));
}
}