Saturday, March 14, 2009

Common Utility methods

1.Convert from Date object to String with given format
/**
* Formats the date object in given format.
* @param aDate the date object
* @param aNewformat the new format
* @return string in new format.
*/
public static String getFormattedDate(Date aDate, String aNewformat)
{
if (aDate != null)
{
SimpleDateFormat dateFormat = new SimpleDateFormat(aNewformat);
String formattedDate = dateFormat.format((aDate));
return formattedDate;
}
return "";
}
2.Convert Calendar To String with given format
/**
* Formats the date object in given format.
* @param aCalendar the date object
* @param aNewformat the new format
* @return string in new format.
*/
public static String getFormattedDate(Calendar aCalendar, String aNewformat) {
//For Ex: aNewformat =dd/mm/yyyy
if (aCalendar != null)
{
SimpleDateFormat dateFormat = new SimpleDateFormat(aNewformat);
String formattedDate = dateFormat.format((aCalendar.getTime()));
return formattedDate;
}
return "";
}
3.Convert Calendar to Date
/**
* Checks calendar object for null and returns date object.
* @param aCalendar the calendar object to check.
* @return Date object or null
*/
public static Date getTime(Calendar aCalendar)
{
if (aCalendar != null)
{
return aCalendar.getTime();
}
return null;
}
You Can Find More on SimpleDateFormat click here

AddToAny

Contact Form

Name

Email *

Message *