日期处理

package janson.calendar;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

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

/**
* 取得某年某周的第一天 
* 对于交叉: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;
}

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

}

}

你可能感兴趣的:(日期)