DateUtils.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * Copyright (c) 2016-2019 人人开源 All rights reserved.
  3. * <p>
  4. * https://www.renren.io
  5. * <p>
  6. * 版权所有,侵权必究!
  7. */
  8. package io.renren.common.utils;
  9. import org.apache.commons.lang.StringUtils;
  10. import org.joda.time.DateTime;
  11. import org.joda.time.LocalDate;
  12. import org.joda.time.format.DateTimeFormat;
  13. import org.joda.time.format.DateTimeFormatter;
  14. import java.text.SimpleDateFormat;
  15. import java.util.Date;
  16. import java.util.regex.Pattern;
  17. /**
  18. * 日期处理
  19. *
  20. * @author Mark 735032128@qq.com
  21. */
  22. public class DateUtils {
  23. /** 时间格式(yyyy-MM-dd) */
  24. public final static String DATE_PATTERN = "yyyy-MM-dd";
  25. /** 时间格式(yyyy-MM-dd HH:mm:ss) */
  26. public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
  27. /**
  28. * 日期格式化 日期格式为:yyyy-MM-dd
  29. * @param date 日期
  30. * @return 返回yyyy-MM-dd格式日期
  31. */
  32. public static String format(Date date) {
  33. return format(date, DATE_PATTERN);
  34. }
  35. /**
  36. * 日期格式化 日期格式为:yyyy-MM-dd
  37. * @param date 日期
  38. * @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN
  39. * @return 返回yyyy-MM-dd格式日期
  40. */
  41. public static String format(Date date, String pattern) {
  42. if (date != null) {
  43. SimpleDateFormat df = new SimpleDateFormat(pattern);
  44. return df.format(date);
  45. }
  46. return null;
  47. }
  48. /**
  49. * 字符串转换成日期
  50. * @param strDate 日期字符串
  51. * @param pattern 日期的格式,如:DateUtils.DATE_TIME_PATTERN
  52. */
  53. public static Date stringToDate(String strDate, String pattern) {
  54. if (StringUtils.isBlank(strDate)) {
  55. return null;
  56. }
  57. DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
  58. return fmt.parseLocalDateTime(strDate).toDate();
  59. }
  60. /**
  61. * 根据周数,获取开始日期、结束日期
  62. * @param week 周期 0本周,-1上周,-2上上周,1下周,2下下周
  63. * @return 返回date[0]开始日期、date[1]结束日期
  64. */
  65. public static Date[] getWeekStartAndEnd(int week) {
  66. DateTime dateTime = new DateTime();
  67. LocalDate date = new LocalDate(dateTime.plusWeeks(week));
  68. date = date.dayOfWeek().withMinimumValue();
  69. Date beginDate = date.toDate();
  70. Date endDate = date.plusDays(6).toDate();
  71. return new Date[]{beginDate, endDate};
  72. }
  73. /**
  74. * 对日期的【秒】进行加/减
  75. *
  76. * @param date 日期
  77. * @param seconds 秒数,负数为减
  78. * @return 加/减几秒后的日期
  79. */
  80. public static Date addDateSeconds(Date date, int seconds) {
  81. DateTime dateTime = new DateTime(date);
  82. return dateTime.plusSeconds(seconds).toDate();
  83. }
  84. /**
  85. * 对日期的【分钟】进行加/减
  86. *
  87. * @param date 日期
  88. * @param minutes 分钟数,负数为减
  89. * @return 加/减几分钟后的日期
  90. */
  91. public static Date addDateMinutes(Date date, int minutes) {
  92. DateTime dateTime = new DateTime(date);
  93. return dateTime.plusMinutes(minutes).toDate();
  94. }
  95. /**
  96. * 对日期的【小时】进行加/减
  97. *
  98. * @param date 日期
  99. * @param hours 小时数,负数为减
  100. * @return 加/减几小时后的日期
  101. */
  102. public static Date addDateHours(Date date, int hours) {
  103. DateTime dateTime = new DateTime(date);
  104. return dateTime.plusHours(hours).toDate();
  105. }
  106. /**
  107. * 对日期的【天】进行加/减
  108. *
  109. * @param date 日期
  110. * @param days 天数,负数为减
  111. * @return 加/减几天后的日期
  112. */
  113. public static Date addDateDays(Date date, int days) {
  114. DateTime dateTime = new DateTime(date);
  115. return dateTime.plusDays(days).toDate();
  116. }
  117. /**
  118. * 对日期的【周】进行加/减
  119. *
  120. * @param date 日期
  121. * @param weeks 周数,负数为减
  122. * @return 加/减几周后的日期
  123. */
  124. public static Date addDateWeeks(Date date, int weeks) {
  125. DateTime dateTime = new DateTime(date);
  126. return dateTime.plusWeeks(weeks).toDate();
  127. }
  128. /**
  129. * 对日期的【月】进行加/减
  130. *
  131. * @param date 日期
  132. * @param months 月数,负数为减
  133. * @return 加/减几月后的日期
  134. */
  135. public static Date addDateMonths(Date date, int months) {
  136. DateTime dateTime = new DateTime(date);
  137. return dateTime.plusMonths(months).toDate();
  138. }
  139. /**
  140. * 对日期的【年】进行加/减
  141. *
  142. * @param date 日期
  143. * @param years 年数,负数为减
  144. * @return 加/减几年后的日期
  145. */
  146. public static Date addDateYears(Date date, int years) {
  147. DateTime dateTime = new DateTime(date);
  148. return dateTime.plusYears(years).toDate();
  149. }
  150. /**
  151. * 常规自动日期格式识别
  152. * @param str 时间字符串
  153. * @return Date
  154. * @author dc
  155. */
  156. public static String getDateFormat(String str) {
  157. boolean year = false;
  158. Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
  159. if (pattern.matcher(str.substring(0, 4)).matches()) {
  160. year = true;
  161. }
  162. StringBuilder sb = new StringBuilder();
  163. int index = 0;
  164. if (!year) {
  165. if (str.contains("月") || str.contains("-") || str.contains("/") || str.contains(".")) {
  166. if (Character.isDigit(str.charAt(0))) {
  167. index = 1;
  168. }
  169. } else {
  170. index = 3;
  171. }
  172. }
  173. for (int i = 0; i < str.length(); i++) {
  174. char chr = str.charAt(i);
  175. if (Character.isDigit(chr)) {
  176. if (index == 0) {
  177. sb.append("y");
  178. }
  179. if (index == 1) {
  180. sb.append("M");
  181. }
  182. if (index == 2) {
  183. sb.append("d");
  184. }
  185. if (index == 3) {
  186. sb.append("H");
  187. }
  188. if (index == 4) {
  189. sb.append("m");
  190. }
  191. if (index == 5) {
  192. sb.append("s");
  193. }
  194. if (index == 6) {
  195. sb.append("S");
  196. }
  197. } else {
  198. if (i > 0) {
  199. char lastChar = str.charAt(i - 1);
  200. if (Character.isDigit(lastChar)) {
  201. index++;
  202. }
  203. }
  204. sb.append(chr);
  205. }
  206. }
  207. return sb.toString();
  208. }
  209. }