TimeTools.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package com.it.ocs.salesStatistics.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.TimeZone;
  7. import org.apache.commons.lang.StringUtils;
  8. public class TimeTools {
  9. private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  10. private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  11. // 获取当前时间
  12. public static Date getStoptime() throws ParseException {
  13. Date date = new Date();
  14. String string = format.format(date);
  15. string = string + " 00:00:00";
  16. return sdf.parse(string);
  17. }
  18. public static Date getEndTime() throws Exception {
  19. return format.parse(format.format(new Date()));
  20. }
  21. // 月份加减
  22. public static Date getChangeMonth(Date date, int i) {
  23. Calendar calendar = Calendar.getInstance();
  24. calendar.setTime(date);
  25. calendar.add(Calendar.MONTH, i);
  26. return calendar.getTime();
  27. }
  28. // 天数加减
  29. public static Date getChangeDay(Date date, int i) {
  30. Calendar calendar = Calendar.getInstance();
  31. calendar.setTime(date);
  32. calendar.add(Calendar.DATE, i);
  33. return calendar.getTime();
  34. }
  35. // 前台传的字符串时间转化成data
  36. public static Date getTime(String starttime) throws ParseException {
  37. starttime = starttime + " 00:00:00";
  38. return sdf.parse(starttime);
  39. }
  40. // 前台传的字符串时间转化成data
  41. public static Date getTime2(String time) throws ParseException {
  42. if (StringUtils.isNotBlank(time)) {
  43. return sdf.parse(time);
  44. }
  45. return null;
  46. }
  47. public static Date strToDate(String time) throws ParseException {
  48. return format.parse(time);
  49. }
  50. // 时间差
  51. @SuppressWarnings("unused")
  52. public static int getMistiming(String starTime, String endTime) throws Exception {
  53. Calendar calendar = Calendar.getInstance();
  54. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  55. Date star = format.parse(starTime);
  56. Date end = format.parse(endTime);
  57. calendar.setTime(star);
  58. long time1 = calendar.getTimeInMillis();
  59. calendar.setTime(end);
  60. long time2 = calendar.getTimeInMillis();
  61. long between_days = (time1 - time2) / (1000 * 3600 * 24);
  62. return Integer.parseInt(String.valueOf(between_days));
  63. }
  64. // 前推一年
  65. @SuppressWarnings({ "unused", "static-access" })
  66. public static Date getChangeYear(Date date) {
  67. Calendar calendar = Calendar.getInstance();
  68. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  69. calendar.setTime(date);
  70. calendar.add(calendar.YEAR, -1);
  71. return calendar.getTime();
  72. }
  73. // 时间类型转化成String
  74. public static String dateToString(Date createdat) {
  75. return format.format(createdat);
  76. }
  77. // 时间类型转化成String
  78. public static String dateToString2(Date createdat) {
  79. return sdf.format(createdat);
  80. }
  81. /**
  82. * 时区 时间转换方法:将当前时间(可能为其他时区)转化成目标时区对应的时间
  83. *
  84. * @param sourceTime
  85. * 时间格式必须为:yyyy-MM-dd HH:mm:ss
  86. * @param sourceId
  87. * 入参的时间的时区id
  88. * @param targetId
  89. * 要转换成目标时区id(一般是是零时区:取值UTC)
  90. * @return string 转化时区后的时间
  91. */
  92. public static String timeConvert(String sourceTime, String sourceId, String targetId) {
  93. // 校验入参是否合法
  94. if (null == sourceId || "".equals(sourceId) || null == targetId || "".equals(targetId) || null == sourceTime
  95. || "".equals(sourceTime)) {
  96. return "";
  97. }
  98. // 校验 时间格式必须为:yyyy-MM-dd HH:mm:ss
  99. String reg = "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";
  100. if (!sourceTime.matches(reg)) {
  101. return "";
  102. }
  103. try {
  104. // 时间格式
  105. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  106. // 根据入参原时区id,获取对应的timezone对象
  107. TimeZone sourceTimeZone = TimeZone.getTimeZone(sourceId);
  108. // 设置SimpleDateFormat时区为原时区(否则是本地默认时区),目的:用来将字符串sourceTime转化成原时区对应的date对象
  109. df.setTimeZone(sourceTimeZone);
  110. // 将字符串sourceTime转化成原时区对应的date对象
  111. Date sourceDate = df.parse(sourceTime);
  112. // 开始转化时区:根据目标时区id设置目标TimeZone
  113. TimeZone targetTimeZone = TimeZone.getTimeZone(targetId);
  114. // 设置SimpleDateFormat时区为目标时区(否则是本地默认时区),目的:用来将字符串sourceTime转化成目标时区对应的date对象
  115. df.setTimeZone(targetTimeZone);
  116. // 得到目标时间字符串
  117. String targetTime = df.format(sourceDate);
  118. return targetTime;
  119. } catch (ParseException e) {
  120. e.printStackTrace();
  121. }
  122. return "";
  123. }
  124. // 时间比较大小
  125. public static boolean compare1(Date startDate, Date endDate, Date invoke) {
  126. Boolean boolean1 = false;
  127. if (invoke.getTime() >= startDate.getTime() && invoke.getTime() < endDate.getTime()) {
  128. boolean1 = true;
  129. }
  130. return boolean1;
  131. }
  132. // 时间比较大小
  133. public static boolean compare2(String startDate, String endDate, String invoke) {
  134. Boolean boolean1 = false;
  135. if (invoke.compareTo(startDate) > -1 && invoke.compareTo(endDate) < 1) {
  136. boolean1 = true;
  137. }
  138. return boolean1;
  139. }
  140. public static boolean compare3(String startDate, String endDate, String invoke) {
  141. Boolean boolean1 = false;
  142. String string = "";
  143. if (StringUtils.isNotBlank(startDate)) {
  144. string = startDate;
  145. } else {
  146. string = endDate;
  147. }
  148. if (invoke.compareTo(string) == 0) {
  149. boolean1 = true;
  150. }
  151. return boolean1;
  152. }
  153. /**
  154. * 判断两个字符串的时间是否是一个月的第一天和最后一天
  155. *
  156. * @param starttime
  157. * 开始时间
  158. * @param endtime
  159. * 结束时间
  160. * @return
  161. * @throws ParseException
  162. */
  163. public static boolean judge(String starttime, String endtime) throws ParseException {
  164. Boolean flag = false;
  165. Date start = format.parse(starttime);
  166. Date end = format.parse(endtime);
  167. Calendar calendar = Calendar.getInstance();
  168. calendar.setTime(end);
  169. int a = calendar.get(calendar.YEAR);
  170. calendar.setTime(start);
  171. int b = calendar.get(calendar.YEAR);
  172. if (a == b) {
  173. a = calendar.get(Calendar.MONTH);
  174. calendar.setTime(end);
  175. b = calendar.get(Calendar.MONTH);
  176. if (a == b) {
  177. calendar.set(Calendar.DATE, (calendar.get(Calendar.DATE) + 1));
  178. if (calendar.get(Calendar.DAY_OF_MONTH) == 1) {
  179. calendar.setTime(start);
  180. int actualMinimum = calendar.get(Calendar.DAY_OF_MONTH);
  181. if (actualMinimum == 1) {
  182. flag = true;
  183. }
  184. }
  185. }
  186. }
  187. return flag;
  188. }
  189. public static void main(String[] args) throws ParseException {
  190. System.out.println(timeConvert(sdf.format(new Date()), "Asia/Shanghai", "UTC"));
  191. System.out.println(judge("2017-03-01", "2017-03-31"));
  192. }
  193. // 时间转化
  194. public static Date getTimeByStation(Date date, String key) throws ParseException {
  195. String sourceId = Tools.getSoreceId(key);
  196. if(StringUtils.isBlank(sourceId)) {
  197. return date;
  198. }
  199. String timeConvert = timeConvert(dateToString2(date), sourceId, "UTC");
  200. Date result = getTime2(timeConvert);
  201. return result;
  202. }
  203. /**
  204. *
  205. * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
  206. *
  207. * @param date
  208. * @return
  209. */
  210. public static int getSeason(String date) {
  211. try {
  212. int season = 0;
  213. SimpleDateFormat format_ = new SimpleDateFormat("yyyy-MM");
  214. Calendar c = Calendar.getInstance();
  215. c.setTime(format_.parse(date));
  216. int month = c.get(Calendar.MONTH);
  217. switch (month) {
  218. case Calendar.JANUARY:
  219. case Calendar.FEBRUARY:
  220. case Calendar.MARCH:
  221. season = 1;
  222. break;
  223. case Calendar.APRIL:
  224. case Calendar.MAY:
  225. case Calendar.JUNE:
  226. season = 2;
  227. break;
  228. case Calendar.JULY:
  229. case Calendar.AUGUST:
  230. case Calendar.SEPTEMBER:
  231. season = 3;
  232. break;
  233. case Calendar.OCTOBER:
  234. case Calendar.NOVEMBER:
  235. case Calendar.DECEMBER:
  236. season = 4;
  237. break;
  238. default:
  239. break;
  240. }
  241. return season;
  242. } catch (Exception e) {
  243. throw new RuntimeException(e.getMessage());
  244. }
  245. }
  246. public static String getLastDayOfMonth(int year, int month) {
  247. Calendar cal = Calendar.getInstance();
  248. cal.set(Calendar.YEAR, year);
  249. cal.set(Calendar.MONTH, month-1);
  250. cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DATE));
  251. return new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime());
  252. }
  253. public static String getFirstDayOfMonth(int year, int month) {
  254. Calendar cal = Calendar.getInstance();
  255. cal.set(Calendar.YEAR, year);
  256. cal.set(Calendar.MONTH, month-1);
  257. cal.set(Calendar.DAY_OF_MONTH,cal.getMinimum(Calendar.DATE));
  258. return new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime());
  259. }
  260. }