123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- /*
- *
- */
- 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<String, SimpleDateFormat> dateFormats = Collections
- .synchronizedMap(new HashMap<String, SimpleDateFormat>());
- // --------------------------------------
- /**
- * 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()));
- }
- /**
- * 获取系统当前时间 <Timestamp>
- *
- * @return 返回<Timestamp>类型对象
- */
- 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<Calendar> list = new ArrayList<Calendar>();
- 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()));
- }
- }
|