46756071cef3bb59205e2bde18a7442b403dd2fa.svn-base 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. *
  3. */
  4. package com.toolkit.date;
  5. import java.sql.Timestamp;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.Calendar;
  10. import java.util.Collections;
  11. import java.util.Date;
  12. import java.util.GregorianCalendar;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. import org.apache.log4j.Logger;
  16. /**
  17. * 日期操作函数工具类 DateHelper.
  18. */
  19. public class DateHelper{
  20. /** The log. */
  21. private static Logger log = Logger.getLogger(DateHelper.class);
  22. /** The date formats. */
  23. private Map<String, SimpleDateFormat> dateFormats = Collections
  24. .synchronizedMap(new HashMap<String, SimpleDateFormat>());
  25. // --------------------------------------
  26. /**
  27. * format yyyy-MM-dd HH:mm:ss 格式日期时间
  28. *
  29. * @return String
  30. */
  31. public static String getDateTime() {
  32. return getDateTime("yyyy-MM-dd HH:mm:ss");
  33. }
  34. /**
  35. * yyyy-MM-dd 格式日期时间
  36. *
  37. * @return String
  38. */
  39. public static String getDate() {
  40. return getDateTime("yyyy-MM-dd HH:mm:ss").substring(0, 10);
  41. }
  42. /**
  43. * 根据日期格式,获取不同的系统当前时间
  44. *
  45. * @param timeFormat
  46. * 日期格式 如:yyy-MM-dd HH:mm:ss
  47. * @return
  48. */
  49. public static String getDateTime(String timeFormat) {
  50. Calendar calendar = Calendar.getInstance();
  51. java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(timeFormat);
  52. return df.format(calendar.getTime());
  53. }
  54. /**
  55. * 获得 yyyy-MM-dd HH:mm:ss 格式化日期
  56. *
  57. * @param timeFormat
  58. * 日期参数
  59. * @param timestamp
  60. * 日期格式 如:
  61. * @return 返回日期字符串
  62. */
  63. public static String getDateTime(String timeFormat, Timestamp timestamp) {
  64. if (timestamp == null)
  65. return "";
  66. if (timeFormat == null)
  67. return timestamp.toString();
  68. if (timeFormat.equals(""))
  69. return timestamp.toString();
  70. java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(timeFormat);
  71. return df.format(new Date(timestamp.getTime()));
  72. }
  73. /**
  74. * 获取系统当前时间 <Timestamp>
  75. *
  76. * @return 返回<Timestamp>类型对象
  77. */
  78. public static Timestamp getTimestampDateTime() {
  79. String sysTime = getDateTime("yyyy-MM-dd HH:mm:ss");
  80. Timestamp timestamp = Timestamp.valueOf(sysTime);
  81. return timestamp;
  82. }
  83. // -------------------------------------
  84. /**
  85. * 八位数字获得日期对象
  86. *
  87. * @param yyyyMMdd
  88. * the yyyy m mdd
  89. *
  90. * @return the calendar
  91. *
  92. * @throws ParseException
  93. * the parse exception
  94. */
  95. public Calendar toDate(String yyyyMMdd) throws ParseException {
  96. return toDate("yyyyMMdd", yyyyMMdd);
  97. }
  98. /**
  99. * 字符串格式化成日期对象
  100. *
  101. * @param format
  102. * the format
  103. * @param date
  104. * the date
  105. *
  106. * @return the calendar
  107. *
  108. * @throws ParseException
  109. * the parse exception
  110. */
  111. public Calendar toDate(String format, String date) throws ParseException {
  112. Calendar _date = new GregorianCalendar();
  113. _date.setTime(getDateFormat(format).parse(date));
  114. log.debug("toDate(" + format + ", " + date + ")=" + _date);
  115. return _date;
  116. }
  117. /**
  118. * 日期对象以指定的字符传返回
  119. *
  120. * @param format
  121. * the format
  122. * @param date
  123. * the date
  124. *
  125. * @return the string
  126. */
  127. public String toString(String format, Calendar cal) {
  128. return getDateFormat(format).format(cal.getTime());
  129. }
  130. /**
  131. * 日期对象格式化成 yyyyMMdd 字符串输出
  132. *
  133. * @param date
  134. * the date
  135. *
  136. * @return the string
  137. */
  138. public String toYYYYMMDD(Calendar date) {
  139. return toString("yyyyMMdd", date);
  140. }
  141. /**
  142. * 日期对象格式化成 yyyyMMdd 整数输出
  143. *
  144. * @param date
  145. * the date
  146. *
  147. * @return the string
  148. */
  149. public int toYYYYMMDDInteger(Calendar date) {
  150. return this.getYear(date) * 10000 + (this.getMonth(date) * 100 + this.getDay(date));
  151. }
  152. /**
  153. * 获得指定日期对应的年份
  154. *
  155. * @param date
  156. * the date
  157. *
  158. * @return the year
  159. */
  160. public Integer getYear(Calendar date) {
  161. log.debug("getYear()");
  162. return new Integer(date.get(Calendar.YEAR));
  163. }
  164. /**
  165. * 获得指定日期对应的月份
  166. *
  167. * @param date
  168. * the date
  169. *
  170. * @return the month
  171. */
  172. public Integer getMonth(Calendar date) {
  173. log.debug("getMonth()");
  174. return new Integer(date.get(Calendar.MONTH) + 1);
  175. }
  176. /**
  177. * 获得指定日期对象对应的日期
  178. *
  179. * @param date
  180. * the date
  181. *
  182. * @return the day
  183. */
  184. public Integer getDay(Calendar date) {
  185. log.debug("getDay()");
  186. return new Integer(date.get(Calendar.DATE));
  187. }
  188. /**
  189. * 获得从某日到某日的间隔时间对象数组
  190. *
  191. * @param fromDate
  192. * the from date
  193. * @param toDate
  194. * the to date
  195. *
  196. * @return the calendar[]
  197. */
  198. public static Calendar[] buildDayInterval(Calendar fromDate, Calendar toDate) {
  199. return buildInterval(fromDate, toDate, Calendar.DATE);
  200. }
  201. /**
  202. * 获得从某日加一个月时间到止点日加一月最后一天的时间对象数组
  203. *
  204. * @param fromDate
  205. * the from date
  206. * @param toDate
  207. * the to date
  208. *
  209. * @return the calendar[]
  210. */
  211. public static Calendar[] buildMonthInterval(Calendar fromDate, Calendar toDate) {
  212. return buildInterval(fromDate, toDate, Calendar.MONTH);
  213. }
  214. /**
  215. * 获得指定月份的第一天日期
  216. *
  217. * @param date
  218. * the date
  219. *
  220. * @return the calendar
  221. */
  222. public Calendar lastDayInMonth(Calendar date) {
  223. Calendar lastMonthDate = (Calendar) date.clone();
  224. lastMonthDate.add(Calendar.MONTH, 1);
  225. lastMonthDate.add(Calendar.DATE, -1);
  226. return lastMonthDate;
  227. }
  228. /**
  229. * 将某个日期增加指定天数,并返回结果。如果传入负数,则为减。
  230. *
  231. * @param cal
  232. * @param ammount
  233. * 调整的日期天数
  234. * @return 结果日期对象
  235. */
  236. public Calendar addDay(Calendar cal, final int ammount) {
  237. cal.add(Calendar.DATE, ammount);
  238. return cal;
  239. }
  240. /**
  241. * 将某个日期增加指定月数,并返回结果。如果传入负数,则为减。
  242. *
  243. * @param ammount
  244. * 调整的日期天数
  245. * @return 结果日期对象
  246. */
  247. public Calendar addMonth(Calendar cal, final int ammount) {
  248. cal.add(Calendar.MONTH, ammount);
  249. return cal;
  250. }
  251. /**
  252. * 将某个日期增加指定年数,并返回结果。如果传入负数,则为减。
  253. *
  254. * @param ammount
  255. * 调整的日期年数
  256. * @return 结果日期对象
  257. */
  258. public Calendar addYear(Calendar cal, final int ammount) {
  259. cal.add(Calendar.YEAR, ammount);
  260. return cal;
  261. }
  262. /**
  263. * 获得从某日到某日的间隔时间对象数组
  264. *
  265. * @param fromDate
  266. * the from date
  267. * @param toDate
  268. * the to date
  269. * @param dateIncrementType
  270. * the date increment type
  271. *
  272. * @return the calendar[]
  273. */
  274. private static Calendar[] buildInterval(Calendar fromDate, Calendar toDate,
  275. int dateIncrementType) {
  276. log.debug("buildInterval(" + fromDate + ", " + toDate + ")");
  277. if (dateIncrementType != Calendar.DATE && dateIncrementType != Calendar.MONTH)
  278. throw new Error("Date increment type not supported");
  279. ArrayList<Calendar> list = new ArrayList<Calendar>();
  280. Calendar cal = (Calendar) toDate.clone();
  281. cal.add(dateIncrementType, 1);
  282. if (dateIncrementType == Calendar.MONTH)
  283. cal.add(Calendar.DATE, -1);
  284. long toDateInMillis = cal.getTimeInMillis();
  285. cal = (Calendar) fromDate.clone();
  286. while (cal.getTimeInMillis() < toDateInMillis) {
  287. list.add((Calendar) cal.clone());
  288. cal.add(dateIncrementType, 1);
  289. }
  290. return list.toArray(new Calendar[list.size()]);
  291. }
  292. /**
  293. * Gets the date format.
  294. *
  295. * @param format
  296. * the format
  297. *
  298. * @return the date format
  299. */
  300. private SimpleDateFormat getDateFormat(String format) {
  301. SimpleDateFormat sdf = dateFormats.get(format);
  302. if (sdf == null) {
  303. sdf = new SimpleDateFormat(format);
  304. dateFormats.put(format, sdf);
  305. }
  306. return sdf;
  307. }
  308. /**
  309. * @param args
  310. * @throws ParseException
  311. */
  312. public static void main(String[] args) throws ParseException {
  313. Calendar c = Calendar.getInstance();
  314. int day = c.getActualMaximum(Calendar.DAY_OF_MONTH);// 本月份的天数
  315. System.out.println("本月天数 : : " + day);
  316. DateHelper dateHelper = new DateHelper();
  317. // Calendar css = dateHelper.toDate("20090829");
  318. Calendar css = dateHelper.toDate("yyyy-MM-dd", "2009-08-29");
  319. String dps = dateHelper.toYYYYMMDD(css);
  320. System.out.println("字符串转换为日期 : " + dps);
  321. // 本月的第一天
  322. Calendar calendar = new GregorianCalendar();
  323. calendar.set(Calendar.DATE, 1);
  324. SimpleDateFormat simpleFormate = new SimpleDateFormat("yyyy-MM-dd");
  325. System.out.println("本月的第一天" + simpleFormate.format(calendar.getTime()));
  326. // 本月的最后一天
  327. calendar = new GregorianCalendar();
  328. calendar.set(Calendar.DATE, 1);
  329. calendar.roll(Calendar.DATE, -1);
  330. simpleFormate = new SimpleDateFormat("yyyy-MM-dd");
  331. System.out.println("本月的最后一天" + simpleFormate.format(calendar.getTime()));
  332. }
  333. }