DateUtils.java 6.7 KB

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