class="DateUtils" name="code">package com.common.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
/**
* The Class DateUtils.
*/
public final class DateUtils {
/**
* Instantiates a new date utils.
*/
private DateUtils() {
}
/**
* Parses the str to date.
*
* @param dateStr
* the date str
* @param pattern
* the pattern
* @return the date
* @throws ParseException
* the parse exception
*/
public static Date parseStrToDate(String dateStr, String pattern) throws ParseException {
Date result = null;
if (StringUtils.isNotEmpty(dateStr)) {
return new SimpleDateFormat(pattern).parse(dateStr);
}
return result;
}
/**
* Parses the date to str.
*
* @param date
* the date
* @param pattern
* the pattern
* @return the string
*/
public static String parseDateToStr(Date date, String pattern) {
String result = null;
if (date != null) {
return new SimpleDateFormat(pattern).format(date);
}
return result;
}
/**
* Parses the date pattern.
*
* @param sourceDate
* the source date
* @param fromPattern
* the pattern of sourceDate
* @param toPattern
* the pattern of return
* @return the string
* @throws ParseException
* the parse exception
*/
public static String parsePattern(String sourceDate, String fromPattern, String toPattern) throws ParseException {
String result = null;
if (StringUtils.isNotEmpty(sourceDate)) {
Date tempDate = parseStrToDate(sourceDate, fromPattern);
result = parseDateToStr(tempDate, toPattern);
}
return result;
}
}
?