日期操作函数

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.apache.log4j.Logger;

public class DateDeal {
	private static final Logger logger = Logger.getLogger("ejb");
	protected static DateFormat formatter1 = new SimpleDateFormat("yyyyMMddHHmmss");
	protected static DateFormat formatter2 = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
	protected static DateFormat formatter3 = new SimpleDateFormat("yyyy年MM月dd日");
	protected static DateFormat formatter4 = new SimpleDateFormat("yyyyMMdd");

	public DateDeal() {}

	/**
	* 计算日期相隔的天数(按照每月30天计算)
	* @param startDate 开始日期
	* @param endDate   结束日期
	* @return  int 天数
	*/
	public static int getDays(String startDate, String endDate) {
		int date = 0;
		int sYear = Integer.parseInt(startDate.substring(0, 4));
		int sMonth = Integer.parseInt(startDate.substring(4, 6));
		int sDate = Integer.parseInt(startDate.substring(6, 8));
		int eYear = Integer.parseInt(endDate.substring(0, 4));
		int eMonth = Integer.parseInt(endDate.substring(4, 6));
		int eDate = Integer.parseInt(endDate.substring(6, 8));

		if ( (sMonth == eMonth) && (sYear == eYear)) {
			date = eDate - sDate;
		}else {
      		if (sDate == 31) {
        		sDate = 30;
      		}
      		
      		if (eDate == 31) {
        		eDate = 30;
      		}
      		
      		if (sMonth == 2 && sDate >= 28) {
        		sDate = 30;
      		}
      		
      		if (eMonth == 2 && eDate >= 28) {
        		eDate = 30;
      		}
      		date = (eYear - sYear) * 360 + (eMonth - sMonth) * 30 + (eDate - sDate);
    	}
    	return date;
  }

	/**
	* 计算日期相隔的天数(实际)
	* @param startDate 开始日期
	* @param endDate   结束日期
	* @return  int 天数
	*/
  	public static long getRealDays(String startDate, String endDate) {
    	Date sDate = null;
    	Date eDate = null;
    	try {
	      	sDate = formatter4.parse(startDate);
    	  	eDate = formatter4.parse(endDate);
    	}catch (ParseException e) {
      		e.printStackTrace();
    	}
    	long beginTime = sDate.getTime();
    	long endTime = eDate.getTime();
    	long betweenDays = (long) ( (endTime - beginTime) / (1000 * 60 * 60 * 24) + 0.5);
    	return betweenDays;
  	}

	/**
	* 字符串转化成日期-(yyyyMMddHHmmss)
	* @param dateString
	* @return
	* @throws ParseException
	*/
	public static Date stringToDate(String dateString) throws ParseException {
    	return formatter1.parse(dateString);
  	}

	/**
	* 日期转化成字符串-(yyyyMMddHHmmss)
	* @param date
	* @return
	* @throws ParseException
	*/
	public static String dateToString(Date date) throws ParseException {
    	return formatter1.format(date);
  	}

	/**
	* 日期转换成中文字符串(yyyy年MM月dd日)
	* @param date
	* @return
	* @throws ParseException
	*/
  	public static String dateToShortChinese(Date date) throws ParseException {
    	return formatter3.format(date);
  	}

	/**
	* 日期字符串转换成中文("yyyy年MM月dd日HH:mm:ss")
	* @param dataString
	* @return
	* @throws ParseException
	*/
	public static String dateStringToChinese(String dataString) throws ParseException {
    	return formatter2.format(stringToDate(dataString));
  	}

	/**
	* 日期字符串转换成中文("yyyy年MM月dd日")
	* @param dataString
	* @return
	* @throws ParseException
	*/
  	public static String dateStringToShortChinese(String dataString) throws ParseException {
    	return formatter3.format(stringToDate(dataString));
  	}

	public static String trans() {
		String dtmp = null;
		String mtmp = null;
	
		Calendar calendarSys = new GregorianCalendar();
		//取当前时间
		Date d = new Date(System.currentTimeMillis());
		calendarSys.setTime(d);
		//转换后,取年,月,日
		Integer year = new Integer(calendarSys.get(Calendar.YEAR));
		Integer month = new Integer(calendarSys.get(Calendar.MONTH) + 1);
		Integer date = new Integer(calendarSys.get(Calendar.DATE));
		
		if (date.intValue() < 10) {
			dtmp = "0" + date.toString();
		}else {
			dtmp = date.toString();
		}
	
		if (month.intValue() < 10) {
			mtmp = "0" + month.toString();
		}else {
			mtmp = month.toString();
		}
		
		//转化为字符串
		String ds = new String(year.toString() + mtmp + dtmp);
		return ds;
	}

