/* * */ package com.toolkit.date; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Map; import org.apache.log4j.Logger; /** * 日期操作函数工具类 DateHelper. */ public class DateHelper{ /** The log. */ private static Logger log = Logger.getLogger(DateHelper.class); /** The date formats. */ private Map dateFormats = Collections .synchronizedMap(new HashMap()); // -------------------------------------- /** * format yyyy-MM-dd HH:mm:ss 格式日期时间 * * @return String */ public static String getDateTime() { return getDateTime("yyyy-MM-dd HH:mm:ss"); } /** * yyyy-MM-dd 格式日期时间 * * @return String */ public static String getDate() { return getDateTime("yyyy-MM-dd HH:mm:ss").substring(0, 10); } /** * 根据日期格式,获取不同的系统当前时间 * * @param timeFormat * 日期格式 如:yyy-MM-dd HH:mm:ss * @return */ public static String getDateTime(String timeFormat) { Calendar calendar = Calendar.getInstance(); java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(timeFormat); return df.format(calendar.getTime()); } /** * 获得 yyyy-MM-dd HH:mm:ss 格式化日期 * * @param timeFormat * 日期参数 * @param timestamp * 日期格式 如: * @return 返回日期字符串 */ public static String getDateTime(String timeFormat, Timestamp timestamp) { if (timestamp == null) return ""; if (timeFormat == null) return timestamp.toString(); if (timeFormat.equals("")) return timestamp.toString(); java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(timeFormat); return df.format(new Date(timestamp.getTime())); } /** * 获取系统当前时间 * * @return 返回类型对象 */ public static Timestamp getTimestampDateTime() { String sysTime = getDateTime("yyyy-MM-dd HH:mm:ss"); Timestamp timestamp = Timestamp.valueOf(sysTime); return timestamp; } // ------------------------------------- /** * 八位数字获得日期对象 * * @param yyyyMMdd * the yyyy m mdd * * @return the calendar * * @throws ParseException * the parse exception */ public Calendar toDate(String yyyyMMdd) throws ParseException { return toDate("yyyyMMdd", yyyyMMdd); } /** * 字符串格式化成日期对象 * * @param format * the format * @param date * the date * * @return the calendar * * @throws ParseException * the parse exception */ public Calendar toDate(String format, String date) throws ParseException { Calendar _date = new GregorianCalendar(); _date.setTime(getDateFormat(format).parse(date)); log.debug("toDate(" + format + ", " + date + ")=" + _date); return _date; } /** * 日期对象以指定的字符传返回 * * @param format * the format * @param date * the date * * @return the string */ public String toString(String format, Calendar cal) { return getDateFormat(format).format(cal.getTime()); } /** * 日期对象格式化成 yyyyMMdd 字符串输出 * * @param date * the date * * @return the string */ public String toYYYYMMDD(Calendar date) { return toString("yyyyMMdd", date); } /** * 日期对象格式化成 yyyyMMdd 整数输出 * * @param date * the date * * @return the string */ public int toYYYYMMDDInteger(Calendar date) { return this.getYear(date) * 10000 + (this.getMonth(date) * 100 + this.getDay(date)); } /** * 获得指定日期对应的年份 * * @param date * the date * * @return the year */ public Integer getYear(Calendar date) { log.debug("getYear()"); return new Integer(date.get(Calendar.YEAR)); } /** * 获得指定日期对应的月份 * * @param date * the date * * @return the month */ public Integer getMonth(Calendar date) { log.debug("getMonth()"); return new Integer(date.get(Calendar.MONTH) + 1); } /** * 获得指定日期对象对应的日期 * * @param date * the date * * @return the day */ public Integer getDay(Calendar date) { log.debug("getDay()"); return new Integer(date.get(Calendar.DATE)); } /** * 获得从某日到某日的间隔时间对象数组 * * @param fromDate * the from date * @param toDate * the to date * * @return the calendar[] */ public static Calendar[] buildDayInterval(Calendar fromDate, Calendar toDate) { return buildInterval(fromDate, toDate, Calendar.DATE); } /** * 获得从某日加一个月时间到止点日加一月最后一天的时间对象数组 * * @param fromDate * the from date * @param toDate * the to date * * @return the calendar[] */ public static Calendar[] buildMonthInterval(Calendar fromDate, Calendar toDate) { return buildInterval(fromDate, toDate, Calendar.MONTH); } /** * 获得指定月份的第一天日期 * * @param date * the date * * @return the calendar */ public Calendar lastDayInMonth(Calendar date) { Calendar lastMonthDate = (Calendar) date.clone(); lastMonthDate.add(Calendar.MONTH, 1); lastMonthDate.add(Calendar.DATE, -1); return lastMonthDate; } /** * 将某个日期增加指定天数,并返回结果。如果传入负数,则为减。 * * @param cal * @param ammount * 调整的日期天数 * @return 结果日期对象 */ public Calendar addDay(Calendar cal, final int ammount) { cal.add(Calendar.DATE, ammount); return cal; } /** * 将某个日期增加指定月数,并返回结果。如果传入负数,则为减。 * * @param ammount * 调整的日期天数 * @return 结果日期对象 */ public Calendar addMonth(Calendar cal, final int ammount) { cal.add(Calendar.MONTH, ammount); return cal; } /** * 将某个日期增加指定年数,并返回结果。如果传入负数,则为减。 * * @param ammount * 调整的日期年数 * @return 结果日期对象 */ public Calendar addYear(Calendar cal, final int ammount) { cal.add(Calendar.YEAR, ammount); return cal; } /** * 获得从某日到某日的间隔时间对象数组 * * @param fromDate * the from date * @param toDate * the to date * @param dateIncrementType * the date increment type * * @return the calendar[] */ private static Calendar[] buildInterval(Calendar fromDate, Calendar toDate, int dateIncrementType) { log.debug("buildInterval(" + fromDate + ", " + toDate + ")"); if (dateIncrementType != Calendar.DATE && dateIncrementType != Calendar.MONTH) throw new Error("Date increment type not supported"); ArrayList list = new ArrayList(); Calendar cal = (Calendar) toDate.clone(); cal.add(dateIncrementType, 1); if (dateIncrementType == Calendar.MONTH) cal.add(Calendar.DATE, -1); long toDateInMillis = cal.getTimeInMillis(); cal = (Calendar) fromDate.clone(); while (cal.getTimeInMillis() < toDateInMillis) { list.add((Calendar) cal.clone()); cal.add(dateIncrementType, 1); } return list.toArray(new Calendar[list.size()]); } /** * Gets the date format. * * @param format * the format * * @return the date format */ private SimpleDateFormat getDateFormat(String format) { SimpleDateFormat sdf = dateFormats.get(format); if (sdf == null) { sdf = new SimpleDateFormat(format); dateFormats.put(format, sdf); } return sdf; } /** * @param args * @throws ParseException */ public static void main(String[] args) throws ParseException { Calendar c = Calendar.getInstance(); int day = c.getActualMaximum(Calendar.DAY_OF_MONTH);// 本月份的天数 System.out.println("本月天数 : : " + day); DateHelper dateHelper = new DateHelper(); // Calendar css = dateHelper.toDate("20090829"); Calendar css = dateHelper.toDate("yyyy-MM-dd", "2009-08-29"); String dps = dateHelper.toYYYYMMDD(css); System.out.println("字符串转换为日期 : " + dps); // 本月的第一天 Calendar calendar = new GregorianCalendar(); calendar.set(Calendar.DATE, 1); SimpleDateFormat simpleFormate = new SimpleDateFormat("yyyy-MM-dd"); System.out.println("本月的第一天" + simpleFormate.format(calendar.getTime())); // 本月的最后一天 calendar = new GregorianCalendar(); calendar.set(Calendar.DATE, 1); calendar.roll(Calendar.DATE, -1); simpleFormate = new SimpleDateFormat("yyyy-MM-dd"); System.out.println("本月的最后一天" + simpleFormate.format(calendar.getTime())); } }