TimeExchange.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package com.happy.Until;
  2. import org.apache.commons.lang.time.DateUtils;
  3. import java.sql.Timestamp;
  4. import java.text.DateFormat;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. /**
  10. * 时间转化工具 date转为时间戳 时间戳转date 互相与String的转换
  11. * 所有出现的String time 格式都必须为(yyyy-MM-dd HH:mm:ss),否则出错
  12. * @author 赵仁杰
  13. *
  14. */
  15. public class TimeExchange {
  16. /**
  17. * String(yyyy-MM-dd HH:mm:ss) 转 Date
  18. *
  19. * @param time
  20. * @return
  21. * @throws ParseException
  22. */
  23. // String date = "2010/05/04 12:34:23";
  24. public static Date StringToDate(String time) throws ParseException {
  25. Date date = new Date();
  26. // 注意format的格式要与日期String的格式相匹配
  27. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  28. try {
  29. date = dateFormat.parse(time);
  30. System.out.println(date.toString());
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. return date;
  35. }
  36. /**
  37. * Date转为String(yyyy-MM-dd HH:mm:ss)
  38. *
  39. * @param time
  40. * @return
  41. */
  42. public static String DateToString(Date time) {
  43. String dateStr = "";
  44. Date date = new Date();
  45. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss");
  46. try {
  47. dateStr = dateFormat.format(time);
  48. System.out.println(dateStr);
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. return dateStr;
  53. }
  54. /**
  55. * String(yyyy-MM-dd HH:mm:ss)转10位时间戳
  56. * @param time
  57. * @return
  58. */
  59. public static Integer StringToTimestamp(String time){
  60. int times = 0;
  61. try {
  62. times = (int) ((Timestamp.valueOf(time).getTime())/1000);
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. if(times==0){
  67. System.out.println("String转10位时间戳失败");
  68. }
  69. return times;
  70. }
  71. /**
  72. * 10位int型的时间戳转换为String(yyyy-MM-dd HH:mm:ss)
  73. * @param time
  74. * @return
  75. */
  76. public static String timestampToString(Integer time){
  77. //int转long时,先进行转型再进行计算,否则会是计算结束后在转型
  78. long temp = (long)time*1000;
  79. Timestamp ts = new Timestamp(temp);
  80. String tsStr = "";
  81. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  82. try {
  83. //方法一
  84. tsStr = dateFormat.format(ts);
  85. System.out.println(tsStr);
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. return tsStr;
  90. }
  91. /**
  92. * 10位时间戳转Date
  93. * @param time
  94. * @return
  95. */
  96. public static Date TimestampToDate(Integer time){
  97. long temp = (long)time*1000;
  98. Timestamp ts = new Timestamp(temp);
  99. Date date = new Date();
  100. try {
  101. date = ts;
  102. //System.out.println(date);
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. return date;
  107. }
  108. /**
  109. * Date类型转换为10位时间戳
  110. * @param time
  111. * @return
  112. */
  113. public static Integer DateToTimestamp(Date time){
  114. Timestamp ts = new Timestamp(time.getTime());
  115. return (int) ((ts.getTime())/1000);
  116. }
  117. // 当前时间加5分钟
  118. public static String TimeRangeI(String time) throws ParseException {
  119. // 当前时间+5分钟
  120. Date endTime = DateUtils.addMinutes(StringToDate(time), 300);
  121. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  122. return simpleDateFormat.format(endTime);
  123. }
  124. // 当前时间加2分钟
  125. public static String TimeRangeI10(String time,int m) throws ParseException {
  126. Calendar nowTime2 = Calendar.getInstance();
  127. nowTime2.setTime(StringToDate(time));
  128. nowTime2.add(Calendar.SECOND, m);//10分钟前的时间
  129. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  130. return simpleDateFormat.format(nowTime2.getTime());
  131. }
  132. // 当前时间减5分钟
  133. public static String TimeRangeD(String time) throws ParseException {
  134. Calendar nowTime2 = Calendar.getInstance();
  135. nowTime2.setTime(StringToDate(time));
  136. nowTime2.add(Calendar.MINUTE, -300);//5分钟前的时间
  137. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  138. return simpleDateFormat.format(nowTime2.getTime());
  139. }
  140. // 获取当前日期
  141. public static String getDate(){
  142. SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
  143. return sp.format(new Date());
  144. }
  145. // 获取当前时间
  146. public static String getTime(){
  147. SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  148. return sp.format(new Date());
  149. }
  150. public static String getYear(){
  151. SimpleDateFormat sp = new SimpleDateFormat("yyyy");
  152. return sp.format(new Date());
  153. }
  154. public static String getMonth(){
  155. SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM");
  156. return sp.format(new Date());
  157. }
  158. /**
  159. * 计算两个日期的时间差
  160. * @param time1
  161. * @param time2
  162. * @return
  163. */
  164. public static double getTimeDifference(String time1, String time2) {
  165. SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  166. long t1 = 0L;
  167. long t2 = 0L;
  168. try {
  169. t1 = timeformat.parse(time1).getTime();
  170. } catch (ParseException e) {
  171. // TODO Auto-generated catch block
  172. e.printStackTrace();
  173. }
  174. try {
  175. t2 = timeformat.parse(time2).getTime();
  176. } catch (ParseException e) {
  177. // TODO Auto-generated catch block
  178. e.printStackTrace();
  179. }
  180. //因为t1-t2得到的是毫秒级,所以要初3600000得出小时.算天数或秒同理
  181. double hours=(double) ((t2 - t1)/3600000);
  182. double minutes=(double) (((t2 - t1)/1000-hours*3600)/60/60);
  183. return hours+minutes;
  184. }
  185. public static void main(String[] args) throws ParseException {
  186. String time1 = "2022-07-22 11:21:20";
  187. String time2 = "2022-07-22 12:31:20";
  188. System.out.println(getTimeDifference(time1,time2));
  189. }
  190. }