import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateTimeUtil {
public static String getDateStr(String pattern,Date date){
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(date);
}
public static Date getDateFromStr(String pattern,String dateStr)throws Exception{
if(dateStr == null){
return null;
}
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.parse(dateStr);
}
public static Date addYear(Date date,int amount){
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.YEAR, amount);
return cal.getTime();
}
public static Date addMonth(Date date,int amount){
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.MONTH, amount);
return cal.getTime();
}
public static Date addDate(Date date,int amount){
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.DATE, amount);
return cal.getTime();
}
public static Date addHour(Date date,int amount){
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.HOUR, amount);
return cal.getTime();
}
public static Date addMinute(Date date,int amount){
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.MINUTE, amount);
return cal.getTime();
}
}