Java 日期 周 年



/**
* java日期操作(月末、周末等的日期操作)
*
* @author
*
*/
public class DateUtil {

/** */
/**
* 取得某天相加(减)後的那一天
*
* @param date
* @param num
* (可正可负)
* @return
*/
public static Date getAnotherDate(Date date, int num) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DAY_OF_YEAR, num);
return c.getTime();
}

/** */
/**
* 取得某月的的最后一天
*
* @param year
* @param month
* @return
*/
public static Date getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);// 年
cal.set(Calendar.MONTH, month - 1);// 月,因为Calendar里的月是从0开始,所以要减1
cal.set(Calendar.DATE, 1);// 日,设为一号
cal.add(Calendar.MONTH, 1);// 月份加一,得到下个月的一号
cal.add(Calendar.DATE, -1);// 下一个月减一为本月最后一天
return cal.getTime();// 获得月末是几号
}

/** */
/**
* 取得某天是一年中的多少周
*
* @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 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());
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);
return c.getTime();
}

/** */
/**
* 取得某一年共有多少周
*
* @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());
}
/**
*
* 获取某一年某一周的日期
* @description
* @param year
* @param week
* @return
*/
public static List<String> getWeekDays(int year,int week){
List<String> list = new ArrayList<String>();

Date date = getFirstDayOfWeek(year,week);
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");

for (int i = 0; i < 7; i++) {
list.add(d.format(date));
System.out.println(d.format(date));

date.setDate(date.getDate()+1);
}
return list;
}
/** */
/**
* 取得某年某周的第一天 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-05为2009年第一周的第一天
*
* @param year
* @param week
* @return
*/
public static Date getFirstDayOfWeek(int year, int week) {
Calendar calFirst = Calendar.getInstance();
calFirst.set(year, 0, 7);
Date firstDate = getFirstDayOfWeek(calFirst.getTime());

Calendar firstDateCal = Calendar.getInstance();
firstDateCal.setTime(firstDate);

Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));

Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, (week - 1) * 7);
firstDate = getFirstDayOfWeek(cal.getTime());

return firstDate;
}

/** */
/**
* 取得某年某周的最后一天 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周, 2009-01-04为
* 2008年最后一周的最后一天
*
* @param year
* @param week
* @return
*/
public static Date getLastDayOfWeek(int year, int week) {
Calendar calLast = Calendar.getInstance();
calLast.set(year, 0, 7);
Date firstDate = getLastDayOfWeek(calLast.getTime());

Calendar firstDateCal = Calendar.getInstance();
firstDateCal.setTime(firstDate);

Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));

Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, (week - 1) * 7);
Date lastDate = getLastDayOfWeek(cal.getTime());

return lastDate;
}

/**
*获取当前日期的年、月、日
*/
public void display() {
Calendar cal = Calendar.getInstance();
// 年
int year = cal.get(cal.YEAR);
// 月
int month = cal.get(cal.MONTH) + 1;
// 日
int date = cal.get(cal.DATE);
// 星期
int today = cal.get(cal.DAY_OF_WEEK) - 1;
}


public static void main(String args[]){

List<String> listWeekDate = DateUtil.getWeekDays(2010,52);

for(String weeks :listWeekDate){
System.out.println("weeks:"+weeks);
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值