日期包装工具类DateWarpUtil

package com.tanfei.sysUtils.wrapCommonUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间日期包装类
 * @author 谭飞
 * @date 2012-01-12
 */
public class DateWarpUtil {

	 public static final DateFormat default_date_format = new SimpleDateFormat("yyyy-MM-dd");
	 /*年月日时分秒*/
	 public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
	 /*年月日*/
	 public static final String DATE_PART_FORMAT = "yyyy-MM-dd";
	 /*时分秒毫秒*/
	 public static final String TIME_PART_FORMAT = "HH:mm:ss.SSS";
	 /*年*/
	 public static final String YEAR_PART_FORMAT = "yyyy";
	 /*月*/
	 public static final String MONTH_PART_FORMAT = "MM";
	 /*日*/
	 public static final String DAY_PART_FORMAT = "dd";
	 /*时*/
	 public static final String HOUR_PART_FORMAT = "HH";
	 /*分*/
	 public static final String MINUTES_PART_FORMAT = "mm";
	 /*秒*/
	 public static final String SECOND_PART_FORMAT = "ss";
	 /*毫秒*/
	 public static final String MILLISECOND_PART_FORMAT = "SSS";
	 
	
	 
	 /**
	   * 格式化日期时间
	   * @param formatStr
	   * @return
	   */
	  public static String getCurDateTime(String formatStr)
	  {
	    SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
	    String now = sdf.format(new Date());
	    return now;
	  }
	  
	 /**
	  * 取得当前日期时间
	  * @return
	  */
	 public static String getCurDateTime()
	  {
	    return getCurDateTime(DEFAULT_PATTERN);
	  }

	 /**
	  * 取得当前日期
	  * @return
	  */
	  public static String getCurDate()
	  {
	    return getCurDateTime(DATE_PART_FORMAT);
	  }
	  
	  /**
	   * 取得当前时间
	   * @return
	   */
	  public static String getCurTime()
	  {
		  return getCurDateTime(TIME_PART_FORMAT);
	  }
	  
	 /**
	  * 取得当前年份
	  * @return
	  */
	  public static String getCurYear()
	  {
		  return getCurDateTime(YEAR_PART_FORMAT);
	  }
	  
	  /**
	   * 取得当前月份
	   * @return
	   */
	  public static String getCurMonth()
	  {
		  return getCurDateTime(MONTH_PART_FORMAT);
	  }
	  
	  /**
	   * 取得当前日
	   * @return
	   */
	  public static String getCurDay()
	  {
		  return getCurDateTime(DAY_PART_FORMAT);
	  }
	  
	  /**
	   * 取得当前时
	   * @return
	   */
	  public static String getCurHour()
	  {
		  return getCurDateTime(HOUR_PART_FORMAT);
	  }
	  
	  /**
	   * 取得当前分钟
	   * @return
	   */
	  public static String getCurMinute()
	  {
		  return getCurDateTime(MINUTES_PART_FORMAT);
	  }
	  
	  /**
	   * 取得当前秒
	   * @return
	   */
	  public static String getCurSecond()
	  {
		  return getCurDateTime(SECOND_PART_FORMAT);
	  }
	  
	  /**
	   * 取得当前毫秒
	   * @return
	   */
	  public static String getCurMillisecond()
	  {
		  return getCurDateTime(MILLISECOND_PART_FORMAT);
	  }
	  
	  /*-----------------扩展--------------*/
	  public static final String FIRSTDAY_PART_FORMAT = "yyyy-MM-01";
	  public static final String LASTDAY_PART_FORMAT = "yyyy-MM-";
	  
	  /**
	   * 取得当月的第一天
	   * @return
	   */
	  public static String getCurFirstDay()
	  {
		  return getCurDateTime(FIRSTDAY_PART_FORMAT);
	  }
	  
	  /**
	   * 取得当月最后一天
	   * @return
	   */
	  public static String getCurLastDay()
	  {
		  Calendar cal = Calendar.getInstance();
		  int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		  return getCurDateTime(LASTDAY_PART_FORMAT + maxDay);
	  }
	  
	  /**
	   * 两个日期相差的天数
	   * @param firstDateStr 起止日期 yyyy-MM-dd
	   * @param lastDateStr 结束日期 yyyy-MM-dd
	   * @return 天数
	   */
	  public static String getTwoDateDays(String firstDateStr, String lastDateStr)
	  {
		  SimpleDateFormat format = new SimpleDateFormat(DATE_PART_FORMAT);
		  long days = 0;
		  try {
			  if(Long.valueOf(firstDateStr.replace("-", "")) >= Long.valueOf(lastDateStr.replace("-", "")))
			  {
				days =   (format.parse(firstDateStr).getTime() 
							- format.parse(lastDateStr).getTime())/(24*60*60*1000);
			  }else
			  {
				  days =   (format.parse(lastDateStr).getTime() 
							- format.parse(firstDateStr).getTime())/(24*60*60*1000);
			  }
		  } catch (Exception e) {
				System.out.println("日期转换异常:传入的日期字符串格式不正确,正确格式(yyyy-MM-dd)如:2012-01-01。" );
				e.printStackTrace();
			}
		  return days + "";
	  }
	  
	 
	  
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("当前日期时间:" + getCurDateTime()
				+ "\n当前日期:" + getCurDate()
				+ "\n当前时间:" + getCurTime()
		);
		
		System.out.println("------------\n"+ getTwoDateDays("2011-01-10", "2012-01-12") );
		
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值