【 tulaoshi.com - 编程语言 】
直接上代码
代码如下:
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 日期工具类
* @author WXQ
*
*/
public class DateUtils {
private static final Log log = LogFactory.getLog(DateUtils.class);
/**
* 取得当前日期是多少周
*
* @param date
* @return
*/
public static int getWeekOfYear(Date date) {
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.MONDAY);
/**设置一年中第一个星期所需的最少天数,例如,如果定义第一个星期包含一年第一个月的第一天,则使用值 1 调用此方法。
* 如果最少天数必须是一整个星期,则使用值 7 调用此方法。 **/
c.setMinimalDaysInFirstWeek(1);
c.setTime(date);
return c.get(Calendar.WEEK_OF_YEAR);
}
/**
* 得到某一年周的总数
*
* @param year
* @return
*/
public static int getMaxWeekNumOfYear(int year) {
Calendar c = Calendar.getInstance();
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 = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.WEEK_OF_YEAR, week);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//设置周一
c.setFirstDayOfWeek(Calendar.MONDAY);
return c.getTime();
}
/**
* 得到某年某周的最后一天
*
* @param year
* @param week
* @return
*/
public static Date getLastDayOfWeek(int year, int week) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.WEEK_OF_YEAR, week);
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
return c.getTime();
}
/**
* 得到某年某月的第一天
*
* @param year
* @param month
* @return
*/
public static Date getFirstDayOfMonth(int year, int month) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month-1);
c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
return c.getTime();
}
/**
* 得到某年某月的最后一天
*
* @param year
* @param month
* @return
*/
public static Date getLastDayOfMonth(int year, int month) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month-1);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
return c.getTime();
}
/**
* 得到某年某季度第一天
*
* @param year
* @param quarter
* @return
*/
public static Date getFirstDayOfQuarter(int year, int quarter) {
int month = 0;
if (quarter 4) {
return null;
} else {
month = (quarter - 1) * 3 + 1;
}
return getFirstDayOfMonth(year, month);
}
/**
* 得到某年某季度最后一天
*
* @param year
* @param quarter
* @return
*/
public static Date getLastDayOfQuarter(int year, int quarter) {
int month = 0;
if (quarter 4) {
return null;
} else {
month = quarter * 3;
}
return getLastDayOfMonth(year, month);
}
/**
* 得到某年第一天
*
* @param year
* @return
*/
public static Date getFirstDayOfYear(int year) {
return getFirstDayOfQuarter(year,1);
}
/**
* 得到某年最后一天
*
* @param year
* @return
*/
public static Date getLastDayOfYear(int year) {
return getLastDayOfQuarter(year,4);
}
public static void main(String[] args) {
log.info(getFirstDayOfWeek(2013,1));
log.info(getLastDayOfWeek(2013,1));
log.info(getFirstDayOfMonth(2013,2));
log.info(getLastDayOfMonth(2013,2));
log.info(getFirstDayOfQuarter(2013,2));
log.info(getLastDayOfQuarter(2013,2));
log.info(getFirstDayOfYear(2013));
log.info(getLastDayOfYear(2013));
}
}