时间函数详解编程语言

import java.text.DateFormat; 
import java.text.DateFormatSymbols; 
import java.text.FieldPosition; 
import java.text.Format; 
import java.text.ParseException; 
import java.text.ParsePosition; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Locale; 
import java.util.Map; 
import java.util.TimeZone; 
import com.icos.utility.string.StringUtil; 
import org.apache.log4j.Logger; 
public class DateUtils { 
/** 
* 常量空值 
*/ 
static final String EMPTY = ""; // 空值 
/** 
* 年月日时分秒 
*/ 
static final FastDateFormat ISO_DATETIME_FORMAT = FastDateFormat 
.getInstance("yyyy-MM-dd HH:mm:ss"); 
/** 
* 年月日时分秒毫秒 
*/ 
static final FastDateFormat ISO_DATETIME_TIME_ZONE_FORMAT = FastDateFormat 
.getInstance("yyyy-MM-dd HH:mm:ssZZ"); 
/** 
* 年月日 
*/ 
static final FastDateFormat ISO_DATE_FORMAT = FastDateFormat 
.getInstance("yyyy-MM-dd"); 
/** 
* 年月日 
*/ 
static final FastDateFormat ISODATEFORMAT = FastDateFormat 
.getInstance("yyyyMMdd"); 
/** 
* 年月日,毫秒 
*/ 
static final FastDateFormat ISO_DATE_TIME_ZONE_FORMAT = FastDateFormat 
.getInstance("yyyy-MM-ddZZ"); 
/** 
* 时分秒 
*/ 
static final FastDateFormat ISO_TIME_FORMAT = FastDateFormat 
.getInstance("HH:mm:ss"); 
/** 
* 时分秒,毫秒 
*/ 
static final FastDateFormat ISO_TIME_TIME_ZONE_FORMAT = FastDateFormat 
.getInstance("HH:mm:ssZZ"); 
/** 
* 时分秒 
*/ 
static final FastDateFormat ISO_TIME_NO_T_FORMAT = FastDateFormat 
.getInstance("HH:mm:ss"); 
/** 
* 时分秒,毫秒 
*/ 
static final FastDateFormat ISO_TIME_NO_T_TIME_ZONE_FORMAT = FastDateFormat 
.getInstance("HH:mm:ssZZ"); 
/** 
* 国际化的年月日,时分秒 
*/ 
static final FastDateFormat SMTP_DATETIME_FORMAT = FastDateFormat 
.getInstance("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US); 
static Logger logger = Logger.getLogger(DateUtils.class); 
/** 
* 功能描述: 根据毫秒数,和时间格式,得到一个时间字符串 
*  
* @param millis 
*            long类型的毫秒数 
* @param pattern 
*            字符串的时间格式 
* @return String 时间字符串 
* @author daixl 
* @since 2011.02.23 
*/ 
public static String format(long millis, String pattern) { 
return format(new Date(millis), pattern, null, null); 
} 
/** 
* 功能描述: 根据Date对象,和时间格式,得到一个时间字符串 
*  
* @param date 
*            一个Date对象 
* @param pattern 
*            字符串的时间格式 
* @return String 时间字符串 
* @author daixl 
* @since 2011.02.23 
*/ 
public static String format(Date date, String pattern) { 
return format(date, pattern, null, null); 
} 
/** 
* 功能描述: 根据Calendar对象,和时间格式,得到一个时间字符串 
*  
* @param calendar 
*            一个Calendar对象 
* @param pattern 
*            字符串的时间格式 
* @return String 时间字符串 
* @author daixl 
* @since 2011.02.23 
*/ 
public static String format(Calendar calendar, String pattern) { 
return format(calendar, pattern, null, null); 
} 
/** 
* 功能描述: 根据毫秒数,和时间格式,得到一个时间字符串 
*  
* @param millis 
*            long类型的毫秒数 
* @param pattern 
*            字符串的时间格式 
* @param timeZone 
*            TimeZone对象 
* @return String 时间字符串 
* @author daixl 
* @since 2011.02.23 
*/ 
public static String format(long millis, String pattern, TimeZone timeZone) { 
return format(new Date(millis), pattern, timeZone, null); 
} 
/** 
* 功能描述: 根据 Date 对象,和时间格式,得到一个时间字符串 
*  
* @param date 
*            一个Date对象 
* @param pattern 
*            字符串的时间格式 
* @param timeZone 
*            TimeZone对象 
* @return String 时间字符串 
* @since 2011.02.23 
*/ 
public static String format(Date date, String pattern, TimeZone timeZone) { 
return format(date, pattern, timeZone, null); 
} 
/** 
* 功能描述: 根据Calendar对象,和时间格式,得到一个时间字符串 
*  
* @param calendar 
*            一个Calendar对象 
* @param pattern 
*            字符串的时间格式 
* @param timeZone 
*            TimeZone对象 
* @return String 时间字符串 
* @author daixl 
* @since 2011.02.23 
*/ 
public static String format(Calendar calendar, String pattern, 
TimeZone timeZone) { 
return format(calendar, pattern, timeZone, null); 
} 
/** 
* 功能描述: 根据毫秒数,和时间格式,得到一个时间字符串 
*  
* @param millis 
*            long类型的毫秒数 
* @param pattern 
*            字符串的时间格式 
* @param locale 
*            locale对象 
* @return String 时间字符串 
* @author daixl 
* @since 2011.02.23 
*/ 
public static String format(long millis, String pattern, Locale locale) { 
return format(new Date(millis), pattern, null, locale); 
} 
/** 
* 功能描述: 根据 Date 对象,和时间格式,得到一个时间字符串 
*  
* @param pattern 
*            字符串的时间格式 
* @param locale 
*            Locale对象 
* @return String 时间字符串 
* @author daixl 
* @since 2011.02.23 
*/ 
public static String format(Date date, String pattern, Locale locale) { 
return format(date, pattern, null, locale); 
} 
/** 
* 功能描述: 根据 Calendar 对象,和时间格式,得到一个时间字符串 
*  
* @param calendar 
*            Calendar对象 
* @param pattern 
*            字符串的时间格式 
* @param locale 
*            Locale对象 
* @return String 时间字符串 
* @author daixl 
* @since 2011.02.23 
*/ 
public static String format(Calendar calendar, String pattern, Locale locale) { 
return format(calendar, pattern, null, locale); 
} 
/** 
* 功能描述: 根据毫秒数,和时间格式,得到一个时间字符串 
*  
* @param millis 
*            long类型的毫秒数 
* @param pattern 
*            字符串的时间格式 
* @param locale 
*            Locale对象 
* @param timeZone 
*            TimeZone对象 
* @return String 时间字符串 
* @author daixl 
* @since 2011.02.23 
*/ 
public static String format(long millis, String pattern, TimeZone timeZone, 
Locale locale) { 
return format(new Date(millis), pattern, timeZone, locale); 
} 
/** 
* 功能描述: 根据 Date 对象,和时间格式,得到一个时间字符串 
*  
* @param date 
*            Date对象 
* @param pattern 
*            字符串的时间格式 
* @param timeZone 
*            TimeZone对象 
* @param locale 
*            Locale对象 
* @return String 时间字符串 
* @author daixl 
* @since 2011.02.23 
*/ 
public static String format(Date date, String pattern, TimeZone timeZone, 
Locale locale) { 
FastDateFormat df = FastDateFormat.getInstance(pattern, timeZone, 
locale); 
return df.format(date); 
} 
/** 
* 功能描述: 根据 Date 对象,和时间格式,得到一个时间字符串 
*  
* @param calendar 
*            calendar对象 
* @param pattern 
*            字符串的时间格式 
* @param timeZone 
*            TimeZone对象 
* @param locale 
*            Locale对象 
* @return String 时间字符串 
* @author daixl 
* @since 2011.02.23 
*/ 
public static String format(Calendar calendar, String pattern, 
TimeZone timeZone, Locale locale) { 
FastDateFormat df = FastDateFormat.getInstance(pattern, timeZone, 
locale); 
return df.format(calendar); 
} 
/** 
* 功能描述:比较2个Date类型的时间是否是同一天 
*  
* @param date1 
*            第一个Date对象, 
* @param date2 
*            第二个Date对象 
* @return boolean 布尔值 
* @author daixl 
* @since 2011.02.24 
*/ 
public static boolean isSameDay(Date date1, Date date2) { 
if (date1 == null || date2 == null) { 
throw new IllegalArgumentException("The date must not be null"); 
} 
Calendar cal1 = Calendar.getInstance(); 
cal1.setTime(date1); 
Calendar cal2 = Calendar.getInstance(); 
cal2.setTime(date2); 
return isSameDay(cal1, cal2); 
} 
/** 
* 功能描述:比较2个Calendar类型的时间是否是同一天 
*  
* @param cal1 
*            第一个Calendar对象, 
* @param cal2 
*            第二个Calendar对象, 
* @return boolean 布尔值 
* @author daixl 
* @since 2011.02.24 
*/ 
public static boolean isSameDay(Calendar cal1, Calendar cal2) { 
if (cal1 == null || cal2 == null) { 
throw new IllegalArgumentException("The date must not be null"); 
} 
return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) 
&& cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1 
.get(Calendar.DAY_OF_YEAR) == cal2 
.get(Calendar.DAY_OF_YEAR)); 
} 
/** 
* 功能描述:比较2个Date类型的时间是否是同一个对象 
*  
* @param date1 
*            第一个Date对象, 
* @param date2 
*            第二个Date对象 
* @return boolean 布尔值 
* @author daixl 
* @since 2011.02.24 
*/ 
public static boolean isSameInstant(Date date1, Date date2) { 
if (date1 == null || date2 == null) { 
throw new IllegalArgumentException("The date must not be null"); 
} 
return date1.getTime() == date2.getTime(); 
} 
/** 
* 功能描述:比较2个Calendar类型的是否是同一个对象 
*  
* @param cal1 
*            第一个Calendar对象, 
* @param cal2 
*            第二个Calendar对象, 
* @return boolean 布尔值 
* @author daixl 
* @since 2011.02.24 
*/ 
public static boolean isSameInstant(Calendar cal1, Calendar cal2) { 
if (cal1 == null || cal2 == null) { 
throw new IllegalArgumentException("The date must not be null"); 
} 
return cal1.getTime().getTime() == cal2.getTime().getTime(); 
} 
/** 
* 功能描述:给定一个日期和一个数值,增加它的年 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @author daixl 
* @since 2011.02.24 
*/ 
public static Date addYears(Date date, int amount) { 
return add(date, Calendar.YEAR, amount); 
} 
/** 
* 功能描述:给定一个日期和一个数值,增加它的月 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @author daixl 
* @since 2011.02.24 
*/ 
public static Date addMonths(Date date, int amount) { 
return add(date, Calendar.MONTH, amount); 
} 
/** 
* 功能描述:给定一个日期和一个数值,增加它的星期 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @author daixl 
* @since 2011.02.24 
*/ 
public static Date addWeeks(Date date, int amount) { 
return add(date, Calendar.WEEK_OF_YEAR, amount); 
} 
/** 
* 功能描述:给定一个日期和一个数值,增加它的天 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @author daixl 
* @since 2011.02.24 
*/ 
public static Date addDays(Date date, int amount) { 
return add(date, Calendar.DAY_OF_MONTH, amount); 
} 
/** 
* 功能描述:给定一个日期和一个数值,增加它的小时 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @author daixl 
* @since 2011.02.24 
*/ 
public static Date addHours(Date date, int amount) { 
return add(date, Calendar.HOUR_OF_DAY, amount); 
} 
/** 
* 功能描述:给定一个日期和一个数值,增加它的分钟数 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @author daixl 
* @since 2011.02.24 
*/ 
public static Date addMinutes(Date date, int amount) { 
return add(date, Calendar.MINUTE, amount); 
} 
/** 
* 功能描述:给定一个日期和一个数值,增加它的秒数 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @author daixl 
* @since 2011.02.24 
*/ 
public static Date addSeconds(Date date, int amount) { 
return add(date, Calendar.SECOND, amount); 
} 
public static String getLastTimeString(int value, int count, String format) { 
long val = getTimeinteger() / value; 
val = (val - count) * value; 
return format(val, format); 
} 
/** 
* 功能描述:获得前一天的日期字符串 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @since 2016.04.07 
*/ 
public static String getLastTimeString(int count, String pattern) { 
String strDate = getYmdStr(getDateAfterDays(new Date(), count), pattern); 
return strDate; 
} 
/** 
* 功能描述:给定一个日期和一个数值,增加它的毫秒数 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @since 2011.02.24 
*/ 
public static Date addMilliseconds(Date date, int amount) { 
return add(date, Calendar.MILLISECOND, amount); 
} 
/** 
* 功能描述:给定一个日期和2个数值,返回计算后的日期 
*  
* @param date 
*            Date对象 
* @param calendarField 
*            int类型 
* @param amount 
*            int类型 
* @return Date Date对象 
* @since 2011.02.24 
*/ 
private static Date add(Date date, int calendarField, int amount) { 
if (date == null) { 
throw new IllegalArgumentException("The date must not be null"); 
} 
Calendar c = Calendar.getInstance(); 
c.setTime(date); 
c.add(calendarField, amount); 
return c.getTime(); 
} 
/** 
* @param value 
* @return 
*/ 
public static String getString(Object value) { 
if (value instanceof Date) { 
return ((Date) value).toString(); 
} else { 
return value == null ? "" : value.toString(); 
} 
} 
/** 
* 功能描述:给定一个日期和一个数值,修改它的年份 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
*/ 
public static Date setYears(Date date, int amount) { 
return set(date, Calendar.YEAR, amount); 
} 
/** 
* 功能描述:给定一个日期和一个数值,修改它的月份 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @author daixl 
* @since 2011.02.24 
*/ 
public static Date setMonths(Date date, int amount) { 
return set(date, Calendar.MONTH, amount); 
} 
/** 
* 功能描述:给定一个日期和一个数值,修改它的天 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
*/ 
public static Date setDays(Date date, int amount) { 
return set(date, Calendar.DAY_OF_MONTH, amount); 
} 
/** 
* 功能描述:给定一个日期和一个数值,修改它的下小时 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @author daixl 
* @since 2011.02.24 
*/ 
public static Date setHours(Date date, int amount) { 
return set(date, Calendar.HOUR_OF_DAY, amount); 
} 
/** 
* 功能描述:给定一个日期和一个数值,修改它的分钟 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @author daixl 
* @since 2011.02.24 
*/ 
public static Date setMinutes(Date date, int amount) { 
return set(date, Calendar.MINUTE, amount); 
} 
/** 
* 功能描述:给定一个日期和一个数值,修改它的秒 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @since 2011.02.24 
*/ 
public static Date setSeconds(Date date, int amount) { 
return set(date, Calendar.SECOND, amount); 
} 
/** 
* 功能描述:给定一个日期和一个数值,修改它的毫秒 
*  
* @param date 
*            Date对象 
* @param amount 
*            int类型 
* @return Date Date对象 
* @since 2011.02.24 
*/ 
public static Date setMilliseconds(Date date, int amount) { 
return set(date, Calendar.MILLISECOND, amount); 
} 
/** 
* 功能描述:给定一个日期和2个数值,返回修改后的Date 
*  
* @param date 
*            Date对象 
* @param calendarField 
*            int类型 
* @param amount 
*            int类型 
* @return Date Date对象 
* @since 2011.02.24 
*/ 
private static Date set(Date date, int calendarField, int amount) { 
if (date == null) { 
throw new IllegalArgumentException("The date must not be null"); 
} 
Calendar c = Calendar.getInstance(); 
c.setLenient(false); 
c.setTime(date); 
c.set(calendarField, amount); 
return c.getTime(); 
} 
/** 
* 功能描述:给定一个Date日期,返回一个Calendar对象 
*  
* @param date 
*            Date对象 
* @return Calendar Calendar对象 
* @since 2011.02.24 
*/ 
public static Calendar toCalendar(Date date) { 
Calendar c = Calendar.getInstance(); 
c.setTime(date); 
return c; 
} 
public static int getMinute() { 
return getMinute(currentDate()); 
} 
public static int getHour() { 
return getHour(currentDate()); 
} 
public static int getDay() { 
return getDay(currentDate()); 
} 
public static int getMonth() { 
return getMonth(currentDate()); 
} 
public static int getYear() { 
return getYear(currentDate()); 
} 
public static int getWeekOfYear(Date date) { 
Calendar cal = new GregorianCalendar(); 
cal.setTime(date); 
return (cal.get(GregorianCalendar.WEEK_OF_YEAR) + 1); 
} 
/** 
* 功能描述:给定一个日期,获得它的年 
*  
* @param date 
*            一个Date对象 
* @return String 返回一个String对象 
* @since 2011.02.24 
*/ 
public static int getYear(Date date) { 
Calendar cal = new GregorianCalendar(); 
cal.setTime(date); 
return cal.get(GregorianCalendar.YEAR); 
} 
/** 
* 功能描述:给定一个日期,获得它的月 
*  
* @param date 
*            一个Date对象 
* @return String 返回一个String对象 
* @since 2011.02.24 
*/ 
public static int getMonth(Date date) { 
Calendar cal = new GregorianCalendar(); 
cal.setTime(date); 
return (cal.get(GregorianCalendar.MONTH) + 1); 
} 
/** 
* 功能描述:给定一个日期,获得它的天 
*  
* @param date 
*            一个Date对象 
* @return String 返回一个String对象 
* @since 2011.02.24 
*/ 
public static int getDay(Date date) { 
Calendar cal = new GregorianCalendar(); 
cal.setTime(date); 
return cal.get(GregorianCalendar.DAY_OF_MONTH); 
} 
/** 
* 功能描述: 得到当前时间点 
*  
* @param 
* @return: int 
* @version: 2.0 
*/ 
public static int getHour(Date date) { 
Calendar c = Calendar.getInstance(); 
c.setTime(date); 
return c.get(Calendar.HOUR_OF_DAY); 
} 
/** 
* 功能描述:给定一个日期和一个年的数值,添加到这个日期年中,并返回 
*  
* @param date 
*            一个Date对象 
* @param years 
*            int类型 
* @return Date 返回一个Date对象 
*/ 
public static Date addYear(Date date, int years) { 
Calendar cal = new GregorianCalendar(); 
cal.setTime(date); 
cal.add(GregorianCalendar.YEAR, years); 
return cal.getTime(); 
} 
/** 
* 功能描述:给定一个日期和一个月的数值,添加到这个日期月中,并返回 
*  
* @param date 
*            一个Date对象 
* @param months 
*            int类型 
* @return Date 返回一个Date对象 
*/ 
public static Date addMonth(Date date, int months) { 
Calendar cal = new GregorianCalendar(); 
cal.setTime(date); 
cal.add(GregorianCalendar.MONTH, months); 
return cal.getTime(); 
} 
/** 
* 功能描述:给定一个日期和一个天的数值,添加到这个日期的天中,并返回 
*  
* @param date 
*            一个Date对象 
* @param day 
*            int类型 
* @return Date 返回一个Date对象 
*/ 
public static Date addDay(Date date, int day) { 
Calendar cal = new GregorianCalendar(); 
cal.setTime(date); 
cal.add(GregorianCalendar.DAY_OF_YEAR, day); 
return cal.getTime(); 
} 
public static String getNow(String pattern) { 
return format(new Date(), pattern); 
} 
/** 
* 功能描述:给定一个日期,判断它的星期,是否周末 
*  
* @param date 
*              Date对象 
* @return boolean  布尔值 
*/ 
public static boolean isSundayOrSaturday(Date date) { 
Calendar cal = new GregorianCalendar(); 
cal.setTime(date); 
int day = cal.get(GregorianCalendar.DAY_OF_WEEK); 
if (GregorianCalendar.SATURDAY == day 
|| GregorianCalendar.SUNDAY == day) { 
return true; 
} 
return false; 
} 
/** 
* 功能描述:给定字符串,转换成匹配格式的Date对象 
*  
* @param dateStr 
*              一个String对象 
* @return Date  一个Date对象 
* @since 2011.02.23 
*/ 
public static Date getDate(String dateStr) { 
if (StringUtil.isBlank(dateStr)) 
return null; 
dateStr = dateStr.trim(); 
SimpleDateFormat df = null; 
if (dateStr.length() > 19) { 
dateStr = dateStr.substring(0, 19); 
} 
if (dateStr.matches("[//d]{4}")) { 
df = new SimpleDateFormat("yyyy"); 
} else if (dateStr.matches("[//d]{4}-[//d]{1,2}")) { 
df = new SimpleDateFormat("yyyy-MM"); 
} else if (dateStr.matches("[//d]{4}-[//d]{1,2}-[//d]{1,2}")) { 
df = new SimpleDateFormat("yyyy-MM-dd"); 
} else if (dateStr 
.matches("[//d]{4}-[//d]{1,2}-[//d]{1,2}[//s]{1,}[//d]{1,2}")) { 
df = new SimpleDateFormat("yyyy-MM-dd HH"); 
} else if (dateStr 
.matches("[//d]{4}-[//d]{1,2}-[//d]{1,2}[//s]{1,}[//d]{1,2}:[//d]{1,2}")) { 
df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
} else if (dateStr 
.matches("[//d]{4}-[//d]{1,2}-[//d]{1,2}[//s]{1,}[//d]{1,2}:[//d]{1,2}:[//d]{1,2}")) { 
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
} else if (dateStr.matches("[//d]{1,2}-[//d]{1,2}[//s]{1,}[//d]{1,2}")) { 
df = new SimpleDateFormat("MM-dd HH"); 
} else { 
return null; 
} 
try { 
return df.parse(dateStr); 
} catch (ParseException e) { 
e.printStackTrace(); 
return null; 
} 
} 
public static boolean isDate(Object obj) { 
if (null == obj || !(obj instanceof Date)) { 
return false; 
} else { 
return true; 
} 
} 
/** 
* 获取当前时间对应于1970-01-01 00:00:00的秒值 
*  
* @return 对应于1970-01-01 00:00:00的秒值 
*/ 
public static Long getTimeinteger() { 
Calendar ca = Calendar.getInstance(); 
return ca.getTimeInMillis(); 
} 
public static Long getTimeinteger(Date date) { 
return date.getTime(); 
} 
public static Long getTimeinteger(String date) { 
return getDate(date).getTime(); 
} 
/** 
* 获得日期的long类型 
* */ 
public static Long getDateLong(int count, String format) { 
String strDate = getLastTimeString(count, format); 
Long longDate = getTimeinteger(strDate); 
return longDate; 
} 
/** 
* 生成与传入秒值对应的日期类型 
*  
* @param seconds 
* @return 
* @author James Cheung Date:Jul 25, 2012 
*/ 
public static Date integerToDate(Long seconds) { 
Long timeInMillis = seconds * 1000; 
Date date = new Date(timeInMillis); 
return date; 
} 
/* 
* 返回一个日期中的最后一天,当月最后一天 
*  
* @param date it's type must be Date 
*/ 
public static Date lastDay(Object date) { 
if (!isDate(date)) { 
logger.error("输入错误的类型,不能转换为日期"); 
} 
Date dateConver = (Date) date; 
Calendar calendar = new GregorianCalendar(); 
calendar.setTime(dateConver); 
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE)); 
return calendar.getTime(); 
} 
/* 
* 返回一个日期中的年份 
*  
* @param date it's type must be Date 
*/ 
public static int getYear(Object date) { 
if (!isDate(date)) { 
logger.error("输入错误的类型,不能转换为日期"); 
} 
Date dateConver = (Date) date; 
Calendar calendar = Calendar.getInstance(); 
calendar.setTime(dateConver); 
return calendar.get(Calendar.YEAR); 
} 
/* 
* 返回一个日期中的星期 
*  
* @param date it's type must be Date 
*/ 
public static int getWeek(Object date) { 
if (!isDate(date)) { 
logger.error("输入错误的类型,不能转换为日期"); 
} 
Date dateConver = (Date) date; 
Calendar calendar = Calendar.getInstance(); 
calendar.setTime(dateConver); 
int[] weeks = { 7, 1, 2, 3, 4, 5, 6 }; 
return weeks[calendar.get(Calendar.DAY_OF_WEEK) - 1]; 
} 
/* 
* 返回星期类型 
*  
* @param date it's type must be Date 
*  
* @param weekType the Week Type to be returned dayType如果为0返回整数,1返回中文,其他返回英文 
*/ 
public static String weekType(Object date, Object weekType) { 
if (!isDate(date)) { 
return null; 
} 
Date dateConver = (Date) date; 
Calendar calendar = Calendar.getInstance(); 
calendar.setTime(dateConver); 
String[] weeksNum = { "7", "1", "2", "3", "4", "5", "6" }; 
String[] weeksCn = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; 
String[] weeksEng = { "Sunday", "Monday", "Tuesday", "Wendsday", 
"Thursday", "Friday", "Satday" }; 
try { 
int weekTypes = (Integer) weekType; 
if (0 == weekTypes) { 
return weeksNum[calendar.get(Calendar.DAY_OF_WEEK) - 1]; 
} else if (1 == weekTypes) { 
return weeksCn[calendar.get(Calendar.DAY_OF_WEEK) - 1]; 
} else { 
return weeksEng[calendar.get(Calendar.DAY_OF_WEEK) - 1]; 
} 
} catch (Exception e) { 
return weeksEng[calendar.get(Calendar.DAY_OF_WEEK) - 1]; 
} 
} 
/* 
* 返回一当前机器中的日期 
*/ 
public static Date currentDate() { 
return new Date(); 
} 
public static String currentDateString(String fomat) { 
return format(new Date(), fomat); 
} 
/* 
* 返回一个日期时间中的小时数 
*  
* @param date it's type must be Date 
*/ 
public static int getHour(Object date) { 
if (!isDate(date)) { 
logger.error("输入错误的类型,不能转换为日期"); 
} 
Date dateConvert = (Date) date; 
Calendar calendar = Calendar.getInstance(); 
calendar.setTime(dateConvert); 
return calendar.get(Calendar.HOUR_OF_DAY); 
} 
/* 
* 返回一个日期时间中的分钟数 
*  
* @param date it's type must be Date 
*/ 
public static int getMinute(Object date) { 
if (!isDate(date)) { 
logger.error("输入错误的类型,不能转换为日期"); 
} 
Date dateConvert = (Date) date; 
Calendar calendar = Calendar.getInstance(); 
calendar.setTime(dateConvert); 
return calendar.get(Calendar.MINUTE); 
} 
/* 
* 返回一个日期时间中的秒钟 
*  
* @param date it's type must be Date 
*/ 
public static int getSecond(Object date) { 
if (!isDate(date)) { 
logger.error("输入错误的类型,不能转换为日期"); 
} 
Date dateConvert = (Date) date; 
Calendar calendar = Calendar.getInstance(); 
calendar.setTime(dateConvert); 
return calendar.get(Calendar.SECOND); 
} 
/* 
* 返回一个日期中的月份 
*  
* @param date it's type must be Date 
*/ 
public static int getMonth(Object date) { 
if (!isDate(date)) { 
logger.error("输入错误的类型,不能转换为日期"); 
} 
Date dateConvert = (Date) date; 
Calendar calendar = Calendar.getInstance(); 
calendar.setTime(dateConvert); 
return calendar.get(Calendar.MONTH) + 1; 
} 
/* 
* 返回一个日期中的天数 
*  
* @param date it's type must be Date 
*/ 
public static int getDay(Object date) { 
if (!isDate(date)) { 
logger.error("输入错误的类型,不能转换为日期"); 
} 
Date dateConvert = (Date) date; 
Calendar calendar = Calendar.getInstance(); 
calendar.setTime(dateConvert); 
return calendar.get(Calendar.DATE); 
} 
/* 
* 返回两个日期相差的天数 
*  
* @param dateStart it's type must be Date 
*  
* @param dateEnd it's type must be Date 
*/ 
public static int daysBetweenTwoDate(Object dateStart, Object dateEnd) { 
if (!isDate(dateStart) || !isDate(dateEnd)) { 
logger.error("输入错误的类型,不能转换为日期"); 
} 
Date dateStartConver = (Date) dateStart; 
Date dateEndConver = (Date) dateEnd; 
long millSeconds = dateEndConver.getTime() - dateStartConver.getTime(); 
int res = (int) (millSeconds % (24 * 60 * 60 * 1000)); 
return res == 0 ? (int) (millSeconds / (24 * 60 * 60 * 1000)) 
: ((int) (millSeconds / (24 * 60 * 60 * 1000) + 1)); 
} 
static class FastDateFormat extends Format { 
/** 
* Required for serialization support. 
*  
* @see java.io.Serializable 
*/ 
private static final long serialVersionUID = 1L; 
/** 
* FULL locale dependent date or time style. 
*/ 
public static final int FULL = DateFormat.FULL; 
/** 
* LONG locale dependent date or time style. 
*/ 
public static final int LONG = DateFormat.LONG; 
/** 
* MEDIUM locale dependent date or time style. 
*/ 
public static final int MEDIUM = DateFormat.MEDIUM; 
/** 
* SHORT locale dependent date or time style. 
*/ 
public static final int SHORT = DateFormat.SHORT; 
/** 
* The c default pattern. 
*/ 
private static String cDefaultPattern; // lazily initialised by 
// getInstance() 
/** 
* The Constant cInstanceCache. 
*/ 
private static final Map<FastDateFormat, FastDateFormat> cInstanceCache = new HashMap<FastDateFormat, FastDateFormat>( 
7); 
/** 
* The Constant cDateInstanceCache. 
*/ 
private static final Map<Object, FastDateFormat> cDateInstanceCache = new HashMap<Object, FastDateFormat>( 
7); 
/** 
* The Constant cTimeInstanceCache. 
*/ 
private static final Map<Object, FastDateFormat> cTimeInstanceCache = new HashMap<Object, FastDateFormat>( 
7); 
/** 
* The Constant cDateTimeInstanceCache. 
*/ 
private static final Map<Object, FastDateFormat> cDateTimeInstanceCache = new HashMap<Object, FastDateFormat>( 
7); 
/** 
* The Constant cTimeZoneDisplayCache. 
*/ 
private static final Map<Object, String> cTimeZoneDisplayCache = new HashMap<Object, String>( 
7); 
/** 
* The pattern. 
*/ 
private final String mPattern; 
/** 
* The time zone. 
*/ 
private final TimeZone mTimeZone; 
/** 
* Whether the time zone overrides any on Calendars. 
*/ 
private final boolean mTimeZoneForced; 
/** 
* The locale. 
*/ 
private final Locale mLocale; 
/** 
* Whether the locale overrides the default. 
*/ 
private final boolean mLocaleForced; 
/** 
* The parsed rules. 
*/ 
private transient Rule[] mRules; 
/** 
* The estimated maximum length. 
*/ 
private transient int mMaxLengthEstimate; 
/** 
* Gets the single instance of FastDateFormat. 
*  
* @return single instance of FastDateFormat 
*/ 
public static FastDateFormat getInstance() { 
return getInstance(getDefaultPattern(), null, null); 
} 
/** 
* Gets the single instance of FastDateFormat. 
*  
* @param pattern 
*            the pattern 
* @return single instance of FastDateFormat 
*/ 
public static FastDateFormat getInstance(String pattern) { 
return getInstance(pattern, null, null); 
} 
/** 
* Gets the single instance of FastDateFormat. 
*  
* @param pattern 
*            the pattern 
* @param timeZone 
*            the time zone 
* @return single instance of FastDateFormat 
*/ 
public static FastDateFormat getInstance(String pattern, 
TimeZone timeZone) { 
return getInstance(pattern, timeZone, null); 
} 
/** 
* Gets the single instance of FastDateFormat. 
*  
* @param pattern 
*            the pattern 
* @param locale 
*            the locale 
* @return single instance of FastDateFormat 
*/ 
public static FastDateFormat getInstance(String pattern, Locale locale) { 
return getInstance(pattern, null, locale); 
} 
/** 
* Gets the single instance of FastDateFormat. 
*  
* @param pattern 
*            the pattern 
* @param timeZone 
*            the time zone 
* @param locale 
*            the locale 
* @return single instance of FastDateFormat 
*/ 
public static synchronized FastDateFormat getInstance(String pattern, 
TimeZone timeZone, Locale locale) { 
FastDateFormat emptyFormat = new FastDateFormat(pattern, timeZone, 
locale); 
FastDateFormat format = cInstanceCache.get(emptyFormat); 
if (format == null) { 
format = emptyFormat; 
format.init(); // convert shell format into usable one 
cInstanceCache.put(format, format); // this is OK! 
} 
return format; 
} 
/** 
* Gets the date instance. 
*  
* @param style 
*            the style 
* @return the date instance 
*/ 
public static FastDateFormat getDateInstance(int style) { 
return getDateInstance(style, null, null); 
} 
/** 
* Gets the date instance. 
*  
* @param style 
*            the style 
* @param locale 
*            the locale 
* @return the date instance 
*/ 
public static FastDateFormat getDateInstance(int style, Locale locale) { 
return getDateInstance(style, null, locale); 
} 
/** 
* Gets the date instance. 
*  
* @param style 
*            the style 
* @param timeZone 
*            the time zone 
* @return the date instance 
*/ 
public static FastDateFormat getDateInstance(int style, 
TimeZone timeZone) { 
return getDateInstance(style, timeZone, null); 
} 
/** 
* Gets the date instance. 
*  
* @param style 
*            the style 
* @param timeZone 
*            the time zone 
* @param locale 
*            the locale 
* @return the date instance 
*/ 
public static synchronized FastDateFormat getDateInstance(int style, 
TimeZone timeZone, Locale locale) { 
Object key = Integer.valueOf(style); 
if (timeZone != null) { 
key = new Pair(key, timeZone); 
} 
if (locale == null) { 
locale = Locale.getDefault(); 
} 
key = new Pair(key, locale); 
FastDateFormat format = cDateInstanceCache.get(key); 
if (format == null) { 
try { 
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat 
.getDateInstance(style, locale); 
String pattern = formatter.toPattern(); 
format = getInstance(pattern, timeZone, locale); 
cDateInstanceCache.put(key, format); 
} catch (ClassCastException ex) { 
throw new IllegalArgumentException( 
"No date pattern for locale: " + locale); 
} 
} 
return format; 
} 
/** 
* Gets the time instance. 
*  
* @param style 
*            the style 
* @return the time instance 
*/ 
public static FastDateFormat getTimeInstance(int style) { 
return getTimeInstance(style, null, null); 
} 
/** 
* Gets the time instance. 
*  
* @param style 
*            the style 
* @param locale 
*            the locale 
* @return the time instance 
*/ 
public static FastDateFormat getTimeInstance(int style, Locale locale) { 
return getTimeInstance(style, null, locale); 
} 
/** 
* Gets the time instance. 
*  
* @param style 
*            the style 
* @param timeZone 
*            the time zone 
* @return the time instance 
*/ 
public static FastDateFormat getTimeInstance(int style, 
TimeZone timeZone) { 
return getTimeInstance(style, timeZone, null); 
} 
/** 
* Gets the time instance. 
*  
* @param style 
*            the style 
* @param timeZone 
*            the time zone 
* @param locale 
*            the locale 
* @return the time instance 
*/ 
public static synchronized FastDateFormat getTimeInstance(int style, 
TimeZone timeZone, Locale locale) { 
Object key = Integer.valueOf(style); 
if (timeZone != null) { 
key = new Pair(key, timeZone); 
} 
if (locale != null) { 
key = new Pair(key, locale); 
} 
FastDateFormat format = cTimeInstanceCache.get(key); 
if (format == null) { 
if (locale == null) { 
locale = Locale.getDefault(); 
} 
try { 
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat 
.getTimeInstance(style, locale); 
String pattern = formatter.toPattern(); 
format = getInstance(pattern, timeZone, locale); 
cTimeInstanceCache.put(key, format); 
} catch (ClassCastException ex) { 
throw new IllegalArgumentException( 
"No date pattern for locale: " + locale); 
} 
} 
return format; 
} 
/** 
* Gets the date time instance. 
*  
* @param dateStyle 
*            the date style 
* @param timeStyle 
*            the time style 
* @return the date time instance 
*/ 
public static FastDateFormat getDateTimeInstance(int dateStyle, 
int timeStyle) { 
return getDateTimeInstance(dateStyle, timeStyle, null, null); 
} 
/** 
* Gets the date time instance. 
*  
* @param dateStyle 
*            the date style 
* @param timeStyle 
*            the time style 
* @param locale 
*            the locale 
* @return the date time instance 
*/ 
public static FastDateFormat getDateTimeInstance(int dateStyle, 
int timeStyle, Locale locale) { 
return getDateTimeInstance(dateStyle, timeStyle, null, locale); 
} 
/** 
* Gets the date time instance. 
*  
* @param dateStyle 
*            the date style 
* @param timeStyle 
*            the time style 
* @param timeZone 
*            the time zone 
* @return the date time instance 
*/ 
public static FastDateFormat getDateTimeInstance(int dateStyle, 
int timeStyle, TimeZone timeZone) { 
return getDateTimeInstance(dateStyle, timeStyle, timeZone, null); 
} 
/** 
* Gets the date time instance. 
*  
* @param dateStyle 
*            the date style 
* @param timeStyle 
*            the time style 
* @param timeZone 
*            the time zone 
* @param locale 
*            the locale 
* @return the date time instance 
*/ 
public static synchronized FastDateFormat getDateTimeInstance( 
int dateStyle, int timeStyle, TimeZone timeZone, Locale locale) { 
Object key = new Pair(Integer.valueOf(dateStyle), 
Integer.valueOf(timeStyle)); 
if (timeZone != null) { 
key = new Pair(key, timeZone); 
} 
if (locale == null) { 
locale = Locale.getDefault(); 
} 
key = new Pair(key, locale); 
FastDateFormat format = cDateTimeInstanceCache.get(key); 
if (format == null) { 
try { 
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat 
.getDateTimeInstance(dateStyle, timeStyle, locale); 
String pattern = formatter.toPattern(); 
format = getInstance(pattern, timeZone, locale); 
cDateTimeInstanceCache.put(key, format); 
} catch (ClassCastException ex) { 
throw new IllegalArgumentException( 
"No date time pattern for locale: " + locale); 
} 
} 
return format; 
} 
/** 
* Gets the time zone display. 
*  
* @param tz 
*            the tz 
* @param daylight 
*            the daylight 
* @param style 
*            the style 
* @param locale 
*            the locale 
* @return the time zone display 
*/ 
static synchronized String getTimeZoneDisplay(TimeZone tz, 
boolean daylight, int style, Locale locale) { 
Object key = new TimeZoneDisplayKey(tz, daylight, style, locale); 
String value = cTimeZoneDisplayCache.get(key); 
if (value == null) { 
value = tz.getDisplayName(daylight, style, locale); 
cTimeZoneDisplayCache.put(key, value); 
} 
return value; 
} 
/** 
* <p> 
* Gets the default pattern. 
* </p> 
*  
* @return the default pattern 
*/ 
private static synchronized String getDefaultPattern() { 
if (cDefaultPattern == null) { 
cDefaultPattern = new SimpleDateFormat().toPattern(); 
} 
return cDefaultPattern; 
} 
/** 
* Instantiates a new fast date format. 
*  
* @param pattern 
*            the pattern 
* @param timeZone 
*            the time zone 
* @param locale 
*            the locale 
*/ 
protected FastDateFormat(String pattern, TimeZone timeZone, 
Locale locale) { 
super(); 
if (pattern == null) { 
throw new IllegalArgumentException( 
"The pattern must not be null"); 
} 
mPattern = pattern; 
mTimeZoneForced = (timeZone != null); 
if (timeZone == null) { 
timeZone = TimeZone.getDefault(); 
} 
mTimeZone = timeZone; 
mLocaleForced = (locale != null); 
if (locale == null) { 
locale = Locale.getDefault(); 
} 
mLocale = locale; 
} 
/** 
* Inits the. 
*/ 
protected void init() { 
List<Rule> rulesList = parsePattern(); 
mRules = rulesList.toArray(new Rule[rulesList.size()]); 
int len = 0; 
for (int i = mRules.length; --i >= 0;) { 
len += mRules[i].estimateLength(); 
} 
mMaxLengthEstimate = len; 
} 
/** 
* Parses the pattern. 
*  
* @return the list 
*/ 
protected List<Rule> parsePattern() { 
DateFormatSymbols symbols = new DateFormatSymbols(mLocale); 
List<Rule> rules = new ArrayList<Rule>(); 
String[] ERAs = symbols.getEras(); 
String[] months = symbols.getMonths(); 
String[] shortMonths = symbols.getShortMonths(); 
String[] weekdays = symbols.getWeekdays(); 
String[] shortWeekdays = symbols.getShortWeekdays(); 
String[] AmPmStrings = symbols.getAmPmStrings(); 
int length = mPattern.length(); 
int[] indexRef = new int[1]; 
for (int i = 0; i < length; i++) { 
indexRef[0] = i; 
String token = parseToken(mPattern, indexRef); 
i = indexRef[0]; 
int tokenLen = token.length(); 
if (tokenLen == 0) { 
break; 
} 
Rule rule; 
char c = token.charAt(0); 
switch (c) { 
case 'G': // era designator (text) 
rule = new TextField(Calendar.ERA, ERAs); 
break; 
case 'y': // year (number) 
if (tokenLen >= 4) { 
rule = selectNumberRule(Calendar.YEAR, tokenLen); 
} else { 
rule = TwoDigitYearField.INSTANCE; 
} 
break; 
case 'M': // month in year (text and number) 
if (tokenLen >= 4) { 
rule = new TextField(Calendar.MONTH, months); 
} else if (tokenLen == 3) { 
rule = new TextField(Calendar.MONTH, shortMonths); 
} else if (tokenLen == 2) { 
rule = TwoDigitMonthField.INSTANCE; 
} else { 
rule = UnpaddedMonthField.INSTANCE; 
} 
break; 
case 'd': // day in month (number) 
rule = selectNumberRule(Calendar.DAY_OF_MONTH, tokenLen); 
break; 
case 'h': // hour in am/pm (number, 1..12) 
rule = new TwelveHourField(selectNumberRule(Calendar.HOUR, 
tokenLen)); 
break; 
case 'H': // hour in day (number, 0..23) 
rule = selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen); 
break; 
case 'm': // minute in hour (number) 
rule = selectNumberRule(Calendar.MINUTE, tokenLen); 
break; 
case 's': // second in minute (number) 
rule = selectNumberRule(Calendar.SECOND, tokenLen); 
break; 
case 'S': // millisecond (number) 
rule = selectNumberRule(Calendar.MILLISECOND, tokenLen); 
break; 
case 'E': // day in week (text) 
rule = new TextField(Calendar.DAY_OF_WEEK, 
tokenLen < 4 ? shortWeekdays : weekdays); 
break; 
case 'D': // day in year (number) 
rule = selectNumberRule(Calendar.DAY_OF_YEAR, tokenLen); 
break; 
case 'F': // day of week in month (number) 
rule = selectNumberRule(Calendar.DAY_OF_WEEK_IN_MONTH, 
tokenLen); 
break; 
case 'w': // week in year (number) 
rule = selectNumberRule(Calendar.WEEK_OF_YEAR, tokenLen); 
break; 
case 'W': // week in month (number) 
rule = selectNumberRule(Calendar.WEEK_OF_MONTH, tokenLen); 
break; 
case 'a': // am/pm marker (text) 
rule = new TextField(Calendar.AM_PM, AmPmStrings); 
break; 
case 'k': // hour in day (1..24) 
rule = new TwentyFourHourField(selectNumberRule( 
Calendar.HOUR_OF_DAY, tokenLen)); 
break; 
case 'K': // hour in am/pm (0..11) 
rule = selectNumberRule(Calendar.HOUR, tokenLen); 
break; 
case 'z': // time zone (text) 
if (tokenLen >= 4) { 
rule = new TimeZoneNameRule(mTimeZone, mTimeZoneForced, 
mLocale, TimeZone.LONG); 
} else { 
rule = new TimeZoneNameRule(mTimeZone, mTimeZoneForced, 
mLocale, TimeZone.SHORT); 
} 
break; 
case 'Z': // time zone (value) 
if (tokenLen == 1) { 
rule = TimeZoneNumberRule.INSTANCE_NO_COLON; 
} else { 
rule = TimeZoneNumberRule.INSTANCE_COLON; 
} 
break; 
case '/'': // literal text 
String sub = token.substring(1); 
if (sub.length() == 1) { 
rule = new CharacterLiteral(sub.charAt(0)); 
} else { 
rule = new StringLiteral(sub); 
} 
break; 
default: 
throw new IllegalArgumentException( 
"Illegal pattern component: " + token); 
} 
rules.add(rule); 
} 
return rules; 
} 
/** 
* Parses the token. 
*  
* @param pattern 
*            the pattern 
* @param indexRef 
*            the index ref 
* @return the string 
*/ 
protected String parseToken(String pattern, int[] indexRef) { 
StringBuilder buf = new StringBuilder(); 
int i = indexRef[0]; 
int length = pattern.length(); 
char c = pattern.charAt(i); 
if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') { 
// Scan a run of the same character, which indicates a time 
// pattern. 
buf.append(c); 
while (i + 1 < length) { 
char peek = pattern.charAt(i + 1); 
if (peek == c) { 
buf.append(c); 
i++; 
} else { 
break; 
} 
} 
} else { 
// This will identify token as text. 
buf.append('/''); 
boolean inLiteral = false; 
for (; i < length; i++) { 
c = pattern.charAt(i); 
if (c == '/'') { 
if (i + 1 < length && pattern.charAt(i + 1) == '/'') { 
// '' is treated as escaped ' 
i++; 
buf.append(c); 
} else { 
inLiteral = !inLiteral; 
} 
} else if (!inLiteral 
&& (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z')) { 
i--; 
break; 
} else { 
buf.append(c); 
} 
} 
} 
indexRef[0] = i; 
return buf.toString(); 
} 
/** 
* Select number rule. 
*  
* @param field 
*            the field 
* @param padding 
*            the padding 
* @return the number rule 
*/ 
protected NumberRule selectNumberRule(int field, int padding) { 
switch (padding) { 
case 1: 
return new UnpaddedNumberField(field); 
case 2: 
return new TwoDigitNumberField(field); 
default: 
return new PaddedNumberField(field, padding); 
} 
} 
/** 
* Format. 
*  
* @param millis 
*            the millis 
* @return the string 
*/ 
public String format(long millis) { 
return format(new Date(millis)); 
} 
/** 
* Format. 
*  
* @param date 
*            the date 
* @return the string 
*/ 
public String format(Date date) { 
Calendar c = new GregorianCalendar(mTimeZone); 
c.setTime(date); 
return applyRules(c, new StringBuffer(mMaxLengthEstimate)) 
.toString(); 
} 
/** 
* Format. 
*  
* @param calendar 
*            the calendar 
* @return the string 
*/ 
public String format(Calendar calendar) { 
return format(calendar, new StringBuffer(mMaxLengthEstimate)) 
.toString(); 
} 
/** 
* Format. 
*  
* @param millis 
*            the millis 
* @param buf 
*            the buf 
* @return the string buffer 
*/ 
public StringBuffer format(long millis, StringBuffer buf) { 
return format(new Date(millis), buf); 
} 
/** 
* Format. 
*  
* @param date 
*            the date 
* @param buf 
*            the buf 
* @return the string buffer 
*/ 
public StringBuffer format(Date date, StringBuffer buf) { 
Calendar c = new GregorianCalendar(mTimeZone); 
c.setTime(date); 
return applyRules(c, buf); 
} 
/** 
* Format. 
*  
* @param calendar 
*            the calendar 
* @param buf 
*            the buf 
* @return the string buffer 
*/ 
public StringBuffer format(Calendar calendar, StringBuffer buf) { 
if (mTimeZoneForced) { 
calendar.getTimeInMillis(); // / LANG-538 
calendar = (Calendar) calendar.clone(); 
calendar.setTimeZone(mTimeZone); 
} 
return applyRules(calendar, buf); 
} 
/** 
* Apply rules. 
*  
* @param calendar 
*            the calendar 
* @param buf 
*            the buf 
* @return the string buffer 
*/ 
protected StringBuffer applyRules(Calendar calendar, StringBuffer buf) { 
Rule[] rules = mRules; 
int len = mRules.length; 
for (int i = 0; i < len; i++) { 
rules[i].appendTo(buf, calendar); 
} 
return buf; 
} 
@Override 
public Object parseObject(String source, ParsePosition pos) { 
pos.setIndex(0); 
pos.setErrorIndex(0); 
return null; 
} 
/** 
* Gets the pattern. 
*  
* @return the pattern 
*/ 
public String getPattern() { 
return mPattern; 
} 
/** 
* Gets the time zone. 
*  
* @return the time zone 
*/ 
public TimeZone getTimeZone() { 
return mTimeZone; 
} 
/** 
* Gets the time zone overrides calendar. 
*  
* @return the time zone overrides calendar 
*/ 
public boolean getTimeZoneOverridesCalendar() { 
return mTimeZoneForced; 
} 
/** 
* Gets the locale. 
*  
* @return the locale 
*/ 
public Locale getLocale() { 
return mLocale; 
} 
/** 
* Gets the max length estimate. 
*  
* @return the max length estimate 
*/ 
public int getMaxLengthEstimate() { 
return mMaxLengthEstimate; 
} 
@Override 
public boolean equals(Object obj) { 
if (obj instanceof FastDateFormat == false) { 
return false; 
} 
FastDateFormat other = (FastDateFormat) obj; 
if ((mPattern == other.mPattern || mPattern.equals(other.mPattern)) 
&& (mTimeZone == other.mTimeZone || mTimeZone 
.equals(other.mTimeZone)) 
&& (mLocale == other.mLocale || mLocale 
.equals(other.mLocale)) 
&& (mTimeZoneForced == other.mTimeZoneForced) 
&& (mLocaleForced == other.mLocaleForced)) { 
return true; 
} 
return false; 
} 
@Override 
public int hashCode() { 
int total = 0; 
total += mPattern.hashCode(); 
total += mTimeZone.hashCode(); 
total += (mTimeZoneForced ? 1 : 0); 
total += mLocale.hashCode(); 
total += (mLocaleForced ? 1 : 0); 
return total; 
} 
@Override 
public String toString() { 
return "FastDateFormat[" + mPattern + "]"; 
} 
/** 
* The Interface Rule. 
*/ 
private interface Rule { 
/** 
* Estimate length. 
*  
* @return the int 
*/ 
int estimateLength(); 
/** 
* Append to. 
*  
* @param buffer 
*            the buffer 
* @param calendar 
*            the calendar 
*/ 
void appendTo(StringBuffer buffer, Calendar calendar); 
} 
/** 
* The Interface NumberRule. 
*/ 
private interface NumberRule extends Rule { 
/** 
* Append to. 
*  
* @param buffer 
*            the buffer 
* @param value 
*            the value 
*/ 
void appendTo(StringBuffer buffer, int value); 
} 
/** 
* The Class CharacterLiteral. 
*/ 
private static class CharacterLiteral implements Rule { 
/** 
* The m value. 
*/ 
private final char mValue; 
/** 
* Instantiates a new character literal. 
*  
* @param value 
*            the value 
*/ 
CharacterLiteral(char value) { 
mValue = value; 
} 
public int estimateLength() { 
return 1; 
} 
public void appendTo(StringBuffer buffer, Calendar calendar) { 
buffer.append(mValue); 
} 
} 
/** 
* The Class StringLiteral. 
*/ 
private static class StringLiteral implements Rule { 
/** 
* The m value. 
*/ 
private final String mValue; 
/** 
* Instantiates a new string literal. 
*  
* @param value 
*            the value 
*/ 
StringLiteral(String value) { 
mValue = value; 
} 
public int estimateLength() { 
return mValue.length(); 
} 
public void appendTo(StringBuffer buffer, Calendar calendar) { 
buffer.append(mValue); 
} 
} 
/** 
* The Class TextField. 
*/ 
private static class TextField implements Rule { 
/** 
* The m field. 
*/ 
private final int mField; 
/** 
* The m values. 
*/ 
private final String[] mValues; 
/** 
* Instantiates a new text field. 
*  
* @param field 
*            the field 
* @param values 
*            the values 
*/ 
TextField(int field, String[] values) { 
mField = field; 
mValues = values; 
} 
public int estimateLength() { 
int max = 0; 
for (int i = mValues.length; --i >= 0;) { 
int len = mValues[i].length(); 
if (len > max) { 
max = len; 
} 
} 
return max; 
} 
public void appendTo(StringBuffer buffer, Calendar calendar) { 
buffer.append(mValues[calendar.get(mField)]); 
} 
} 
/** 
* The Class UnpaddedNumberField. 
*/ 
private static class UnpaddedNumberField implements NumberRule { 
/** 
* The m field. 
*/ 
private final int mField; 
/** 
* Instantiates a new unpadded number field. 
*  
* @param field 
*            the field 
*/ 
UnpaddedNumberField(int field) { 
mField = field; 
} 
public int estimateLength() { 
return 4; 
} 
public void appendTo(StringBuffer buffer, Calendar calendar) { 
appendTo(buffer, calendar.get(mField)); 
} 
public final void appendTo(StringBuffer buffer, int value) { 
if (value < 10) { 
buffer.append((char) (value + '0')); 
} else if (value < 100) { 
buffer.append((char) (value / 10 + '0')); 
buffer.append((char) (value % 10 + '0')); 
} else { 
buffer.append(Integer.toString(value)); 
} 
} 
} 
/** 
* The Class UnpaddedMonthField. 
*/ 
private static class UnpaddedMonthField implements NumberRule { 
/** 
* The Constant INSTANCE. 
*/ 
static final UnpaddedMonthField INSTANCE = new UnpaddedMonthField(); 
/** 
* Instantiates a new unpadded month field. 
*/ 
UnpaddedMonthField() { 
super(); 
} 
public int estimateLength() { 
return 2; 
} 
public void appendTo(StringBuffer buffer, Calendar calendar) { 
appendTo(buffer, calendar.get(Calendar.MONTH) + 1); 
} 
public final void appendTo(StringBuffer buffer, int value) { 
if (value < 10) { 
buffer.append((char) (value + '0')); 
} else { 
buffer.append((char) (value / 10 + '0')); 
buffer.append((char) (value % 10 + '0')); 
} 
} 
} 
/** 
* The Class PaddedNumberField. 
*/ 
private static class PaddedNumberField implements NumberRule { 
/** 
* The m field. 
*/ 
private final int mField; 
/** 
* The m size. 
*/ 
private final int mSize; 
/** 
* Instantiates a new padded number field. 
*  
* @param field 
*            the field 
* @param size 
*            the size 
*/ 
PaddedNumberField(int field, int size) { 
if (size < 3) { 
throw new IllegalArgumentException(); 
} 
mField = field; 
mSize = size; 
} 
public int estimateLength() { 
return 4; 
} 
public void appendTo(StringBuffer buffer, Calendar calendar) { 
appendTo(buffer, calendar.get(mField)); 
} 
public final void appendTo(StringBuffer buffer, int value) { 
if (value < 100) { 
for (int i = mSize; --i >= 2;) { 
buffer.append('0'); 
} 
buffer.append((char) (value / 10 + '0')); 
buffer.append((char) (value % 10 + '0')); 
} else { 
int digits; 
if (value < 1000) { 
digits = 3; 
} else { 
if (!(value > -1)) { 
throw new IllegalArgumentException(String.format( 
"Negative values should not be possible", 
Long.valueOf(value))); 
} 
digits = Integer.toString(value).length(); 
} 
for (int i = mSize; --i >= digits;) { 
buffer.append('0'); 
} 
buffer.append(Integer.toString(value)); 
} 
} 
} 
/** 
* The Class TwoDigitNumberField. 
*/ 
private static class TwoDigitNumberField implements NumberRule { 
/** 
* The m field. 
*/ 
private final int mField; 
/** 
* Instantiates a new two digit number field. 
*  
* @param field 
*            the field 
*/ 
TwoDigitNumberField(int field) { 
mField = field; 
} 
public int estimateLength() { 
return 2; 
} 
public void appendTo(StringBuffer buffer, Calendar calendar) { 
appendTo(buffer, calendar.get(mField)); 
} 
public final void appendTo(StringBuffer buffer, int value) { 
if (value < 100) { 
buffer.append((char) (value / 10 + '0')); 
buffer.append((char) (value % 10 + '0')); 
} else { 
buffer.append(Integer.toString(value)); 
} 
} 
} 
/** 
* The Class TwoDigitYearField. 
*/ 
private static class TwoDigitYearField implements NumberRule { 
/** 
* The Constant INSTANCE. 
*/ 
static final TwoDigitYearField INSTANCE = new TwoDigitYearField(); 
/** 
* Instantiates a new two digit year field. 
*/ 
TwoDigitYearField() { 
super(); 
} 
public int estimateLength() { 
return 2; 
} 
public void appendTo(StringBuffer buffer, Calendar calendar) { 
appendTo(buffer, calendar.get(Calendar.YEAR) % 100); 
} 
public final void appendTo(StringBuffer buffer, int value) { 
buffer.append((char) (value / 10 + '0')); 
buffer.append((char) (value % 10 + '0')); 
} 
} 
/** 
* The Class TwoDigitMonthField. 
*/ 
private static class TwoDigitMonthField implements NumberRule { 
/** 
* The Constant INSTANCE. 
*/ 
static final TwoDigitMonthField INSTANCE = new TwoDigitMonthField(); 
/** 
* Instantiates a new two digit month field. 
*/ 
TwoDigitMonthField() { 
super(); 
} 
public int estimateLength() { 
return 2; 
} 
public void appendTo(StringBuffer buffer, Calendar calendar) { 
appendTo(buffer, calendar.get(Calendar.MONTH) + 1); 
} 
public final void appendTo(StringBuffer buffer, int value) { 
buffer.append((char) (value / 10 + '0')); 
buffer.append((char) (value % 10 + '0')); 
} 
} 
/** 
* The Class TwelveHourField. 
*/ 
private static class TwelveHourField implements NumberRule { 
/** 
* The m rule. 
*/ 
private final NumberRule mRule; 
/** 
* Instantiates a new twelve hour field. 
*  
* @param rule 
*            the rule 
*/ 
TwelveHourField(NumberRule rule) { 
mRule = rule; 
} 
public int estimateLength() { 
return mRule.estimateLength(); 
} 
public void appendTo(StringBuffer buffer, Calendar calendar) { 
int value = calendar.get(Calendar.HOUR); 
if (value == 0) { 
value = calendar.getLeastMaximum(Calendar.HOUR) + 1; 
} 
mRule.appendTo(buffer, value); 
} 
public void appendTo(StringBuffer buffer, int value) { 
mRule.appendTo(buffer, value); 
} 
} 
/** 
* The Class TwentyFourHourField. 
*/ 
private static class TwentyFourHourField implements NumberRule { 
/** 
* The m rule. 
*/ 
private final NumberRule mRule; 
/** 
* Instantiates a new twenty four hour field. 
*  
* @param rule 
*            the rule 
*/ 
TwentyFourHourField(NumberRule rule) { 
mRule = rule; 
} 
public int estimateLength() { 
return mRule.estimateLength(); 
} 
public void appendTo(StringBuffer buffer, Calendar calendar) { 
int value = calendar.get(Calendar.HOUR_OF_DAY); 
if (value == 0) { 
value = calendar.getMaximum(Calendar.HOUR_OF_DAY) + 1; 
} 
mRule.appendTo(buffer, value); 
} 
public void appendTo(StringBuffer buffer, int value) { 
mRule.appendTo(buffer, value); 
} 
} 
/** 
* The Class TimeZoneNameRule. 
*/ 
private static class TimeZoneNameRule implements Rule { 
/** 
* The m time zone. 
*/ 
private final TimeZone mTimeZone; 
/** 
* The m time zone forced. 
*/ 
private final boolean mTimeZoneForced; 
/** 
* The m locale. 
*/ 
private final Locale mLocale; 
/** 
* The m style. 
*/ 
private final int mStyle; 
/** 
* The m standard. 
*/ 
private final String mStandard; 
/** 
* The m daylight. 
*/ 
private final String mDaylight; 
/** 
* Constructs an instance of <code>TimeZoneNameRule</code> with the 
* specified properties. 
*  
* @param timeZone 
*            the time zone 
* @param timeZoneForced 
*            if <code>true</code> the time zone is forced into 
*            standard and daylight 
* @param locale 
*            the locale 
* @param style 
*            the style 
*/ 
TimeZoneNameRule(TimeZone timeZone, boolean timeZoneForced, 
Locale locale, int style) { 
mTimeZone = timeZone; 
mTimeZoneForced = timeZoneForced; 
mLocale = locale; 
mStyle = style; 
if (timeZoneForced) { 
mStandard = getTimeZoneDisplay(timeZone, false, style, 
locale); 
mDaylight = getTimeZoneDisplay(timeZone, true, style, 
locale); 
} else { 
mStandard = null; 
mDaylight = null; 
} 
} 
public int estimateLength() { 
if (mTimeZoneForced) { 
return Math.max(mStandard.length(), mDaylight.length()); 
} else if (mStyle == TimeZone.SHORT) { 
return 4; 
} else { 
return 40; 
} 
} 
public void appendTo(StringBuffer buffer, Calendar calendar) { 
if (mTimeZoneForced) { 
if (mTimeZone.useDaylightTime() 
&& calendar.get(Calendar.DST_OFFSET) != 0) { 
buffer.append(mDaylight); 
} else { 
buffer.append(mStandard); 
} 
} else { 
TimeZone timeZone = calendar.getTimeZone(); 
if (timeZone.useDaylightTime() 
&& calendar.get(Calendar.DST_OFFSET) != 0) { 
buffer.append(getTimeZoneDisplay(timeZone, true, 
mStyle, mLocale)); 
} else { 
buffer.append(getTimeZoneDisplay(timeZone, false, 
mStyle, mLocale)); 
} 
} 
} 
} 
/** 
* The Class TimeZoneNumberRule. 
*/ 
private static class TimeZoneNumberRule implements Rule { 
/** 
* The Constant INSTANCE_COLON. 
*/ 
static final TimeZoneNumberRule INSTANCE_COLON = new TimeZoneNumberRule( 
true); 
/** 
* The Constant INSTANCE_NO_COLON. 
*/ 
static final TimeZoneNumberRule INSTANCE_NO_COLON = new TimeZoneNumberRule( 
false); 
/** 
* The m colon. 
*/ 
final boolean mColon; 
/** 
* Instantiates a new time zone number rule. 
*  
* @param colon 
*            the colon 
*/ 
TimeZoneNumberRule(boolean colon) { 
mColon = colon; 
} 
public int estimateLength() { 
return 5; 
} 
public void appendTo(StringBuffer buffer, Calendar calendar) { 
int offset = calendar.get(Calendar.ZONE_OFFSET) 
+ calendar.get(Calendar.DST_OFFSET); 
if (offset < 0) { 
buffer.append('-'); 
offset = -offset; 
} else { 
buffer.append('+'); 
} 
int hours = offset / (60 * 60 * 1000); 
buffer.append((char) (hours / 10 + '0')); 
buffer.append((char) (hours % 10 + '0')); 
if (mColon) { 
buffer.append(':'); 
} 
int minutes = offset / (60 * 1000) - 60 * hours; 
buffer.append((char) (minutes / 10 + '0')); 
buffer.append((char) (minutes % 10 + '0')); 
} 
} 
/** 
* The Class TimeZoneDisplayKey. 
*/ 
private static class TimeZoneDisplayKey { 
/** 
* The m time zone. 
*/ 
private final TimeZone mTimeZone; 
/** 
* The m style. 
*/ 
private final int mStyle; 
/** 
* The m locale. 
*/ 
private final Locale mLocale; 
/** 
* Constructs an instance of <code>TimeZoneDisplayKey</code> with 
* the specified properties. 
*  
* @param timeZone 
*            the time zone 
* @param daylight 
*            adjust the style for daylight saving time if 
*            <code>true</code> 
* @param style 
*            the timezone style 
* @param locale 
*            the timezone locale 
*/ 
TimeZoneDisplayKey(TimeZone timeZone, boolean daylight, int style, 
Locale locale) { 
mTimeZone = timeZone; 
if (daylight) { 
style |= 0x80000000; 
} 
mStyle = style; 
mLocale = locale; 
} 
@Override 
public int hashCode() { 
return mStyle * 31 + mLocale.hashCode(); 
} 
@Override 
public boolean equals(Object obj) { 
if (this == obj) { 
return true; 
} 
if (obj instanceof TimeZoneDisplayKey) { 
TimeZoneDisplayKey other = (TimeZoneDisplayKey) obj; 
return mTimeZone.equals(other.mTimeZone) 
&& mStyle == other.mStyle 
&& mLocale.equals(other.mLocale); 
} 
return false; 
} 
} 
/** 
* The Class Pair. 
*/ 
private static class Pair { 
/** 
* The m obj1. 
*/ 
private final Object mObj1; 
/** 
* The m obj2. 
*/ 
private final Object mObj2; 
/** 
* Instantiates a new pair. 
*  
* @param obj1 
*            the obj1 
* @param obj2 
*            the obj2 
*/ 
public Pair(Object obj1, Object obj2) { 
mObj1 = obj1; 
mObj2 = obj2; 
} 
@Override 
public boolean equals(Object obj) { 
if (this == obj) { 
return true; 
} 
if (!(obj instanceof Pair)) { 
return false; 
} 
Pair key = (Pair) obj; 
return (mObj1 == null ? key.mObj1 == null : mObj1 
.equals(key.mObj1)) 
&& (mObj2 == null ? key.mObj2 == null : mObj2 
.equals(key.mObj2)); 
} 
@Override 
public int hashCode() { 
return (mObj1 == null ? 0 : mObj1.hashCode()) 
+ (mObj2 == null ? 0 : mObj2.hashCode()); 
} 
@Override 
public String toString() { 
return "[" + mObj1 + ':' + mObj2 + ']'; 
} 
} 
@Override 
public StringBuffer format(Object obj, StringBuffer toAppendTo, 
FieldPosition pos) { 
return null; 
} 
} 
/** 
* 功能描述:给定Date对象,转换成指定格式的字符串日期 
*  
* @param date 
*            一个Date对象, 
* @return : String ex: 2009-06-09 
* @author : daixl 
* @since : 2011.02.23 
*/ 
public static String getYmdStr(Date date) { 
if (date == null) 
return EMPTY; 
return ISO_DATE_FORMAT.format(date); 
} 
/** 
* 功能描述:根据字符串获取一个格式化的时间字符串 
*  
* @param date 
*            一个Date对象, 
* @return : String ex: 2009-06-09 
* @author : daixl 
* @since : 2011.02.23 
*/ 
public static String getYmdStr(Date date, String pattern) { 
if (date == null) 
return EMPTY; 
return format(date, pattern); 
} 
/** 
* 功能描述: 将Date日期转换为带时分秒的字符串型返回 
*  
* @param date 
*            the date 
* @return : String, ex: 2009-06-09 02:04:05 
* @author : daixl 
* @since : 2011.02.23 
*/ 
public static String getYmdhisStr(Date date) { 
if (date == null) 
return EMPTY; 
return ISO_DATETIME_FORMAT.format(date); 
} 
/** 
* 功能描述:给定一个日期和一个整形数值,的到给日期的前一天。 
*  
* @param date 
*              Date对象 
* @param n 
*            整形数值 
* @return Date  一个Date对象 
* @author daixl 
* @since 2011.02.23 
*/ 
public static Date getDateByDateAndHour(Date date, int n) { 
return new Date(date.getTime() + n * 3600000); 
} 
/** 
* 功能描述:给定一个日期和一个整形数值,的到给日期的前一天。 
*  
* @param date 
*              Date对象 
* @param n 
*            整形数值 
* @return Date  一个Date对象 
* @author daixl 
* @since 2011.02.23 
*/ 
public static Date getDateAfterDays(Date date, int n) { 
Date rtnDate = new Date(); 
rtnDate.setTime(date.getTime() + Long.parseLong("86400000") * n); 
return rtnDate; 
} 
public static Long getCurrentDateLong() { 
Date dt = new Date(); 
// 得到秒数,Date类型的getTime()返回毫秒数 
long lSysTime1 = dt.getTime() / 1000; 
// System.out.println(lSysTime1); 
return lSysTime1; 
} 
/** 
* 获得昨天的年 
* */ 
public static String getStrYear(int iI, String strFormat) { 
String str = DateUtils.getLastTimeString(iI, "yyyy-MM-dd"); 
String[] strList = str.split("-"); 
return strList[0]; 
} 
/** 
* 获得昨天的月 
* */ 
public static String getStrMonth(int iI, String strFormat) { 
String str = DateUtils.getLastTimeString(iI, "yyyy-MM-dd"); 
String[] strList = str.split("-"); 
return strList[1]; 
} 
/** 
* 获得昨天的日 
* */ 
public static String getStrDay(int iI, String strFormat) { 
String str = DateUtils.getLastTimeString(iI, "yyyy-MM-dd"); 
String[] strList = str.split("-"); 
return strList[2]; 
} 
public static long getSystemtime() { 
return System.currentTimeMillis(); 
} 
/** 
* 获得一个时间,增加或是减少几个小时候的字符串日期. 
*  
* */ 
public static String getStrCalHour(int iI, String strFormat) { 
DateFormat df = new SimpleDateFormat(strFormat); 
Calendar c = Calendar.getInstance(); 
c.add(Calendar.HOUR, iI); // 目前時間加3小時 
return df.format(c.getTime()); 
} 
// 将时间转换成 年-月-日 格式 
public static String toDate(Date createDate) { 
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd"); 
return formater.format(createDate); 
} 
// 将时间转换成 年-月 格式 
public static String toMonth(Date createDate) { 
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM"); 
return formater.format(createDate); 
} 
// 返回当天日期的前一天 
public static String getYesterday(String dt) throws ParseException { 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
Calendar cal = Calendar.getInstance(); 
cal.setTime(sdf.parse(dt)); 
cal.add(Calendar.DATE, -1); 
String yesterday = sdf.format(cal.getTime()); 
return yesterday; 
} 
public static String getYesday(Date date, String strPatten, int iN) { 
Calendar c = Calendar.getInstance(); 
c.setTime(date); 
int day = c.get(Calendar.DATE); 
c.set(Calendar.DATE, day - iN); 
String dayBefore = new SimpleDateFormat(strPatten).format(c.getTime()); 
return dayBefore; 
} 
public static String getYesterday(String strDate, String strPatten, int iN) { 
Date date = DateUtils.getDate(strDate); 
Calendar c = Calendar.getInstance(); 
c.setTime(date); 
int day = c.get(Calendar.DATE); 
c.set(Calendar.DATE, day - iN); 
String dayBefore = new SimpleDateFormat(strPatten).format(c.getTime()); 
return dayBefore; 
} 
public static String getMonthShort(String strDate, int plusMonthNum) { 
Date date = DateUtils.getDate(strDate); 
Calendar cal = Calendar.getInstance(); 
cal.setTime(date); 
cal.add(Calendar.MONTH, plusMonthNum); 
cal.set(Calendar.DAY_OF_MONTH, 1); 
return toMonth(cal.getTime()); 
} 
public static String getMonthLashDay(String strDate, int plusMonthNum) { 
Date date = DateUtils.getDate(strDate); 
Calendar cal = Calendar.getInstance(); 
cal.setTime(date); 
cal.add(Calendar.MONTH, plusMonthNum); 
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE)); 
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); 
} 
/** 
* @Function 获取时间,前面所有的时间,都是减少 n 天,前推多少天。 
* */ 
public static String getYMDDate(String date, int In) { 
Date dateYear = DateUtils.getDate(date); 
date = DateUtils.getYesday(dateYear, "yyyy-MM-dd", In); 
return date; 
} 
/** 
* @Function 获取时间,前面所有的时间,都是减少 n 月,前推多少月。 
* */ 
public static String getYMDate(String date, int In) { 
Date dateYear = DateUtils.getDate(date); 
date = DateUtils.getYesday(dateYear, "yyyy-MM", In); 
return date; 
} 
/** 
* 王展翅 得到当前日期时间,格式为yyyyMMdd 
*  
* @return String 
*/ 
public static String getFormatTo_yyyymmdd(Date date, String strFormat) 
throws Exception { 
SimpleDateFormat formatter = new SimpleDateFormat(strFormat); 
return formatter.format(date); 
} 
public static String strToDateDayTime(String date) { 
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); 
formatter.setLenient(false); 
Date newDate = null; 
try { 
newDate = formatter.parse(date); 
formatter = new SimpleDateFormat("yyyy-MM-dd"); 
} catch (ParseException e) { 
e.printStackTrace(); 
} 
return formatter.format(newDate); 
} 
public static String strToDateHour(String date) { 
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM"); 
formatter.setLenient(false); 
Date newDate = null; 
try { 
newDate = formatter.parse(date); 
formatter = new SimpleDateFormat("yyyy-MM"); 
} catch (ParseException e) { 
e.printStackTrace(); 
} 
return formatter.format(newDate); 
} 
public static String getDateByFormatAndNum(Date date, String strFormat, 
int num) { 
Calendar calendar = Calendar.getInstance(); 
calendar.setTime(date); 
calendar.add(Calendar.DATE, num); 
Date newDate = calendar.getTime(); 
SimpleDateFormat formatter = new SimpleDateFormat(strFormat); 
return formatter.format(newDate); 
} 
public static String getDateByFormat(Date date, String strFormat) { 
SimpleDateFormat formatter = new SimpleDateFormat(strFormat); 
return formatter.format(date); 
} 
public static Date stringToDateByFormat(String dayString, String strFormat) { 
SimpleDateFormat formatter = new SimpleDateFormat(strFormat); 
Date date = null; 
try { 
date = formatter.parse(dayString); 
} catch (ParseException e) { 
e.printStackTrace(); 
} 
return date; 
} 
public static Date stringToStringDateByFormat(String dayString, String strFormat) { 
SimpleDateFormat formatter = new SimpleDateFormat(strFormat); 
Date date = null; 
try { 
date = formatter.parse(dayString); 
} catch (ParseException e) { 
e.printStackTrace(); 
} 
return date; 
} 
/** 
* 取当前时间的前几天 
*  
* @param date 
* @return 
*/ 
public static String getNextDay(Integer anyday) { 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式 
Calendar calendar = Calendar.getInstance(); 
calendar.setTime(new Date()); 
calendar.add(Calendar.DAY_OF_MONTH, anyday); 
Date date1 = calendar.getTime(); 
String time = df.format(date1); 
// System.out.println(time); 
return time; 
} 
/** 
* 取当前时间的前几天 
*  
* @param date 
* @return 
*/ 
public static String getNextDay1(Integer anyday, String daytime) { 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式 
Calendar calendar = Calendar.getInstance(); 
try { 
calendar.setTime(df.parse(daytime)); 
} catch (ParseException e) { 
e.printStackTrace(); 
} 
calendar.add(Calendar.DAY_OF_MONTH, anyday); 
Date date1 = calendar.getTime(); 
String time = df.format(date1); 
// System.out.println(time); 
return time; 
} 
public static String getNextMon(Integer anymon, String daytime) { 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");// 设置日期格式 
Calendar calendar = Calendar.getInstance(); 
try { 
calendar.setTime(df.parse(daytime)); 
} catch (ParseException e) { 
e.printStackTrace(); 
} 
calendar.add(Calendar.MONTH, anymon); 
Date date1 = calendar.getTime(); 
String time = df.format(date1); 
// System.out.println(time); 
return time; 
} 
public static String dateFormat(int number,String datetime) {   
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");   
Date date = null;   
try {   
date = sdf.parse(datetime);   
} catch (ParseException e) {   
e.printStackTrace();   
}   
Calendar cl = Calendar.getInstance();   
cl.setTime(date);   
cl.add(Calendar.MONTH,number);   
date = cl.getTime();   
return sdf.format(date);   
}   
/** 
* 取当前时间的前几天 
*  
* @param date 
* @return 
*/ 
public static String getjitianDay(Integer anyday, Date daydate) { 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式 
Calendar calendar = Calendar.getInstance(); 
calendar.setTime(daydate); 
calendar.add(Calendar.DAY_OF_MONTH, anyday); 
Date date1 = calendar.getTime(); 
String time = df.format(date1); 
// System.out.println(time); 
return time; 
} 
/** 
* 字符串时间 按照指定格式返回时间字符串 
*  
* @param oldDate 
*            旧时间(字符串类型) 
* @param oldDateFormat 
*            旧时间格式 
* @param newDateFormat 
*            生成时间格式 
* @return 
*/ 
public static String stringDateToStringDate(String oldDate, 
String oldDateFormat, String newDateFormat) { 
SimpleDateFormat formatter = new SimpleDateFormat(oldDateFormat); 
SimpleDateFormat newFormatter = new SimpleDateFormat(newDateFormat); 
Date date = null; 
String str = null; 
try { 
date = formatter.parse(oldDate); 
str = newFormatter.format(date); 
} catch (ParseException e) { 
e.printStackTrace(); 
} 
return str; 
} 
/** 
* <li>功能描述:时间相减得到天数 
*  
* @param beginDateStr 
* @param endDateStr 
* @return long 
* @author Administrator 
*/ 
public static long getDaySub(String beginDateStr, String endDateStr) { 
long day = 0; 
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat( 
"yyyy-MM-dd"); 
java.util.Date beginDate; 
java.util.Date endDate; 
try { 
beginDate = format.parse(beginDateStr); 
endDate = format.parse(endDateStr); 
day = (endDate.getTime() - beginDate.getTime()) 
/ (24 * 60 * 60 * 1000); 
// System.out.println("相隔的天数="+day); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
return day; 
} 
public static void main(String[] args) throws Exception { 
// ISO_DATETIME_TIME_ZONE_FORMAT 
// System.out.println(DateUtils.getLastTimeString(0, "yyyy-MM-dd")); 
// System.out.println(DateUtils.getLastTimeString(-1, 
// "yyyy-MM-dd HH:mm:ss")); 
// Date dateYear = DateUtils.getDate("2017-12"); 
// String strYearMonth = DateUtils.getYmdStr(dateYear, "yyyy-MM"); 
// 
// Date d1 = DateUtils.getDate("2017-12-26"); 
// String ss1 = DateUtils.getYesday(d1,"yyyy-MM-dd",7); 
// String ss2 = DateUtils.getFormatTo_yyyymmdd(d1,"yyyyMMdd"); 
// System.out.println(ss2); 
System.out.println(DateUtils.getMonthLashDay("2017-11-29", -1)); 
System.out.println(DateUtils.getYMDDate("2017-12-29", 30)); 
System.out.println(DateUtils.getYMDate("2017-12", 2)); 
// 20171226 
// 2017-12-26 19:32:12 
System.out.print(DateUtils.getYesday(new Date(), "yyyyMMdd", 1)); 
} 
} 

  

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/17861.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论