DateHelper

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

/*
 * DateHelper.java
 * This is a static class for providing Date function.
 */

public class DateHelper {

	static final public String cDateFormat = "dd/MM/yyyy";
	static final public String cGlobalDateFormat = "dd MMM yyyy";	
	static public int GetDayOfWeek(Date oInputDate)
	{
		return oInputDate.getDay();
	}
	
	static public int GetMonday(Date oInputDate)
	{
		return GetDayOfWeek(oInputDate)-1;
	}
	
	static public int GetFriday(Date oInputDate)
	{
		return 5-GetDayOfWeek(oInputDate);
	}
	
	static public int GetSunday(Date oInputDate)
	{
		return GetDayOfWeek(oInputDate)-0;
	}
	
	static public int GetSaturday(Date oInputDate)
	{
		return 6-GetDayOfWeek(oInputDate);
	}
	static public Date GetStartOfWeek(Date oInputDate)
	{	
		return ToNextNDays(oInputDate, 0-GetSunday(oInputDate));
	}
	
	static public Date GetEndDayOfWeek(Date oInputDate)
	{	
		return ToNextNDays(oInputDate, GetSaturday(oInputDate));
	}
	
	/*
	 * The function is to get next n days from input date
	 * 
	 * Date oInputDate : Input date
	 * int iNoOfDay : Number of day to add
	 */
	static public Date ToNextNDays(Date oInputDate, int iNoOfDay)
	{
		Calendar oCalendar = Calendar.getInstance(); 
		oCalendar.setTime(oInputDate);
		oCalendar.add(Calendar.DATE, iNoOfDay);
		return oCalendar.getTime();
	}
	
	static public String FormatDate(Date oInputDate, String sFormat){
		String output = "";
		if(!util.isEmpty(oInputDate) && !util.isEmpty(sFormat)){
			try{
				DateFormat oDateFormat = new SimpleDateFormat(sFormat);
		        String sDatetime = oDateFormat.format(oInputDate);
		        output =  sDatetime;
			} catch(Exception e){
				e.printStackTrace();
				output = "";
			}
		}
		return output;
	}
	
	static public String FormatDate(Date oInputDate)
	{
		return DateHelper.FormatDate(oInputDate, cDateFormat);
	}

	static public String FormatGlobalDate(Date oInputDate)
	{
		return DateHelper.FormatDate(oInputDate, cGlobalDateFormat).toUpperCase(Locale.ENGLISH);
	}
	
	//Format global date without throwing exception
	static public String FormatGlobalDateNoEx(String oInputDate)
	{
		String output = "";
			try{
				DateFormat oDateFormat = new SimpleDateFormat(cDateFormat);
				Date outputDate = oDateFormat.parse(oInputDate);
				
				DateFormat oGlobalDateFormat = new SimpleDateFormat(cGlobalDateFormat);
		        String sDatetime = oGlobalDateFormat.format(outputDate);
		        output =  sDatetime;
			} catch(Exception e){
				output = oInputDate;
			}
		return output;
	}
		
	static public Date getDateFromString(String sDate){
		return DateHelper.GetDateFromString(sDate, DateHelper.cDateFormat);
	}

