/**
* 拓展日期工具类
* @author hejianwei
*
*/
public class MOEDateUtils {
/**
* 获取某周得第一天
* @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.WEEK_OF_YEAR, week);
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return c.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.WEEK_OF_YEAR, week);
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
c.set(Calendar.HOUR, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
return c.getTime();
}
/**
* 获取某月得第一天
* @param year 年
* @param month 月
* @return
*/
public static Date getFirstDayOfMonth(int year, int month){
Calendar c = new GregorianCalendar();
c.set(year, month, 1);
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return c.getTime();
}
/**
* 获取某月得最后一天
* @param year 年
* @param month 月
* @return
*/
public static Date getLastDayOfMonth(int year, int month){
Calendar c = new GregorianCalendar();
c.clear();
c.set(Calendar.MONTH, month);
int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(year, month, lastDay);
// c.roll(Calendar.DATE, -1);
// c.add(Calendar.DATE, -1);
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return c.getTime();
}
/**
* 获取某季度得第一天
* @param year 年
* @param quarter 季度
* @return
*/
public static Date getFirstDayOfQuarter(int year, int quarter){
Calendar c = new GregorianCalendar();
int month = 0;
switch (quarter) {
case 1:
month = 1-1;
break;
case 2:
month = 4-1;
break;
case 3:
month = 7-1;
break;
case 4:
month = 10-1;
break;
default:
month = c.get(Calendar.MONTH);
break;
}
return getFirstDayOfMonth(year, month);
}
/**
* 获取某季度得最一天
* @param year 年
* @param quarter 季度
* @return
*/
public static Date getLastDayOfQuarter(int year, int quarter){
Calendar c = new GregorianCalendar();
int month = 0;
switch (quarter) {
case 1:
month = 3-1;
break;
case 2:
month = 6-1;
break;
case 3:
month = 9-1;
break;
case 4:
month = 12-1;
break;
default:
month = c.get(Calendar.MONTH);
break;
}
return getLastDayOfMonth(year, month);
}
/**
* 获取某年得第一天
* @param year 年
* @return
*/
public static Date getFirstDayOfYear(int year){
return getFirstDayOfMonth(year, 0);
}
/**
* 获取某年得最后一天
* @param year 年
* @return
*/
public static Date getLastDayOfYear(int year){
return getLastDayOfMonth(year, 11);
}
public static void main(String[] args) {
// System.out.println("周一: " + DateUtils.formatDate(getFirstDayOfWeek(2016, 25)));
//
// System.out.println("周末: " + DateUtils.formatDate(getLastDayOfWeek(2016, 25)));
System.out.println("月初:" + DateUtils.formatDate(getFirstDayOfMonth(2016, 6)));
System.out.println("月末:" + DateUtils.formatDate(getLastDayOfMonth(2016, 8)));
// System.out.println("季初:" + DateUtils.formatDate(getFirstDayOfQuarter(2016, 1)));
//
// System.out.println("季末:" + DateUtils.formatDate(getLastDayOfQuarter(2016, 1)));
//
// System.out.println("年初:" + DateUtils.formatDate(getFirstDayOfYear(2016)));
//
// System.out.println("年末:" + DateUtils.formatDate(getLastDayOfYear(2016)));
}
* 拓展日期工具类
* @author hejianwei
*
*/
public class MOEDateUtils {
/**
* 获取某周得第一天
* @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.WEEK_OF_YEAR, week);
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return c.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.WEEK_OF_YEAR, week);
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
c.set(Calendar.HOUR, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
return c.getTime();
}
/**
* 获取某月得第一天
* @param year 年
* @param month 月
* @return
*/
public static Date getFirstDayOfMonth(int year, int month){
Calendar c = new GregorianCalendar();
c.set(year, month, 1);
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return c.getTime();
}
/**
* 获取某月得最后一天
* @param year 年
* @param month 月
* @return
*/
public static Date getLastDayOfMonth(int year, int month){
Calendar c = new GregorianCalendar();
c.clear();
c.set(Calendar.MONTH, month);
int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(year, month, lastDay);
// c.roll(Calendar.DATE, -1);
// c.add(Calendar.DATE, -1);
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return c.getTime();
}
/**
* 获取某季度得第一天
* @param year 年
* @param quarter 季度
* @return
*/
public static Date getFirstDayOfQuarter(int year, int quarter){
Calendar c = new GregorianCalendar();
int month = 0;
switch (quarter) {
case 1:
month = 1-1;
break;
case 2:
month = 4-1;
break;
case 3:
month = 7-1;
break;
case 4:
month = 10-1;
break;
default:
month = c.get(Calendar.MONTH);
break;
}
return getFirstDayOfMonth(year, month);
}
/**
* 获取某季度得最一天
* @param year 年
* @param quarter 季度
* @return
*/
public static Date getLastDayOfQuarter(int year, int quarter){
Calendar c = new GregorianCalendar();
int month = 0;
switch (quarter) {
case 1:
month = 3-1;
break;
case 2:
month = 6-1;
break;
case 3:
month = 9-1;
break;
case 4:
month = 12-1;
break;
default:
month = c.get(Calendar.MONTH);
break;
}
return getLastDayOfMonth(year, month);
}
/**
* 获取某年得第一天
* @param year 年
* @return
*/
public static Date getFirstDayOfYear(int year){
return getFirstDayOfMonth(year, 0);
}
/**
* 获取某年得最后一天
* @param year 年
* @return
*/
public static Date getLastDayOfYear(int year){
return getLastDayOfMonth(year, 11);
}
public static void main(String[] args) {
// System.out.println("周一: " + DateUtils.formatDate(getFirstDayOfWeek(2016, 25)));
//
// System.out.println("周末: " + DateUtils.formatDate(getLastDayOfWeek(2016, 25)));
System.out.println("月初:" + DateUtils.formatDate(getFirstDayOfMonth(2016, 6)));
System.out.println("月末:" + DateUtils.formatDate(getLastDayOfMonth(2016, 8)));
// System.out.println("季初:" + DateUtils.formatDate(getFirstDayOfQuarter(2016, 1)));
//
// System.out.println("季末:" + DateUtils.formatDate(getLastDayOfQuarter(2016, 1)));
//
// System.out.println("年初:" + DateUtils.formatDate(getFirstDayOfYear(2016)));
//
// System.out.println("年末:" + DateUtils.formatDate(getLastDayOfYear(2016)));
}