DateUtils.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package com.steerinfo.ems.Utils;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.List;
  8. public class DateUtils {
  9. /**
  10. * 日期转换为字符串 格式自定义
  11. *
  12. * @param date
  13. * @param timeformat
  14. * @return
  15. */
  16. public static String dateStr(Date date, String timeformat) {
  17. if (date == null) {
  18. return "";
  19. }
  20. String str = new SimpleDateFormat(timeformat).format(date);
  21. return str;
  22. }
  23. public static String dateStr(Date date) {
  24. if (date == null) {
  25. return "";
  26. }
  27. String str = new SimpleDateFormat("yyyy-mm-dd").format(date);
  28. return str;
  29. }
  30. /**
  31. * 时间戳转标准时间
  32. *
  33. * @param stap 时间戳
  34. * @return
  35. */
  36. public static String stampToDate(String stap){
  37. String time;
  38. try {
  39. if (stap == null || stap.length() == 0) {
  40. time = null;
  41. } else {
  42. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  43. Date date = null;
  44. try {
  45. long lt = new Long(stap);
  46. date = new Date(lt);
  47. }
  48. catch(Exception e) {
  49. date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(stap);
  50. }
  51. time = simpleDateFormat.format(date);
  52. }
  53. } catch (Exception e) {
  54. time = null;
  55. e.printStackTrace();
  56. }
  57. return time;
  58. }
  59. /**
  60. * 时间序列偏移量 偏移量自定义
  61. *
  62. * @param time
  63. * @param offsetinfo 偏移量
  64. * @return
  65. */
  66. public static Date offsetDate(Date time,String offsetinfo) {
  67. String[] offsetinfoarray = offsetinfo.split(",");
  68. Calendar cal = Calendar.getInstance();
  69. cal.setTime(time);
  70. for (int i =0;i < offsetinfoarray.length;i++){
  71. if(offsetinfoarray[i] != null && !"".equals(offsetinfoarray[i])){
  72. String timegran = offsetinfoarray[i].substring(0, offsetinfoarray[i].length() - 2);
  73. int offset = Integer.parseInt(offsetinfoarray[i].substring(offsetinfoarray[i].length() - 2));
  74. switch (timegran){
  75. case "ss":
  76. cal.add(Calendar.SECOND, offset);
  77. break;
  78. case "mm":
  79. cal.add(Calendar.MINUTE, offset);
  80. break;
  81. case "HH":
  82. cal.add(Calendar.HOUR, offset);
  83. break;
  84. case "dd":
  85. cal.add(Calendar.DATE, offset);
  86. break;
  87. case "MM":
  88. cal.add(Calendar.MONTH, offset);
  89. break;
  90. case "yyyy":
  91. cal.add(Calendar.YEAR, offset);
  92. break;
  93. }
  94. }
  95. }
  96. return cal.getTime();
  97. }
  98. public static List<String> getMonitortime(String timegran, String time, String starttime) {
  99. List<String> list = new ArrayList<String>();
  100. if("YEAR".equals(timegran)){
  101. list.add(time);
  102. }
  103. else{
  104. try {
  105. Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(starttime);//定义开始日期
  106. Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse((Integer.parseInt(time) + 1) + "-01-01");//定义结束日期
  107. Calendar dd = Calendar.getInstance();//定义日期实例
  108. dd.setTime(d1);//设置日期起始时间
  109. while(dd.getTime().before(d2)) {//判断是否到结束日期
  110. if ("MONTH".equals(timegran)) {
  111. list.add(dateStr(dd.getTime(), "yyyy-MM"));
  112. dd.add(Calendar.MONTH, 1);
  113. }
  114. else{
  115. list.add(dateStr(dd.getTime(), "yyyy-MM-dd"));
  116. dd.add(Calendar.DAY_OF_YEAR, 1);
  117. }
  118. }
  119. }
  120. catch (ParseException e){
  121. e.printStackTrace();
  122. }
  123. }
  124. return list;
  125. }
  126. /**
  127. * 自动补全日期并返回新日期 偏移量自定义
  128. *
  129. * @param time 时间
  130. * @param interval 偏移量
  131. * @return
  132. */
  133. public static String changeTime(String time,int interval){
  134. Calendar dd = Calendar.getInstance();
  135. try{
  136. if(time.length() == 4){
  137. Date d = new SimpleDateFormat("yyyy-MM-dd").parse(time+"-01-01");
  138. dd.setTime(d);
  139. dd.add(Calendar.YEAR, interval);
  140. }
  141. if(time.length() == 7){
  142. Date d = new SimpleDateFormat("yyyy-MM-dd").parse(time+"-01");
  143. dd.setTime(d);
  144. dd.add(Calendar.MONTH, interval);
  145. }
  146. if(time.length() == 10){
  147. Date d = new SimpleDateFormat("yyyy-MM-dd").parse(time);
  148. dd.setTime(d);
  149. dd.add(Calendar.DATE, interval);
  150. }
  151. }
  152. catch (ParseException e){
  153. e.printStackTrace();
  154. }
  155. return dateStr(dd.getTime(), "yyyy-MM-dd");
  156. }
  157. /**
  158. * 获取现在时间
  159. * @Param timeformat 时间格式
  160. * @return(String)格式自定义
  161. */
  162. public static String getCurrentTime(String timeformat){
  163. Date time = new Date();
  164. return dateStr(time, timeformat);
  165. }
  166. /**
  167. * 获取现在时间
  168. * @Param timeformat 时间格式
  169. * @return(Date)格式自定义
  170. */
  171. public static Date getCurrentTimetoDate(String timeformat) {
  172. try {
  173. Date date = new java.sql.Date(new Date().getTime());
  174. String datestr = new SimpleDateFormat("yyyy-mm-dd").format(date);
  175. Date time = new SimpleDateFormat(timeformat).parse(datestr);
  176. return time;
  177. }
  178. catch (ParseException e){
  179. e.printStackTrace();
  180. return null;
  181. }
  182. }
  183. /**
  184. * 将"yyyy-MM-dd"转换为 格式自定义
  185. *
  186. * @param date
  187. * @param timeformat
  188. * @return
  189. */
  190. public static String dateToString(String date, String timeformat) {
  191. String Strtime = "";
  192. try {
  193. if(date != null && !"".equals(date)) {
  194. Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date + " 00:00:00");
  195. Strtime = new SimpleDateFormat(timeformat).format(parse);
  196. }
  197. }
  198. catch (ParseException e) {
  199. e.printStackTrace();
  200. }
  201. return Strtime;
  202. }
  203. /**
  204. * 判断某天是否是该月的第一天
  205. * @param date
  206. * @return
  207. */
  208. public static boolean isFirstDayOfMonth(Date date) {
  209. Calendar calendar = Calendar.getInstance();
  210. calendar.setTime(date);
  211. System.out.println(calendar.get(Calendar.MONTH));
  212. return calendar.get(Calendar.DAY_OF_MONTH) == 1;
  213. }
  214. public static String getDate(){
  215. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  216. Date dt = null;
  217. String reStr = null;
  218. try {
  219. dt = sdf.parse(DateUtils.dateStr(new Date(),"yyyy-MM-dd"));
  220. Calendar rightNow = Calendar.getInstance();
  221. rightNow.setTime(dt);
  222. rightNow.add(Calendar.DAY_OF_MONTH, -3);
  223. Date dt1 = rightNow.getTime();
  224. reStr = sdf.format(dt1);
  225. } catch (ParseException e) {
  226. e.printStackTrace();
  227. }
  228. return reStr ;
  229. }
  230. /**
  231. * 根据月份得到当月天数
  232. * @param date
  233. * @return
  234. * @throws ParseException
  235. */
  236. public static int getDaysOfMonth(String date) throws ParseException {
  237. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
  238. Calendar calendar = Calendar.getInstance();
  239. calendar.setTime(sdf.parse(date));
  240. return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  241. }
  242. /**
  243. * 根据月份得到当月天数
  244. * @param date
  245. * @return
  246. * @throws ParseException
  247. */
  248. public static int getDaysOfMonth(Date date) throws ParseException {
  249. Calendar calendar = Calendar.getInstance();
  250. calendar.setTime(date);
  251. return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  252. }
  253. }