DateUtil.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package cn.efunbox.audio.utils;
  2. import org.apache.commons.lang3.time.DateFormatUtils;
  3. import org.apache.commons.lang3.time.DateUtils;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Calendar;
  9. import java.util.Date;
  10. import java.util.Locale;
  11. /**
  12. * 日期工具类
  13. */
  14. public class DateUtil {
  15. private static Logger log = LoggerFactory.getLogger(DateUtil.class);
  16. private static final String DEFAULT_PATTERN = "yyyy-MM-dd";
  17. /**
  18. * 一天的毫秒数
  19. */
  20. public static long TIME_OF_ONE_DAY = 24 * 60 * 60 * 1000;
  21. /**
  22. * 将日期转换为字符串
  23. *
  24. * @return
  25. */
  26. public static String dateFormat() {
  27. return dateFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
  28. }
  29. /**
  30. * 将日期转换为字符串
  31. *
  32. * @return
  33. */
  34. public static String dateFormatStr(Date date) {
  35. return dateFormat(date, "yyyyMMddHHmmssSSS");
  36. }
  37. public static String dateFormat(String pattern) {
  38. return dateFormat(new Date(), pattern);
  39. }
  40. public static String dateFormat(Date date, String pattern) {
  41. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  42. return sdf.format(date);
  43. }
  44. /**
  45. * 字符串转换为日期
  46. *
  47. * @param source
  48. * @return
  49. * @throws ParseException
  50. */
  51. public static Date strToDate(String source) {
  52. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  53. try {
  54. return sdf.parse(source);
  55. } catch (ParseException e) {
  56. return null;
  57. }
  58. }
  59. public static Date StrToDate(String source, String pattern) throws ParseException {
  60. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  61. return sdf.parse(source);
  62. }
  63. /**
  64. * 增加年
  65. *
  66. * @param date
  67. * @param i
  68. * @return
  69. */
  70. public static Date addYears(Date date, int i) {
  71. return DateUtils.addYears(date, i);
  72. }
  73. /**
  74. * 增加月
  75. *
  76. * @param date
  77. * @param i
  78. * @return
  79. */
  80. public static Date addMonths(Date date, int i) {
  81. return DateUtils.addMonths(date, i);
  82. }
  83. /**
  84. * 增加小时
  85. *
  86. * @param date
  87. * @param amount
  88. * @return
  89. */
  90. public static Date addHours(Date date, int amount) {
  91. return DateUtils.addHours(date, amount);
  92. }
  93. /**
  94. * 增加天数
  95. *
  96. * @param date
  97. * @param amount
  98. * @return
  99. */
  100. public static Date addDays(Date date, int amount) {
  101. return DateUtils.addDays(date, amount);
  102. }
  103. /**
  104. * 日期格式换行为GMT格式
  105. *
  106. * @param date
  107. * @return
  108. */
  109. public static String toGMTString(Date date) {
  110. SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
  111. df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
  112. return df.format(date);
  113. }
  114. /**
  115. * 获取传入日期和当前当前日期相隔分钟数
  116. *
  117. * @param date
  118. * @return
  119. * @throws ParseException
  120. */
  121. public static int getDateDiffMinutes(String date){
  122. long intervalMilli = 0;
  123. try {
  124. long now = System.currentTimeMillis();
  125. long ago = StrToDate(date, "yyyy-MM-dd HH:mm:ss").getTime();
  126. intervalMilli = now - ago;
  127. if (intervalMilli < 0) {
  128. return 0;
  129. }
  130. }catch (Exception e){
  131. e.printStackTrace();
  132. return 0;
  133. }
  134. return (int)(intervalMilli / (60 * 1000));
  135. }
  136. /**
  137. * 获取传入日期和当前当前日期相隔天数
  138. *
  139. * @param date
  140. * @return
  141. * @throws ParseException
  142. */
  143. public static int getDateDiffDays(String date) throws ParseException {
  144. long now = new Date().getTime();
  145. long ago = StrToDate(date, "yyyy-MM-dd").getTime();
  146. long intervalMilli = now - ago;
  147. if (intervalMilli < 0) {
  148. return 0;
  149. }
  150. return (int) (intervalMilli / (24 * 3600 * 1000));
  151. }
  152. /**
  153. * 获取传入日期和当前当前日期相隔年数
  154. *
  155. * @param date
  156. * @return
  157. * @throws ParseException
  158. */
  159. public static int getDateDiffYears(Date date) {
  160. Calendar cal1 = Calendar.getInstance();
  161. cal1.setTime(date);
  162. Calendar cal2 = Calendar.getInstance();
  163. cal2.setTime(new Date());
  164. return cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR);
  165. }
  166. /**
  167. * 将DATE类型转成String类型,pattern默认为yyyy-MM-dd
  168. *
  169. * @return
  170. */
  171. public static String getDateStr() {
  172. Date date = new Date();
  173. SimpleDateFormat formater = new SimpleDateFormat(DEFAULT_PATTERN);
  174. return formater.format(date);
  175. }
  176. public static String getPreDateStr() {
  177. Date date = new Date(System.currentTimeMillis() - TIME_OF_ONE_DAY);
  178. SimpleDateFormat formater = new SimpleDateFormat(DEFAULT_PATTERN);
  179. return formater.format(date);
  180. }
  181. public static String formatPreDayEndTime() {
  182. Date date = new Date(System.currentTimeMillis() - TIME_OF_ONE_DAY);
  183. return DateFormatUtils.format(date, "yyyy-MM-dd 23:59:59");
  184. }
  185. public static String formatPreDayStartTime() {
  186. Date date = new Date(System.currentTimeMillis() - TIME_OF_ONE_DAY);
  187. return DateFormatUtils.format(date, "yyyy-MM-dd 00:00:00");
  188. }
  189. public static Date getPreDayStartTime() {
  190. String format = DateFormatUtils.format(new Date(System.currentTimeMillis() - TIME_OF_ONE_DAY), "yyyy-MM-dd 00:00:00");
  191. java.text.DateFormat dfNew = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  192. try {
  193. return dfNew.parse(format);
  194. } catch (ParseException e) {
  195. e.printStackTrace();
  196. }
  197. return new Date();
  198. }
  199. public static void main(String[] args) {
  200. Date preDayStartTime = getPreDayStartTime();
  201. //System.out.println(preDayStartTime);
  202. //System.out.println(formatPreDayEndTime());
  203. System.out.println(dateFormat());
  204. }
  205. }