欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java得到某年某周的第一天实现思路及代码

程序员文章站 2023-11-12 23:31:28
直接上代码 复制代码 代码如下: import java.util.calendar; import java.util.date; import org.apache.c...
直接上代码
复制代码 代码如下:

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));

}
}