JSF Marquee Component | Java Example | FlexiGrid using Servlet + JSON |

java.util.Date

leave a comment »

Add days to date

To subtract days,  pass  negative values to days parameter.
public static Date addDaytoDate(Date date, int days) throws NullPointerException {
if (date == null ) {
return date;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, days);
return cal.getTime();
}
Convert String to Date
public static Date convertStringDate(String strDate) {
Date date = null;
if (strDate != null) {
SimpleDateFormat fmt = new SimpleDateFormat(DATE_FORMAT);
try {
date = fmt.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
return date;
}
TimeStamp to Date and vice versa
public static Date convertTimestampToDate(Timestamp timeStmp) {
if (timeStmp == null) {
return new Date();
}
Date date = new Date(timeStmp.getTime());
return date;
}
/**
* Date to Timestamp conversion.
* @param date
* @return
*/
public static Timestamp convertDateToTimestamp(Date date) {
if (date == null) {
return getTimeStamp();
}
Timestamp timeStmp = new Timestamp(date.getTime());
return timeStmp;
}
/**
* timestamp to string coversion.
* @param timeStmp
* @return
*/
public static String convertTimestampToStr(Timestamp timeStmp) {
if (timeStmp == null) {
return “”;
}
SimpleDateFormat fmt = new SimpleDateFormat(DATE_FORMAT);
Date date = new Date(timeStmp.getTime());
return fmt.format(date);
}

Advertisements

Written by speedrace703

December 17, 2009 at 1:12 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: