`

DateHelper

    博客分类:
  • Java
 
阅读更多
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;
	}
}

 

分享到:
评论

相关推荐

    DateHelper日期和时间戳之间的相互转换可用于浏览器和NodeJS

    "DateHelper"是一个专门针对日期和时间戳转换的库,它支持在浏览器环境和Node.js环境中使用,这使得开发者在跨平台开发时能够有一致的日期处理体验。下面将详细探讨DateHelper库的特性、用法以及如何在实际项目中...

    swift-DateHelper-NSDate在Swift中的便利扩展

    为了简化这些操作,开发者常常会创建一个便利扩展,例如"swift-DateHelper-NSDate在Swift中的便利扩展"项目,它为`NSDate`(Objective-C的日期表示类)提供了额外的方法,以便在Swift中更方便地使用。 DateHelper...

    DateHelper,.zip

    简单的约会助手,可以帮助你让你的生活更轻松。

    datehelper.js:一组简单的原型,用于轻松进行日期格式化

    日期助手.js 一组简单的原型,用于简单的日期格式化。 用法如下: 要获取时间值,请使用以下命令: var time = new Date().timeNow('hh:mm:ss'); 将返回10:58:16 var time = new Date().timeNow('hh:mm');...

    DateHelper:Swift Date扩展助手

    日期助手 高性能的Swift日期扩展,用于创建,转换,比较或修改日期。 能力 从字符串创建日期 提供两个初始化程序以从字符串创建日期。 detectFromString: init?(detectFromString string: String) 使用...

    java时间处理工具类--DateUtils

    * 获取DateHelper实例, 使用当前时间作为基准时间 * * @return Date */ public static DateUtils getInstance() { return new DateUtils(null); } /** * 获取年的第一天 * * @param offset ...

    iOS NSdate 时间转换

    这样,我们就可以在项目中通过`DateHelper.sharedInstance.stringFromDate(someNSDate)`或`DateHelper.sharedInstance.dateFromString(someDateString)`方便地进行日期转换,无需每次都创建新的`NSDateFormatter`...

    C# 公历农历转化.rar

    `DateHelper.cs` 文件很可能定义了一个名为 `DateHelper` 的类,这个类包含了将公历日期转化为农历日期,以及农历日期转化为公历日期的方法。在C#中,我们可以利用`DateTime`结构体来处理日期和时间,而转换农历则...

    java datetime

    public static DateHelper getInstance(Date fiducialDate) { return new DateHelper(fiducialDate); } public static DateHelper getInstance() { return new DateHelper(null); } ``` 这两个方法都是为了创建`...

    Java常用日期辅助类

    在给定的标题“Java常用日期辅助类”中,我们可以推测这篇博文可能探讨了Java中的一些核心日期处理工具,如`java.util.Date`,`java.time`包中的类,以及自定义的日期助手类`DateHelper.java`。 `java.util.Date`是...

    Android常用类库

    日期处理在Android应用中很常见,比如记录创建时间、显示日期标签等,DateHelper可以简化这部分的代码。 5. **IOUtils.java**: IOUtils通常封装了输入/输出流的操作,例如读取文件、写入文件、复制流等。在处理...

    asp.net常用工具类

    - `DateHelper`:日期时间处理,如格式化日期,计算时间差等。 - `EncryptionHelper`:加密解密功能,可能包含AES、DES、MD5等算法。 - `CacheHelper`:缓存管理,用于存储和获取数据,提高性能。 - `MailHelper...

    DotNet多年积累的处理类库源码

    2. 日期时间操作:DateHelper类可能包含了处理日期时间的方法,如获取当前时间、日期之间的比较、时间间隔计算等。这有助于开发者在处理时间相关的业务时更加得心应手。 3. 文件系统交互:FileSystemHelper类通常...

    UDF文件系统资料汇总——协议、资料、源码

    UDF(Universal Disk Format)文件系统是一种开放标准的文件系统,广泛应用于光盘、移动硬盘、USB存储设备等,尤其在DVD和蓝光光盘上是标准的文件系统。这个压缩包包含了一系列与UDF相关的资源,如协议文档、结构...

    java常用工具类 Date日期、Mail邮件工具类

    DateHelper 类是 Date 日期工具类的一个实现,提供了多种方法来操作和格式化日期。 1. 日期格式化 DateHelper 类提供了多种方法来格式化日期,例如 formatDate 方法可以将 Date 对象格式化为指定的日期格式字符串...

    javascript日期转换 时间戳转日期格式

    JavaScript中的日期处理是编程中常见的任务之一,尤其是在创建动态用户界面或者进行数据分析时。这个话题主要涉及如何将时间戳转换为可读的日期格式。在JavaScript中,`Date`对象是处理日期和时间的主要工具。...

    distance_of_time_in_words:用Rails更好的时间间隔

    多蒂 ...include ActionView :: Helpers :: DateHelper include ActionView :: Helpers :: TextHelper include ActionView :: Helpers :: NumberHelper distance_of_time_in_words 以此为例: &gt;&gt; dis

    java 获取日期的几天前,几个月前和几年前的实例

    类`DateHelper`中包含了一些静态方法,如`toSeconds()`、`toMinutes()`、`toHours()`、`toDays()`、`toMonths()`和`toYears()`。这些方法将时间差(以毫秒为单位)转换为对应的秒、分、小时、天、月和年的整数。...

    C#中的扩展方法详解

    例如,在我们的示例中,我们创建了一个名为`DateHelper`的静态类,并在其中定义了一个名为`DateToString`的扩展方法,这个方法接收一个`DateTime`类型的参数并返回格式化的字符串。通过`this DateTime dt`,我们声明...

    naming-contest

    可以使用点号(.)分隔各个层级,如`myApp.utils.dateHelper`。 6. 避免使用保留字:JavaScript有一些保留字,如`for`、`if`等,这些不能用于变量或函数的命名。 7. 避免使用误导性的名字:名称应准确反映变量或...

Global site tag (gtag.js) - Google Analytics