相關時間處理類

package com.common.time;

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



/**
 * 相關時間處理類
 * @author 
 * @version 1.0,  2012/09/15
 */
public class DateUtil 
{
	/**
	 * 得到指定日期的前一天
	 * 
	 * @param specifiedDay
	 * @return
	 * @throws Exception
	 */
	public static String getSpecifiedDayBefore(String specifiedDay) 
	{
		Calendar c = Calendar.getInstance();
		Date date = null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try 
		{
			
			date = simpleDateFormat.parse(specifiedDay);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);

		String dayBefore = simpleDateFormat.format(c.getTime());
		return dayBefore;
	}

	/**
	 * 得到指定日期的後一天
	 * 
	 * @param specifiedDay
	 * @return
	 */
	public static String getSpecifiedDayAfter(String specifiedDay) 
	{
		Calendar c = Calendar.getInstance();
		Date date = null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try 
		{
			date = simpleDateFormat.parse(specifiedDay);
		}
		catch (ParseException e) 
		{
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);

		String dayAfter = simpleDateFormat.format(c.getTime());
		return dayAfter;
	}
	
	/**
	 * 獲取指定時間的上個月的最後一天
	 * @param specifiedDay
	 * @return
	 */
	public static String getLastDayOfMonth(String specifiedDay) 
	{
		Calendar c = Calendar.getInstance();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try 
		{
			date = simpleDateFormat.parse(specifiedDay); //轉化爲Date格式
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		c.setTime(date);
		
		c.add(Calendar.MONTH, -1); //獲取上一個月
		
		int maxDate = c.getActualMaximum(Calendar.DAY_OF_MONTH);
	    c.set(Calendar.DAY_OF_MONTH, maxDate);
		
		String LastDayOfMonth = simpleDateFormat.format(c.getTime()); //格式化時間格式
		return LastDayOfMonth;
	}
	
	
	/**
	 * 獲取指定時間的上個月的最後一天
	 * @param specifiedDay
	 * @return
	 */
	public static String getLastDayOfMonth(Date specifiedDay) 
	{
		Calendar c = Calendar.getInstance();
		c.setTime(specifiedDay);
		c.add(Calendar.MONTH, -1); //設置上一個月
		
		int maxDate = c.getActualMaximum(Calendar.DAY_OF_MONTH); //設置最後一天
	    c.set(Calendar.DAY_OF_MONTH, maxDate);
		
		SimpleDateFormat simpleDateFormat =	new SimpleDateFormat("yyyy-MM-dd");
		String LastDayOfMonth = simpleDateFormat.format(c.getTime());   //格式化時間格式
		return LastDayOfMonth;
	}
	
	/**
	 * 獲取指定時間的上個月的第一天
	 * @param specifiedDay
	 * @return
	 */
	public static String getFirstDayOfMonth(String specifiedDay) 
	{
		Calendar c = Calendar.getInstance();
		SimpleDateFormat simpleDateFormat =	new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try 
		{
			date = simpleDateFormat.parse(specifiedDay); //轉化爲Date格式
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		c.setTime(date);
		c.add(Calendar.MONTH, -1); //設置上一個月
		
		int MiniDate = c.getActualMinimum(Calendar.DAY_OF_MONTH);
	    c.set(Calendar.DAY_OF_MONTH, MiniDate);
		
		String FirstDayOfMonth = simpleDateFormat.format(c.getTime());
		return FirstDayOfMonth ;
	}
	
	/**
	 * 獲取上個月的第一天
	 * @param specifiedDay
	 * @return
	 */
	public static String getFirstDayOfMonth(Date specifiedDay) 
	{
		Calendar c = Calendar.getInstance();
		SimpleDateFormat simpleDateFormat =	new SimpleDateFormat("yyyy-MM-dd");

		c.setTime(specifiedDay);
		
		c.add(Calendar.MONTH, -1); //設置上一個月
		
		int MiniDate = c.getActualMinimum(Calendar.DAY_OF_MONTH);
	    c.set(Calendar.DAY_OF_MONTH, MiniDate);
		
		String FirstDayOfMonth = simpleDateFormat.format(c.getTime());
		return FirstDayOfMonth ;
	}
	
	
	
	/**
	 *  
	 * 方法描述:取得當前日期的上月或下月日期 ,amount=-1爲上月日期,amount=1爲下月日期;建立人:
	 * @param s_DateStr
	 * @param s_FormatStr
	 * @return
	 * @throws Exception
	 */
	public static String getFrontBackStrDate(String strDate,int amount) throws Exception 
	{
		if (null == strDate) 
		{
			return null;
		}
		try 
		{

			SimpleDateFormat fmt = new SimpleDateFormat("yy-MM-dd");
			Calendar c = Calendar.getInstance();
			c.setTime(fmt.parse(strDate));
			c.add(Calendar.MONTH, amount);
			return fmt.format(c.getTime());
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return "";
	}

	
	/**
	 * 返回兩時間差,拼接成字符串返回
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static String getTimeSub(Long time1, Long time2 )
	{
		String result = "";
		try 
		{
			Long diff = time2 - time1;   //兩時間差,精確到毫秒 
			
			Long day = diff / (1000 * 60 * 60 * 24);         //以天數爲單位取整
			Long hour=(diff/(60*60*1000)-day*24);            //以小時爲單位取整 
			Long min=((diff/(60*1000))-day*24*60-hour*60);        //以分鐘爲單位取整 
			Long secone=(diff/1000-day*24*60*60-hour*60*60-min*60);
			
			result = day + "|" + hour+ "|" + min ; 
			System.out.println("---兩時間差---> " +day+"天"+hour+"小時"+min+"分"+secone+"秒");
	
		} 
		catch (RuntimeException e) 
		{
			e.printStackTrace();
		}
		return result ;
	}
	
	
}
相關文章
相關標籤/搜索