一个自己写得日期工具

	public static String DATE_FMT = "yyyy-MM-dd";
	public static String TIME_FMT = "yyyy-MM-dd HH:mm:ss";
	public static String SIMPLE_TIME_FMT = "yyyyMMddHHmmss";

	public SimpleDateFormat dfForm = null;

	/**
	 * 格式化日期
	 * 
	 * @param sDate
	 * @param sFmtFrom
	 * @param sFmtTo
	 * @return
	 * @throws ParseException
	 */
	public static String formatDate(String sDate, String sFmtFrom, String sFmtTo)
			throws ParseException {

		SimpleDateFormat sdfFrom = new SimpleDateFormat(sFmtFrom);

		SimpleDateFormat sdfTo = new SimpleDateFormat(sFmtTo);

		return sdfTo.format(sdfFrom.parse(sDate));
	}

	/**
	 * 格式化日期
	 * 
	 * @param dt
	 * @param sFmt
	 * @return
	 */
	public static String formatDate(Date dt, String sFmt) {

		if (dt == null || sFmt == null)

			return "";

		SimpleDateFormat sdfFrom = new SimpleDateFormat(sFmt);

		return sdfFrom.format(dt).toString();

	}
	/**
	 * 格式化日期,yyyy-MM-dd
	 * 
	 * @param dt
	 * @return
	 */
	public static String formatDate(Date dt) {

		return formatDate(dt, DATE_FMT);
	}
	/**
	 * 格式化日期,yyyy-MM-dd HH:mm:ss
	 * 
	 * @param dt
	 * @return
	 */
	public static String formatDateTime(Date dt) {

		return formatDate(dt, TIME_FMT);
	}
	/**
	 * 格式化日期,yyyyMMddHHmmss
	 * 
	 * @param dt
	 * @return
	 */
	public static String formatSimpleDate(Date dt) {

		return formatDate(dt, SIMPLE_TIME_FMT);
	}
	/**
	 * 得到当前时间,yyyyMMddHHmmss
	 * 
	 * @param args
	 */
	public static String getCurrentSimpleTime() {
		Date dt = new Date();
		return formatDate(dt, SIMPLE_TIME_FMT);
	}
	/**
	 * 得到当前时间,yyyy-MM-dd HH:mm:ss
	 * 
	 * @param args
	 */
	public static String getCurrentDateTime() {
		Date dt = new Date();
		return formatDate(dt, TIME_FMT);
	}
	/**
	 * 得到当前时间,yyyy-MM-dd
	 * 
	 * @param args
	 */
	public static String getCurrentDate() {
		Date dt = new Date();
		return formatDate(dt, DATE_FMT);
	}
	/**
	 * 得到当前时间
	 * 
	 * @return
	 */
	public static Timestamp getNowTimestamp() {
		return Timestamp.valueOf(new SimpleDateFormat(TIME_FMT).format(new Date()));
	}
	/**
	 * 转换日期格式
	 * @param date
	 * @return
	 */
	public static Timestamp conver2Timestamp(String date){
		return Timestamp.valueOf(date);
	}
	/**
	 * 
	 * 方法描述:返回Date型日期,yyyy-MM-dd HH:mm:ss
	 * @author ming.li
	 * @time 2011-4-28 下午01:15:32
	 *
	 * @param dateStr
	 * @return
	 * @throws ParseException
	 */
	public static Date string2Date(String dateStr) throws ParseException{
		SimpleDateFormat sdf=new SimpleDateFormat(TIME_FMT);
		return sdf.parse(dateStr);
	}
	/**
	 * 
	 * 方法描述:获取近半年时间,yyyyMM
	 * @author ming.li
	 * @time 2011-4-28 下午01:15:35
	 *
	 * @return
	 */
	public static String[] getNearHalfYear(){
		String[] halfYear=new String[6];
		String today = getCurrentDate();
		String yearTemp=today.substring(0, 4);
		String monthTemp=today.substring(5, 7);
		int year = Integer.parseInt(yearTemp);
		int month = Integer.parseInt(monthTemp)+1;
		if(month>6){
			for(int i=0;i<6;i++){
				month--;
				halfYear[i]=yearTemp+month;
			}
		}else{
			switch(month){
				case 6:{
					halfYear[0]=yearTemp+"05";
					halfYear[1]=yearTemp+"04";
					halfYear[2]=yearTemp+"03";
					halfYear[3]=yearTemp+"02";
					halfYear[4]=yearTemp+"01";
					halfYear[5]=(year-1)+"12";
					break;
				}
				case 5:{
					halfYear[0]=yearTemp+"04";
					halfYear[1]=yearTemp+"03";
					halfYear[2]=yearTemp+"02";
					halfYear[3]=yearTemp+"01";
					halfYear[4]=(year-1)+"12";
					halfYear[5]=(year-1)+"11";
					break;
				}
				case 4:{
					halfYear[0]=yearTemp+"03";
					halfYear[1]=yearTemp+"02";
					halfYear[2]=yearTemp+"01";
					halfYear[3]=(year-1)+"12";
					halfYear[4]=(year-1)+"11";
					halfYear[5]=(year-1)+"10";
					break;
				}
				case 3:{
					halfYear[0]=yearTemp+"02";
					halfYear[1]=yearTemp+"01";
					halfYear[2]=(year-1)+"12";
					halfYear[3]=(year-1)+"11";
					halfYear[4]=(year-1)+"10";
					halfYear[5]=(year-1)+"09";
					break;
				}
				case 2:{
					halfYear[0]=yearTemp+"01";
					halfYear[1]=(year-1)+"12";
					halfYear[2]=(year-1)+"11";
					halfYear[3]=(year-1)+"10";
					halfYear[4]=(year-1)+"09";
					halfYear[5]=(year-1)+"08";
					break;
				}
				case 1:{
					halfYear[0]=(year-1)+"12";
					halfYear[1]=(year-1)+"11";
					halfYear[2]=(year-1)+"10";
					halfYear[3]=(year-1)+"09";
					halfYear[4]=(year-1)+"08";
					halfYear[5]=(year-1)+"07";
					break;
				}
			}
		}
		
		return halfYear;
	}

 

你可能感兴趣的:(java,Date,mm,yyyy,dd)