자바 처리 날짜 도구 클래스 (1)

28512 단어 도구 클래스

package util;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.SimpleTimeZone;

import org.apache.commons.lang.StringUtils;



/**
 *   :       
 * 
 * @author tongbiao
 */

public class DateUtils {
	public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM");
	public static SimpleDateFormat dateFormatDB = new SimpleDateFormat("yyyyMMdd");//           
	public static SimpleDateFormat dataTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final String Y_M_D = "yyyy-MM-dd";
	public static final String Y_M_D_HM = "yyyy-MM-dd HH:mm";
	public static final String Y_M_D_HMS = "yyyy-MM-dd HH:mm:ss";
	public static final String YMD = "yyyyMMdd";
	public static final String YMDHM = "yyyyMMddHHmm";
	public static final String YMDHMS = "yyyyMMddHHmmss";
	public static final String ymd = "yyyy/MM/dd";
	public static final String ymd_HM = "yyyy/MM/dd HH:mm";
	public static final String ymd_HMS = "yyyy/MM/dd HH:mm:ss";
	
	/**
	 *         
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static String getSubTwoTime(String endTime,String startTime){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	 
		try{
			
			Date d1 = df.parse(startTime);
			Date d2 = df.parse(endTime);
			long diff = d1.getTime() - d2.getTime();//            
			long days = diff / (1000 * 60 * 60 * 24);
			
			long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
			long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
//	  System.out.println(""+days+" "+hours+"  "+minutes+" ");
			if(hours <0){
				hours = new BigDecimal(hours).abs().intValue();
			}
			if(minutes <0){
				minutes = new BigDecimal(minutes).abs().intValue();
			}
			return ""+days+"-"+hours+"-"+minutes;
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	/**
	 *         
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static String getSubTwoTimeYY(String endTime,String startTime){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		
		try{
			Date d1 = df.parse(startTime);
			Date d2 = df.parse(endTime);
			long diff = d1.getTime() - d2.getTime();//            
			long days = diff / (1000 * 60 * 60 * 24);
			return ""+days;
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	/**
	 *         
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static String getSubTwoTime1(String endTime,String startTime){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		try{
			
			Date d1 = df.parse(startTime);
			Date d2 = df.parse(endTime);
			long diff = d2.getTime() - d1.getTime();//            
			long days = diff / (1000 * 60 * 60 * 24);
			
			long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
			long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
//	  System.out.println(""+days+" "+hours+"  "+minutes+" ");
			if(hours <0){
				hours = new BigDecimal(hours).abs().intValue();
			}
			if(minutes <0){
				minutes = new BigDecimal(minutes).abs().intValue();
			}
			return ""+days+"-"+hours+"-"+minutes;
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	/**
	 * 
	 * @param dateTime yyyy-MM-dd HH:mm:ss
	 * @return unix   
	 */
	public static String getUnixTimeStamp(String dateTime){
		
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(dataTimeFormat.parse(dateTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return (c.getTimeInMillis()/1000)+"";
	}
	/**
	 * unix      
	 * @param timestampString  1252639886
	 * @param formats
	 * @return
	 */
	public static String gerUnixTime2String(String timestampString, String formats){   
		
		 if(StringUtils.isBlank(timestampString) || "null".equals(timestampString)){
			 return "";
		 }
		  Long timestamp = Long.parseLong(timestampString)*1000;    
		  String date = new java.text.SimpleDateFormat(formats).format(new java.util.Date(timestamp));    
		  return date;    
	}  
	/**
	 * 
	 * @param dateTime yyyy-MM-dd HH:mm:ss
	 * @return unix   
	 */
	public static String getCurrentUnixTimeStamp(){
		
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		return (c.getTimeInMillis()/1000)+"";
	}
	public static String formatDateTime(Date date) {
		return dataTimeFormat.format(date);
	}

	/**
	 *     "yyyy-MM-dd"        
	 * @return "yyyy-MM-dd"        
	 */
	private static SimpleDateFormat newLongYMDFormat() {
		return new SimpleDateFormat("yyyy-MM-dd");
	}

	/**
	 *     "yyyy-MM-dd HH:mm:ss"        
	 * @return "yyyy-MM-dd HH:mm:ss"        
	 */
	private static SimpleDateFormat newLongYMDHMSFormat() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * "yyyyMMddHHmmss"        "yyyy-MM-dd HH:mm:ss"     
	 * @param shortYMDHMS "yyyyMMddHHmmss"     
	 * @return "yyyy-MM-dd HH:mm:ss"     
	 * @throws ParseException
	 */
	public static String toLongYMDHMS(String shortYMDHMS) throws ParseException {
		return newLongYMDHMSFormat().format(newShortYMDHMSFormat().parse(shortYMDHMS));
	}

	/**
	 *   "yyyy-MM-dd"       
	 * @return   "yyyy-MM-dd"       
	 */
	public static String getLongYMD() {
		return newLongYMDFormat().format(new Date());
	}
	/**
	 * 2015 12 21 
	 * @return
	 */
	public static String getLongYMDChina() {
		String str = newLongYMDFormat().format(new Date());
		return str.split("-")[0]+" "+str.split("-")[1]+" "+str.split("-")[2]+" ";
	}

	/**
	 *     "yyyyMMdd"        
	 * @return "yyyyMMdd"        
	 */
	private static SimpleDateFormat newShortYMDFormat() {
		return new SimpleDateFormat("yyyyMMdd");
	}

	/**
	 *     "yyyyMMddHHmmss"        
	 * @return "yyyyMMddHHmmss"        
	 */
	private static SimpleDateFormat newShortYMDHMSFormat() {
		return new SimpleDateFormat("yyyyMMddHHmmss");
	}

	/**
	 *   "yyyyMMddHHmmss"       
	 * @return   "yyyyMMddHHmmss"       
	 */
	public static String getShortYMDHMS() {
		return newShortYMDHMSFormat().format(new Date());
	}

	/**
	 * "yyyyMMdd"        "yyyy-MM-dd"     
	 * @param shortYMD "yyyyMMdd"     
	 * @return "yyyy-MM-dd"     
	 * @throws ParseException
	 */
	public static String toLongYMD(String shortYMD) {
		try {
			return newLongYMDFormat().format(newShortYMDFormat().parse(shortYMD));
		} catch (ParseException e) {
			// Auto-generated catch block

			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 
	 *   :    yyyyMMdd
	 *
	 * @return
	 */
	public static String getDbDate() {
		return dateFormatDB.format(new Date());
	}

	/**
	 * 
	 *   :   yyyy-MM-dd     yyyyMMDD    
	 *
	 * @param dateStr
	 * @return
	 */
	public static String convertClientDateToDbDate(String dateStr) {
		String dbDateStr = null;
		try {
			dbDateStr = dateFormatDB.format(dateFormat.parse(dateStr));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dbDateStr;
	}

	/**
	 * 
	 *   :               :yyyy-MM-dd
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseDate(String dateStr) {
		Date date = null;
		try {
			date = dateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 *  UNIX         
	 * @param timestampString
	 * @return
	 */
	public static String getDate(String timestampString)
	{
		Long timestamp = Long.parseLong(timestampString)*1000;  
		String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(timestamp));  
		return date;  
	}

	/**
	 * 
	 *   :        
	 *   :2008-8-8     2008-08-08
	 *
	 * @param dateStr
	 * @return
	 */
	public static String getDateStrFormat(String dateStr) {
		try {
			Date date = dateFormat.parse(dateStr);
			return dateFormat.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 
	 *   :        
	 *   :2008-8     2008-08
	 *
	 * @param dateStr
	 * @return
	 */
	public static String getDateStrFormat2(String dateStr) {
		try {
			Date date = dateFormat2.parse(dateStr);
			return dateFormat2.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 2008-8-8   20080808 
	 * @param dateStr
	 * @return
	 */
	public static String getDateStrFormatyyyyMMdd(String dateStr) {
		try {
			Date date = dateFormat.parse(dateStr);
			return dateFormatDB.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 *   :               :yyyy-MM-dd HH:mm:ss
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseDateTime(String dateTimeStr) {
		Date date = null;
		try {
			date = dataTimeFormat.parse(dateTimeStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 *            
	 * 
	 * @param startDate
	 *              
	 * @param endDate
	 *              
	 * @return
	 */
	public static int calcDays(String startDate, String endDate) {
		int days = 1;
		try {
			long start = dateFormat.parse(startDate).getTime();
			long end = dateFormat.parse(endDate).getTime();
			days = (int) ((end - start) / (24 * 60 * 60 * 1000));
		} catch (ParseException e) {
			e.printStackTrace();
			return -1;
		}
		return days;
	}

	/**
	 *            
	 * 
	 * @param startDate
	 *              
	 * @param endDate
	 *              
	 * @return
	 */
	public static int calcDay(String startDate, String endDate) {
		int days = 1;
		try {
			long start = dateFormatDB.parse(startDate).getTime();
			long end = dateFormatDB.parse(endDate).getTime();
			days = (int) ((end - start) / (24 * 60 * 60 * 1000));
		} catch (ParseException e) {
			e.printStackTrace();
			return -1;
		}
		return days;
	}

	/**
	 *   :          
	 * 
	 * @param date
	 *            
	 * @param day
	 *            
	 * @return         
	 */
	public static Date addDate(Date date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
		return c.getTime();
	}

	/**
	 *   :          
	 * 
	 * @param date
	 *            
	 * @param minute
	 *            
	 * @return         
	 */
	public static Date addMinute(Date date, int minute) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) + ((long) minute) * 60 * 1000);
		return c.getTime();
	}

	/**
	 * 
	 *   :         
	 *
	 * @param date
	 * @param second
	 * @return
	 */
	public static Date addSecond(Date date, int second) {
		long s = date.getTime();
		s = s + second * 1000;
		return new Date(s);
	}

	/**
	 *   :          
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date diffDate(Date date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) - ((long) day) * 24 * 3600 * 1000);
		return c.getTime();
	}

	/**
	 * 
	 *   :     minute    
	 * 
	 * @param date
	 * @param minute
	 * @return
	 */
	public static Date getDateByMinuteAdd(Date date, int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, minute);
		return calendar.getTime();
	}

	/**
	 *   :        
	 * 
	 * @param startDate
	 *              
	 * @param endDate
	 *              
	 * @return         
	 */
	public static int diffDate(Date startDate, Date endDate) {
		long endMillis = endDate.getTime();
		long startMillis = startDate.getTime();
		long s = (endMillis - startMillis) / (24 * 3600 * 1000);
		return (int) s;
	}

	/**
	 *   :                
	 * 
	 * @param date
	 *          java.util.Date  
	 * @param format
	 *          yyyy-MM-dd HH:mm:ss | yyyy MM dd  HH mm ss 
	 * @return
	 */
	public static String format(Date date, String format) {
		String result = "";
		try {
			if (date == null) {
				date = new Date();//       ,        
			}
			if (StringUtil.isBlank(format)) {//        
				format = "yyyy-MM-dd";
			}
			DateFormat df = new SimpleDateFormat(format);
			result = df.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 *   :                
	 * 
	 * @param dateStr
	 *               
	 * @param format
	 *             dateStr          yyyy-MM-dd HH:mm:ss | yyyy MM dd  HH mm ss 
	 * @return
	 */
	public static Date format(String dateStr, String format) {
		if (StringUtil.isBlank(dateStr)) {
			return new Date();
		}
		if (StringUtil.isBlank(format)) {
			format = "yyyy-MM-dd";
		}
		Date date = null;
		try {
			DateFormat f = new SimpleDateFormat(format);
			date = f.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;

	}

	/**
	 *   :         
	 * 
	 * @param dateStr
	 *               
	 * @param format
	 *                  
	 * @param toFormat
	 *                
	 * @return
	 */
	public static String format(String dateStr, String format, String toFormat) {
		return format(format(dateStr, format), toFormat);
	}

	/**
	 *   :   rss   
	 *   :
	 * @param date
	 * @return
	 */
	public static String formatRssDate(Date date) {
		if (date == null) {
			date = new Date();//       ,        
		}
		SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
		SimpleTimeZone zone = new SimpleTimeZone(8, "GMT");
		sdf.setTimeZone(zone);
		return sdf.format(date);
	}

	/**
	 *   :   
	 * 
	 * @param date
	 * @return
	 */
	public static int getYear(Date date) {
		if (date == null) {
			date = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.YEAR);

	}

	/**
	 *   :   
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date) {
		if (date == null) {
			date = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MONTH) + 1;
	}

	/**
	 *   :   
	 * 
	 * @param date
	 * @return
	 */
	public static int getDay(Date date) {
		if (date == null) {
			date = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 *   :    
	 * 
	 * @param date
	 * @return
	 */
	public static int getHour(Date date) {
		if (date == null) {
			date = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 *   :   
	 * 
	 * @param date
	 * @return
	 */
	public static int getMinute(Date date) {
		if (date == null) {
			date = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MINUTE);
	}

	/**
	 *   :     1:   ,2:    ... 6:    7:   
	 * 
	 * @param date
	 * @return
	 */
	public static int getChinaWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (week == 0) {
			return 7;
		} else {
			return week;
		}
	}

	/**
	 *   :   
	 * 
	 * @param date
	 * @return
	 */
	public static int getSecond2(Date date) {
		if (date == null) {
			date = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.SECOND);
	}

	/**
	 *   :    
	 * 
	 * @param date
	 * @return
	 */
	public static long getMillis(Date date) {
		if (date == null) {
			date = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getTimeInMillis();
	}

	/**
	 *   :           
	 * 
	 * @return
	 */
	public static Date getMonFirstDay() {
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		c.set(getYear(date), getMonth(date) - 1, 1);
		return c.getTime();
	}

	/**
	 *   :            
	 * 
	 * @return
	 */
	public static Date getMonLastDay() {
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		c.set(getYear(date), getMonth(date), 1);
		c.setTimeInMillis(c.getTimeInMillis() - (24 * 3600 * 1000));
		return c.getTime();
	}
	/**
	 *   :            
	 * 
	 * @return
	 */
	public static Date getMonUpDay() {
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		c.set(getYear(date), getMonth(date)-1, 1);
		c.setTimeInMillis(c.getTimeInMillis() - (24 * 3600 * 1000));
		return c.getTime();
	}
	
	/**             */
	public static String getCurrMonthFirstDay() {
		Calendar cal = Calendar.getInstance();
		String s = (getYear(cal)) + "-" + (getMonth(cal)) + "-01";
		return s;
	}
	/**       2015-11 */
	public static String getCurrMonth() {
		Calendar cal = Calendar.getInstance();
		String s = (getYear(cal)) + "-" + (getMonth(cal));
		return getDateStrFormat2(s);
	}

	/**              */
	public static String getCurrMonthLastDay() {
		Calendar cal = Calendar.getInstance();
		String s = (getYear(cal)) + "-" + (getMonth(cal)) + "-" + getDays(cal);
		return s;
	}
	
	/**             */
	public static int getDays(Calendar cal) {
		return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	
	/**          */
	public static int getYear(Calendar cal) {
		return cal.get(Calendar.YEAR);
	}
	
	/**          */
	public static int getMonth(Calendar cal) {
		return (cal.get(Calendar.MONTH) + 1);
	}

	/**               */
	public static int getYear(String date_str, String type) {
		return (convertStrToCal(date_str, type).get(Calendar.YEAR));
	}
	
	/**      * */
	public static Calendar convertDateToCal(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}
	
	/**       (      ) */
	public static Calendar convertStrToCal(String date_str, String type) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(convertStrToDate(date_str, type));
		return cal;
	}
	
	/**       (      ) */
	public static Date convertStrToDate(String date_str, String type) {
		SimpleDateFormat dateformat = new SimpleDateFormat(type);
		try {
			return dateformat.parse(date_str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 *   :         :2008-02-02 23:11:10
	 * 
	 * @return
	 */
	public static String getCurrentDateTime() {
		Date date = new Date();
		return dataTimeFormat.format(date);
	}

	/**
	 *   :         :20101010
	 * 
	 * @return
	 */
	public static String getCurrentDate() {
		Date date = new Date();
		return dateFormat.format(date);
	}
	
	
	/**
	 *     "yyyyMM"        
	 * @return "yyyyMM"        
	 */
	private static SimpleDateFormat newShortYMFormat() {
		return new SimpleDateFormat("yyyyMM");
	}

	/**
	 *          diffMonth    
	 * @param month "yyyyMM"     
	 * @param diffMonth      
	 * @return "yyyyMM"     
	 * @throws ParseException
	 */
	public static String getShortYMDiffMonth(String month, int diffMonth) {
		SimpleDateFormat sdf = newShortYMFormat();
		try {
			sdf.parse(month);
		} catch (ParseException e) {
			// Auto-generated catch block
			e.printStackTrace();
		}
		Calendar c = sdf.getCalendar();
		c.add(Calendar.MONTH, diffMonth);
		return sdf.format(c.getTime());
	}

	/**
	 *         diffDay    
	 * @param shortYMD "yyyyMMdd"     
	 * @param diffDay      
	 * @return "yyyyMMdd"     
	 * @throws ParseException
	 */
	public static String getShortYMDDiffDay(String shortYMD, int diffDay) {
		SimpleDateFormat sdf = newShortYMDFormat();
		try {
			sdf.parse(shortYMD);
		} catch (ParseException e) {
			// Auto-generated catch block
			e.printStackTrace();
		}
		Calendar c = sdf.getCalendar();
		c.add(Calendar.DATE, diffDay);
		return sdf.format(c.getTime());
	}
	/**
	 *        days
	 * @param shortYMD
	 * @param diffDay
	 * @return
	 */
	public static String getAddDay(int diffDay) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy MM dd  HH:mm");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, diffDay);
        return sf.format(c.getTime());
	}

	/**
	 *           
	 * @param shortYM   
	 * @return          
	 * @throws Exception
	 */
	public static String getEndDayOfMonth(String shortYM) {
		String month = "";
		try {
			month = DateUtils.getShortYMDiffMonth(shortYM, 1);
		} catch (Exception e) {
			// Auto-generated catch block
			e.printStackTrace();
		}
		return DateUtils.getShortYMDDiffDay(month + "01", -1);
	}

	/**
	*   "yyyyMMdd"       
	* @return   "yyyyMMdd"       
	*/
	public static String getShortYMD() {
		return newShortYMDFormat().format(new Date());
	}

	/**
	 *                (           )
	 * @param startShortYMD "yyyyMMdd"       
	 * @param endShortYMD "yyyyMMdd"       
	 * @return "yyyyMMdd"       
	 * @throws ParseException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List getShortYMDList(String startShortYMD, String endShortYMD) throws ParseException {
		SimpleDateFormat startDateFormat = newShortYMDFormat();
		startDateFormat.parse(startShortYMD);
		Calendar startCal = startDateFormat.getCalendar();

		SimpleDateFormat endDateFormat = newShortYMDFormat();
		endDateFormat.parse(endShortYMD);
		Calendar endCal = endDateFormat.getCalendar();

		List dateList = new ArrayList();
		while (startCal.before(endCal)) {
			dateList.add(startDateFormat.format(startCal.getTime()));
			startCal.add(Calendar.DATE, 1);
		}

		if (startCal.getTimeInMillis() == endCal.getTimeInMillis()) {
			dateList.add(startDateFormat.format(endCal.getTime()));
		}

		return dateList;
	}

	/**
	 *                (           )
	 * @param startShortYM "yyyyMM"       
	 * @param endShortYM "yyyyMM"       
	 * @return "yyyyMM"       
	 * @throws ParseException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List getShortYMList(String startShortYM, String endShortYM) throws ParseException {
		SimpleDateFormat startDateFormat = newShortYMFormat();
		startDateFormat.parse(startShortYM);
		Calendar startCal = startDateFormat.getCalendar();

		SimpleDateFormat endDateFormat = newShortYMFormat();
		endDateFormat.parse(endShortYM);
		Calendar endCal = endDateFormat.getCalendar();

		List dateList = new ArrayList();
		while (startCal.before(endCal)) {
			dateList.add(startDateFormat.format(startCal.getTime()));
			startCal.add(Calendar.MONTH, 1);
		}

		if (startCal.getTimeInMillis() == endCal.getTimeInMillis()) {
			dateList.add(startDateFormat.format(endCal.getTime()));
		}

		return dateList;
	}

	/**
	 *            
	 * @param expDate
	 * @return
	 */
	public static boolean checkExpDate(String expDate) {
		int year = Integer.parseInt(expDate.substring(0, 4));
		int month = Integer.parseInt(expDate.substring(4, 6));
		int day = Integer.parseInt(expDate.substring(6, 8));
		if (month > 12 || month < 1) {
			return false;
		}

		int[] monthLengths = new int[] { 0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

		if (isLeapYear(year)) {
			monthLengths[2] = 29;
		} else {
			monthLengths[2] = 28;
		}

		int monthLength = monthLengths[month];
		if (day < 1 || day > monthLength) {
			return false;
		}
		return true;
	}

	/** 
	 *       
	 * */
	private static boolean isLeapYear(int year) {
		return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
	}

	/**
	 * 
	 *     :     (end) start      
* : 0, end start 1, end start -1
* @param start * @param end * @return * @throws Exception */ public static int compareEndAndStart(String start, String end) throws Exception { long s = 0; if (8 == start.length()) { s = dateFormatDB.parse(start).getTime(); } else if (10 == start.length()) { s = dateFormat.parse(start).getTime(); } else { s = dataTimeFormat.parse(start).getTime(); } long e = 0; if (8 == end.length()) { e = dateFormatDB.parse(end).getTime(); } else if (10 == end.length()) { e = dateFormat.parse(end).getTime(); } else { e = dataTimeFormat.parse(end).getTime(); } if (e > s) { return 1; } else if (e < s) { return -1; } return 0; } /** * date, * @param date * @return */ public static Date str2Date(String date) { Date ret = null; if (date.length() == 10) { try { ret = new SimpleDateFormat("yyyy-MM-dd").parse(date); } catch (ParseException e) { e.printStackTrace(); } } if (date.length() == 16) { try { ret = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(date); } catch (ParseException e) { e.printStackTrace(); } } if (date.length() == 19) { try { ret = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date); } catch (ParseException e) { e.printStackTrace(); } } if (date.length() == 13) { try { ret = new SimpleDateFormat("yyyy-MM-dd HH").parse(date); } catch (ParseException e) { e.printStackTrace(); } } if (date.length() == 7) { try { ret = new SimpleDateFormat("yyyy-MM").parse(date); } catch (ParseException e) { e.printStackTrace(); } } return ret; } /** * 8 * @return */ public static String getRandomNum(){ Random r=new Random(); int a = r.nextInt(99999999); String cardNum = ""+a; int length = cardNum.length(); if(length<8){ for(int j=0; j 16) { strFormat = Y_M_D_HMS; } else if (argDateStr.length() > 10) { strFormat = Y_M_D_HM; } } SimpleDateFormat sdfFormat = new SimpleDateFormat(strFormat); // sdfFormat.setLenient(false); try { return sdfFormat.parse(argDateStr); } catch (ParseException e) { throw new Exception(e); } } public static String getSSTimeStamp(){ Date d = new Date(); SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss:SS"); String strddd = sdf.format(d); return strddd; } /** *
* * @return */ public static String getWeekOfDate() { String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"}; // String[] weekDays = {" ", " ", " ", " ", " ", " ", " "}; Calendar cal = Calendar.getInstance(); // cal.setTime(dt); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return weekDays[w]; } // public static void main(String[] args) throws Exception { // // System.out.println(getSSTimeStamp()); // } /** * : / / * 1 : 2: 3: * @param date * @return */ public static int getEarlyMidLate(Date date) { int day=getDay(date); int earlyMidLate=0; if(1<=day && day<= 10){ earlyMidLate=1; } if(11<=day && day<=20){ earlyMidLate=2; } if(20

좋은 웹페이지 즐겨찾기