/**
* 某一个月第一天和最后一天
*
* @param date 指定日期
* @param pattern 日期格式
* @param isNeedHms是否需要时分秒
* @return
*/
public static Map getFirstLastDayByMonth(Date date,
String pattern, boolean isNeedHms) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Date theDate = calendar.getTime();
// 第一天
GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
gcLast.setTime(theDate);
gcLast.set(Calendar.DAY_OF_MONTH, 1);
String day_first = df.format(gcLast.getTime());
StringBuffer str = new StringBuffer().append(day_first);
if (isNeedHms)
str.append(" 00:00:00");
day_first = str.toString();
// 最后一天
calendar.add(Calendar.MONTH, 1); // 加一个月
calendar.set(Calendar.DATE, 1); // 设置为该月第一天
calendar.add(Calendar.DATE, -1); // 再减一天即为上个月最后一天
String day_last = df.format(calendar.getTime());
StringBuffer endStr = new StringBuffer().append(day_last);
if (isNeedHms)
endStr.append(" 23:59:59");
day_last = endStr.toString();
Map map = new HashMap();
map.put("first", day_first);
map.put("last", day_last);
return map;
}
/**
* 获取当前季度
*
*/
public static String getQuarter() {
Calendar c = Calendar.getInstance();
int month = c.get(c.MONTH) + 1;
int quarter = 0;
if (month >= 1 && month <= 3) {
quarter = 1;
} else if (month >= 4 && month <= 6) {
quarter = 2;
} else if (month >= 7 && month <= 9) {
quarter = 3;
} else {
quarter = 4;
}
return quarter + "";
}
/**
* 获取某季度的第一天和最后一天
*@param num第几季度
*/
public static String[] getCurrQuarter(int num) {
String[] s = new String[2];
String str = "";
// 设置本年的季
Calendar quarterCalendar = null;
switch (num) {
case 1: // 本年到现在经过了一个季度,在加上前4个季度
quarterCalendar = Calendar.getInstance();
quarterCalendar.set(Calendar.MONTH, 3);
quarterCalendar.set(Calendar.DATE, 1);
quarterCalendar.add(Calendar.DATE, -1);
str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd");
s[0] = str.substring(0, str.length() - 5) + "01-01";
s[1] = str;
break;
case 2: // 本年到现在经过了二个季度,在加上前三个季度
quarterCalendar = Calendar.getInstance();
quarterCalendar.set(Calendar.MONTH, 6);
quarterCalendar.set(Calendar.DATE, 1);
quarterCalendar.add(Calendar.DATE, -1);
str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd");
s[0] = str.substring(0, str.length() - 5) + "04-01";
s[1] = str;
break;
case 3:// 本年到现在经过了三个季度,在加上前二个季度
quarterCalendar = Calendar.getInstance();
quarterCalendar.set(Calendar.MONTH, 9);
quarterCalendar.set(Calendar.DATE, 1);
quarterCalendar.add(Calendar.DATE, -1);
str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd");
s[0] = str.substring(0, str.length() - 5) + "07-01";
s[1] = str;
break;
case 4:// 本年到现在经过了四个季度,在加上前一个季度
quarterCalendar = Calendar.getInstance();
str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd");
s[0] = str.substring(0, str.length() - 5) + "10-01";
s[1] = str.substring(0, str.length() - 5) + "12-31";
break;
}
return s;
}
/**
* 用途:以指定的格式格式化日期字符串
* @param pattern 字符串的格式
* @param currentDate 被格式化日期
* @return String 已格式化的日期字符串
* @throws NullPointerException 如果参数为空
*/
public static String formatDate(Date currentDate, String pattern){
if(currentDate == null || "".equals(pattern) || pattern == null){
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(currentDate);
}