DateUtils.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package com.shawn.util;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. public class DateUtils
  9. {
  10. public static final String PATTERN_MMdd = "MMdd";
  11. public static final String PATTERN_yyyyMMdd = "yyyyMMdd";
  12. public static final String PATTERN_yyyyMMddHHmmss = "yyyyMMddHHmmss";
  13. public static final String PATTERN_yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
  14. public static final String PATTERN_yyyy_MM_dd = "yyyy-MM-dd";
  15. public static final String PATTERN_yyyy_M_d = "yyyy-M-d";
  16. public static final String PATTERN_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
  17. public static final String PATTERN_yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
  18. public static final String PATTERN_HH_mm_ss = "HH:mm:ss";
  19. public static final String PATTERN_HHmmss = "HHmmss";
  20. public static final Map<String, String> mapChDate = new HashMap<String, String>();
  21. public static Date getNextday(Date now)
  22. {
  23. Calendar calendar = Calendar.getInstance();
  24. calendar.setTime(now);
  25. calendar.add(5, 1);
  26. return calendar.getTime();
  27. }
  28. public static Date getPreday(Date now)
  29. {
  30. Calendar calendar = Calendar.getInstance();
  31. calendar.setTime(now);
  32. calendar.add(5, -1);
  33. return calendar.getTime();
  34. }
  35. public static String formatDate_ch(Date date)
  36. {
  37. if(date==null){
  38. throw new IllegalArgumentException("需要转换的对象为空");
  39. }
  40. String str = formatDate(date, "MMdd");
  41. str = (String)mapChDate.get(str.substring(0, 2)) + str.substring(2);
  42. return str;
  43. }
  44. public static String formatDate(Date date, String pattern)
  45. {
  46. if(date==null){
  47. return "";
  48. }
  49. SimpleDateFormat sdf = null;
  50. if (pattern == null)
  51. sdf = new SimpleDateFormat();
  52. else {
  53. sdf = new SimpleDateFormat(pattern);
  54. }
  55. return sdf.format(date);
  56. }
  57. public static String formatDate(Date date) {
  58. SimpleDateFormat sdf = new SimpleDateFormat();
  59. return sdf.format(date);
  60. }
  61. public static Date parseDate(String source, String pattern) throws ParseException
  62. {
  63. SimpleDateFormat sdf = null;
  64. if (pattern == null)
  65. sdf = new SimpleDateFormat();
  66. else {
  67. sdf = new SimpleDateFormat(pattern);
  68. }
  69. return sdf.parse(source);
  70. }
  71. public static Date parseDate(String source) throws ParseException {
  72. SimpleDateFormat sdf = new SimpleDateFormat();
  73. return sdf.parse(source);
  74. }
  75. public static Date parseDate(String source, Date defaultDate) {
  76. try {
  77. SimpleDateFormat sdf = new SimpleDateFormat();
  78. return sdf.parse(source);
  79. }
  80. catch (ParseException e)
  81. {
  82. }
  83. return defaultDate;
  84. }
  85. public static Date parseDate(String source, String pattern, Date defaultDate) {
  86. try {
  87. SimpleDateFormat sdf = null;
  88. if (pattern == null)
  89. sdf = new SimpleDateFormat();
  90. else {
  91. sdf = new SimpleDateFormat(pattern);
  92. }
  93. return sdf.parse(source);
  94. }
  95. catch (ParseException e) {
  96. }
  97. return defaultDate;
  98. }
  99. public static boolean tryParse(String source) throws ParseException {
  100. try {
  101. Integer.parseInt(source);
  102. return true;
  103. } catch (NumberFormatException e) {
  104. }
  105. return false;
  106. }
  107. public static Date changeDate(Date date, TimeType timeType, int amount)
  108. {
  109. if ((date == null) || (timeType == null) || (amount == 0)) {
  110. return date;
  111. }
  112. Calendar c = Calendar.getInstance();
  113. c.setTime(date);
  114. c.add(timeType.getValue(), amount);
  115. return c.getTime();
  116. }
  117. public static boolean isSameDay(Date date1, Date date2)
  118. {
  119. if ((date1 == null) || (date2 == null)) {
  120. throw new IllegalArgumentException("The date must not be null");
  121. }
  122. Calendar cal1 = Calendar.getInstance();
  123. cal1.setTime(date1);
  124. Calendar cal2 = Calendar.getInstance();
  125. cal2.setTime(date2);
  126. return isSameDay(cal1, cal2);
  127. }
  128. public static boolean isSameDay(Calendar cal1, Calendar cal2)
  129. {
  130. if ((cal1 == null) || (cal2 == null)) {
  131. throw new IllegalArgumentException("The date must not be null");
  132. }
  133. return (cal1.get(0) == cal2.get(0)) && (cal1.get(1) == cal2.get(1)) && (cal1.get(6) == cal2.get(6));
  134. }
  135. /** @deprecated */
  136. public static Date add(Date date, int calendarField, int amount)
  137. {
  138. if (date == null) {
  139. throw new IllegalArgumentException("The date must not be null");
  140. }
  141. Calendar c = Calendar.getInstance();
  142. c.setTime(date);
  143. c.add(calendarField, amount);
  144. return c.getTime();
  145. }
  146. public static Date addYears(Date date, int amount)
  147. {
  148. return add(date, 1, amount);
  149. }
  150. public static Date addMonths(Date date, int amount)
  151. {
  152. return add(date, 2, amount);
  153. }
  154. public static Date addWeeks(Date date, int amount)
  155. {
  156. return add(date, 3, amount);
  157. }
  158. public static Date addDays(Date date, int amount)
  159. {
  160. return add(date, 5, amount);
  161. }
  162. public static Date addHours(Date date, int amount)
  163. {
  164. return add(date, 11, amount);
  165. }
  166. public static Date addMinutes(Date date, int amount)
  167. {
  168. return add(date, 12, amount);
  169. }
  170. public static Date addSeconds(Date date, int amount)
  171. {
  172. return add(date, 13, amount);
  173. }
  174. public static Date addMilliseconds(Date date, int amount)
  175. {
  176. return add(date, 14, amount);
  177. }
  178. static
  179. {
  180. mapChDate.put("01", "一月");
  181. mapChDate.put("02", "二月");
  182. mapChDate.put("03", "三月");
  183. mapChDate.put("04", "四月");
  184. mapChDate.put("05", "五月");
  185. mapChDate.put("06", "六月");
  186. mapChDate.put("07", "七月");
  187. mapChDate.put("08", "八月");
  188. mapChDate.put("09", "九月");
  189. mapChDate.put("10", "十月");
  190. mapChDate.put("11", "十一月");
  191. mapChDate.put("12", "十二月");
  192. }
  193. public static enum TimeType
  194. {
  195. SECOND(13), MINUTE(12), HOUR(10), DAY(5),
  196. WEEK(4), MONTH(2),
  197. YEAR(1);
  198. private int value;
  199. private TimeType(int value) {
  200. this.value = value;
  201. }
  202. public int getValue() {
  203. return this.value;
  204. }
  205. }
  206. /**
  207. *
  208. * 毛龙飞
  209. * @Description 获取日期的后几天
  210. * @param d
  211. * @param day
  212. * @return
  213. */
  214. public static Date getDateAfter(Date d, int day) {
  215. Calendar now = Calendar.getInstance();
  216. now.setTime(d);
  217. now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
  218. return now.getTime();
  219. }
  220. }