	static public Date GetDateFromString(String sDate, String sFormat)
	{
		try
		{
			DateFormat oDateFormat = new SimpleDateFormat(sFormat);
			return oDateFormat.parse(sDate);			
		}catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	static public TimeZone getTimeZone(String sTimeZone)
	{
		return TimeZone.getTimeZone(sTimeZone);
	}
	static public Date ConvertDate(Date oInputDate, java.util.TimeZone oFromTimeZone, java.util.TimeZone oToTimeZone)
	{
		try {
			if (oFromTimeZone != null)
			{
				//osdf.setTimeZone(oFromTimeZone);
				Calendar cal = new GregorianCalendar(oFromTimeZone);
				cal.setTime(new Date());  
				cal.set(Calendar.HOUR_OF_DAY, 0);
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				  
				
				//cal.setTime(oInputDate);
				
				Calendar cal2 = new GregorianCalendar(oToTimeZone);
				cal2.setTime(new Date());  
				cal2.set(Calendar.HOUR_OF_DAY, 0);
				cal2.set(Calendar.MINUTE, 0);
				cal2.set(Calendar.SECOND, 0);
				cal2.set(Calendar.MILLISECOND, 0);
				
				Calendar oCalendar = Calendar.getInstance(); 
				oCalendar.setTime(oInputDate);
				
				long different = cal.getTimeInMillis() - cal2.getTimeInMillis();
				oCalendar.setTimeInMillis(oCalendar.getTimeInMillis() + different);
				
								return oCalendar.getTime();
			}
			return oInputDate;
		}
		catch (Exception e1){
			return oInputDate;
		}
		
	
		
	}
	

	static public Date ToGmtTime(Date oInputDate)
	{
		Calendar oCalendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
		Calendar oTmpCalendar = new GregorianCalendar();
		oTmpCalendar.setTime(oInputDate);
		oCalendar.set(oTmpCalendar.get(Calendar.YEAR), oTmpCalendar.get(Calendar.MONTH), oTmpCalendar.get(Calendar.DATE), oTmpCalendar.get(Calendar.HOUR), oTmpCalendar.get(Calendar.MINUTE), oTmpCalendar.get(Calendar.SECOND));
		return oCalendar.getTime();
	}
	
	static public Date getLargerDate(Date oInputDate, Date oInputDate2)
	{
		System.out.println("Enter larger day function");
		if(oInputDate.after(oInputDate2))
			return oInputDate;
		else
			return oInputDate2;
		
	}
	
	static public Date getSmallDate(Date oInputDate, Date oInputDate2)
	{
		System.out.println("Enter small day function");
		if(oInputDate.before(oInputDate2))
			return oInputDate;
		else
			return oInputDate2;
		
	}
	
	static public Date getMaxDate(ArrayList sInputDateList)
	{
		Date oMaxDate = new Date();
		System.out.println("First Date is " + sInputDateList.get(0));
		
		oMaxDate = GetDateFromString((String)sInputDateList.get(0), cDateFormat);
		System.out.println("Size of the ArrayList" + sInputDateList.size());
		for (int i=1; i <sInputDateList.size(); i++)
		{
			Date oTempLargerDate = new Date();
			Date oInputDateList = new Date();
			if (sInputDateList.get(i) != null)
			{
				oInputDateList = GetDateFromString((String)sInputDateList.get(i), cDateFormat);
				System.out.println("Current Max Date:**********" + oMaxDate);
				System.out.println("Current input Date:**********" + oInputDateList);
				oMaxDate = getLargerDate(oMaxDate,oInputDateList);
				System.out.println("Current max date" + oMaxDate);
			}
				
			
		}
		return oMaxDate;
		
	}
	
	static public Date getMinDate(ArrayList sInputDateList)
	{
		Date oMinDate = new Date();
		System.out.println("First Date is " + sInputDateList.get(0));
		
		oMinDate = GetDateFromString((String)sInputDateList.get(0), cDateFormat);
		for (int i=1; i <sInputDateList.size(); i++)
		{
			Date oTempLargerDate = new Date();
			Date oInputDateList = new Date();
			if (sInputDateList.get(i) != null)
			{
				oInputDateList = GetDateFromString((String)sInputDateList.get(i), cDateFormat);
				oMinDate = getSmallDate(oMinDate,oInputDateList);
			}
			
			
		}
		return oMinDate;
	}
	
	static public int getDateDifferenceInDays(Date date1, Date date2){
		long diffDays = 0;
		try{
		    long milliseconds1 = date1.getTime();
		    long milliseconds2 = date2.getTime();
		    long diff = Math.abs(milliseconds1 - milliseconds2);
		    diffDays = diff / (24 * 60 * 60 * 1000);
		} catch(Exception e){
			e.printStackTrace();
		}
	    return (int)diffDays;
	}
}

 

你可能感兴趣的:(Date)