	/* *
	得到当前的时间:时 分 秒
	*/
	public static String transHMS() {
		String dtmp = null;
		String mtmp = null;
		String stmp = null;
	
		Calendar calendarSys = new GregorianCalendar();
		//取当前时间
		Date d = new Date(System.currentTimeMillis());
		calendarSys.setTime(d);
		//转换后,取年,月,日
		Integer hour = new Integer(calendarSys.get(Calendar.HOUR_OF_DAY));
		Integer minute = new Integer(calendarSys.get(Calendar.MINUTE));
		Integer second = new Integer(calendarSys.get(Calendar.SECOND));
		if(hour.intValue()<10){
			dtmp="0"+hour.toString();
		}else{
			dtmp=hour.toString();
		}
	
		if(minute.intValue()<10){
			mtmp="0"+minute.toString();
		}else{
			mtmp=minute.toString();
		}
	
		if(second.intValue()<10){
			stmp="0"+second.toString();
		}else{
			stmp=second.toString();
		}
	
		//转化为字符串
		String ds = new String(dtmp+mtmp+ stmp);
		logger.debug("当前时间:<<<" + ds + ">>>");
		return ds;
	}


	 /*
	 *依据日期(20050808)获得该日期是本月的第几周
	 */
	public static Integer weekOfMonth(String txdate) throws ParseException {
		Date date=null;
		Integer weekOfMonth=null;
		Calendar calendarSys=new GregorianCalendar();
		calendarSys.setFirstDayOfWeek(Calendar.SUNDAY);
	
		//转换日期
		date=formatter4.parse(txdate);
		calendarSys.setTime(date);
	
		weekOfMonth=new Integer(calendarSys.get(Calendar.WEEK_OF_MONTH));

		return weekOfMonth;
	}

	/**
	*依据年月(200508)和本月第几(2)周获得该周在本月的起始日期(20050807-20050813)
	*/
	public static String startEndDate(String yearMonth, Integer weekOfMonth) throws ParseException {
		String firstDateOfMonth=null;
		String startdate=null;
		String enddate=null;
		String startEndDate=null;
		Date date01=null;
	
		Calendar calendarSys=new GregorianCalendar();
		calendarSys.setFirstDayOfWeek(Calendar.SUNDAY);
		//转换日期
		String txdate01=yearMonth+"01"; //某年月1日(20050801)
		date01=formatter4.parse(txdate01);
		calendarSys.setTime(date01);
	
		//依据某年月1日(20050801)获得是星期几(星期1)
		Integer dayOfWeek=new Integer(calendarSys.get(Calendar.DAY_OF_WEEK)-1);
		logger.debug("dayOfWeek:"+dayOfWeek) ;
	
		//获得本月的第一周的起始日期
		calendarSys.add(Calendar.DATE,-dayOfWeek.intValue());
		logger.debug("获得本月的第一周的起始日期:"+formatter4.format(calendarSys.getTime()));
	
		//获得起始日
		calendarSys.add(Calendar.DATE,(weekOfMonth.intValue()-1)*7);
		startdate=formatter4.format(calendarSys.getTime()) ;
		logger.debug("获得起始日:"+startdate);
	
		//获得终止日
		calendarSys.add(Calendar.DATE,6);
		enddate=formatter4.format(calendarSys.getTime());
		logger.debug("获得终止日:"+enddate);
		
		startEndDate=startdate+"-"+enddate;
		return startEndDate;
	}

	/*
	*依据年月日(20050808)加上x年 或 x月 或x日,返回加上x年 或 x月 或x日 后的日期(20050907)
	*param txdate String 交易日期
	*param type String 类型 1-年,2-月,3-日
	*param yearMonthDay Integer x年 或 x月 或x日
	*/
	public static String add(String txdate, String type, Integer yearMonthDay) throws ParseException {
		String resultDate=null;
		
		Calendar calendarSys=new GregorianCalendar();
		calendarSys.setFirstDayOfWeek(Calendar.SUNDAY);
		
		//转换日期
		Date date=formatter4.parse(txdate);
		calendarSys.setTime(date);
		
		//获得起始日
		if(type.trim().equals("1")){
			calendarSys.add(Calendar.YEAR,yearMonthDay.intValue());
		}else if(type.trim().equals("2")){
			calendarSys.add(Calendar.MONTH,yearMonthDay.intValue());
		}else{
			calendarSys.add(Calendar.DATE,yearMonthDay.intValue());
		}
	
		resultDate=formatter4.format(calendarSys.getTime()) ;
		logger.debug("日期:"+resultDate);
	
		return resultDate;
	}

	/**
	* 取得传入的日期的前一天的日期
	* @param curDay String
	* @return String
	*/
	public static String getYDay(String txDate) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		long timeT = sdf.parse(txDate).getTime();
		return sdf.format(new Date(timeT - 24 * 60 * 60 * 1000));
	}
}

 

你可能感兴趣的:(apache,log4j,ejb,360)