Javaは、ある月、ある四半期の初日と最終日を取得します.

3753 ワード

/**
 *             
 * 
 * @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);
}