TimeUtils.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. package com.xzjmyk.pm.activity.util;
  2. import android.content.Context;
  3. import android.text.TextUtils;
  4. import android.widget.TextView;
  5. import com.xzjmyk.pm.activity.R;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Calendar;
  9. import java.util.Date;
  10. @SuppressWarnings("deprecation")
  11. public class TimeUtils {
  12. // ///s 代表Simple日期格式:yyyy-MM-dd
  13. // ///f 代表Full日期格式:yyyy-MM-dd hh:mm:ss
  14. public static final SimpleDateFormat ss_format = new SimpleDateFormat("MM-dd");
  15. public static final SimpleDateFormat day_format = new SimpleDateFormat("yyyy年MM月dd日");
  16. public static final SimpleDateFormat s_format = new SimpleDateFormat("yyyy-MM-dd");
  17. public static final SimpleDateFormat f_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  18. public static final SimpleDateFormat int_format = new SimpleDateFormat("yyyyMMdd");
  19. public static final SimpleDateFormat ym_format = new SimpleDateFormat("yyyyMM");
  20. public static String getWeek(String date) { //yyyy-MM-dd
  21. Calendar c = Calendar.getInstance();
  22. c.setTimeInMillis(TimeUtils.s_str_2_long(date));
  23. String src = "";
  24. int week = c.get(Calendar.DAY_OF_WEEK);
  25. switch (week) {
  26. case 1:
  27. src = "星期天";
  28. break;
  29. case 2:
  30. src = "星期一";
  31. break;
  32. case 3:
  33. src = "星期二";
  34. break;
  35. case 4:
  36. src = "星期三";
  37. break;
  38. case 5:
  39. src = "星期四";
  40. break;
  41. case 6:
  42. src = "星期五";
  43. break;
  44. case 7:
  45. src = "星期六";
  46. break;
  47. }
  48. return src;
  49. }
  50. public static long s_str_2_long(String dateString) {
  51. try {
  52. Date d = s_format.parse(dateString);
  53. return d.getTime();
  54. } catch (ParseException e) {
  55. e.printStackTrace();
  56. }
  57. return 0;
  58. }
  59. public static String day_long_2_str(long timestamp) {
  60. return day_format.format(new Date(timestamp));
  61. }
  62. public static long int_str_2_long(String date) {
  63. try {
  64. Date d = int_format.parse(date);
  65. return d.getTime();
  66. } catch (ParseException e) {
  67. e.printStackTrace();
  68. }
  69. return 0;
  70. }
  71. public static int int_long_2_str(long timestamp) {
  72. return Integer.parseInt(int_format.format(new Date(timestamp)));
  73. }
  74. public static int ym_long_2_str(long timestamp) {
  75. return Integer.parseInt(ym_format.format(new Date(timestamp)));
  76. }
  77. public static long f_str_2_long(String dateString) {
  78. try {
  79. Date d = f_format.parse(dateString);
  80. return d.getTime();
  81. } catch (ParseException e) {
  82. e.printStackTrace();
  83. }
  84. return 0;
  85. }
  86. public static String ss_long_2_str(long timestamp) {
  87. return ss_format.format(new Date(timestamp));
  88. }
  89. public static String s_long_2_str(long timestamp) {
  90. return s_format.format(new Date(timestamp));
  91. }
  92. public static String f_long_2_str(long timestamp) {
  93. return f_format.format(new Date(timestamp));
  94. }
  95. /**
  96. * 获取字符串时间的年份
  97. *
  98. * @param dateString 格式为yyyy-MM-ss,或者yyyy-MM-dd HH:mm:ss
  99. * @return
  100. */
  101. public static int getYear(String dateString) {
  102. try {
  103. Date d = s_format.parse(dateString);
  104. return d.getYear() + 1900;// 年份是基于格林威治时间,所以加上1900
  105. } catch (ParseException e) {
  106. e.printStackTrace();
  107. }
  108. return 0;
  109. }
  110. /**
  111. * 获取字符串时间的月份
  112. *
  113. * @param dateString 格式为yyyy-MM-ss,或者yyyy-MM-dd hh:mm:ss
  114. * @return
  115. */
  116. public static int getMonth(String dateString) {
  117. try {
  118. Date d = s_format.parse(dateString);
  119. return d.getMonth();// 月份从0-11
  120. } catch (ParseException e) {
  121. // TODO Auto-generated catch block
  122. e.printStackTrace();
  123. }
  124. return 0;
  125. }
  126. /**
  127. * 获取字符串时间的天
  128. *
  129. * @param dateString 格式为yyyy-MM-ss,或者yyyy-MM-dd hh:mm:ss
  130. * @return
  131. */
  132. public static int getDayOfMonth(String dateString) {
  133. try {
  134. Date d = s_format.parse(dateString);
  135. return d.getDate();
  136. } catch (ParseException e) {
  137. // TODO Auto-generated catch block
  138. e.printStackTrace();
  139. }
  140. return 0;
  141. }
  142. public static int getHours(String timeString) {
  143. SimpleDateFormat formart = new SimpleDateFormat("HH:mm:ss");
  144. try {
  145. Date date = formart.parse(timeString);
  146. return date.getHours();
  147. } catch (ParseException e) {
  148. // TODO Auto-generated catch block
  149. e.printStackTrace();
  150. }
  151. return 0;
  152. }
  153. public static int getMinutes(String timeString) {
  154. SimpleDateFormat formart = new SimpleDateFormat("HH:mm:ss");
  155. try {
  156. Date date = formart.parse(timeString);
  157. return date.getMinutes();
  158. } catch (ParseException e) {
  159. // TODO Auto-generated catch block
  160. e.printStackTrace();
  161. }
  162. return 0;
  163. }
  164. public static int getSeconds(String timeString) {
  165. SimpleDateFormat formart = new SimpleDateFormat("HH:mm:ss");
  166. try {
  167. Date date = formart.parse(timeString);
  168. return date.getSeconds();
  169. } catch (ParseException e) {
  170. // TODO Auto-generated catch block
  171. e.printStackTrace();
  172. }
  173. return 0;
  174. }
  175. public static String getCurrentTime() {
  176. return f_format.format(new Date(System.currentTimeMillis()));
  177. }
  178. /**
  179. * 在当前时间上加上多少毫秒,返回这个时间
  180. *
  181. * @param mask
  182. * @return
  183. */
  184. public static String getCurrentTimeMask(long mask) {
  185. return f_format.format(new Date(System.currentTimeMillis() + mask));
  186. }
  187. // /////////////////////以上是通用的,下面为特殊需求的////////////////////////
  188. // /**
  189. // * 时间戳转换日期格式
  190. // *
  191. // * @param timestamp
  192. // * 单位秒
  193. // * @return
  194. // */
  195. // public static String getCurrentTime(long timestamp) {
  196. // SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  197. // return f.format(new Date(timestamp * 1000));
  198. // }
  199. /**
  200. * 获取精简的日期
  201. *
  202. * @param time
  203. * @return
  204. */
  205. public static String getSimpleDate(String time) {
  206. SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
  207. Date date = null;
  208. try {
  209. date = f_format.parse(time);
  210. return formater.format(date);
  211. } catch (ParseException e) {
  212. e.printStackTrace();
  213. return "";
  214. }
  215. }
  216. /**
  217. * @param time
  218. * @return
  219. */
  220. public static String getSimpleDateTime(String time) {
  221. SimpleDateFormat formater = new SimpleDateFormat("yy-MM-dd HH:mm");
  222. Date date = null;
  223. try {
  224. date = f_format.parse(time);
  225. return formater.format(date);
  226. } catch (ParseException e) {
  227. e.printStackTrace();
  228. return "";
  229. }
  230. }
  231. public static String getSimpleTime(String time) {
  232. SimpleDateFormat formater = new SimpleDateFormat("HH:mm");
  233. Date date = null;
  234. try {
  235. date = f_format.parse(time);
  236. return formater.format(date);
  237. } catch (ParseException e) {
  238. e.printStackTrace();
  239. return "";
  240. }
  241. }
  242. public static String getChatSimpleDate(String time) {
  243. SimpleDateFormat formater = new SimpleDateFormat("yy-MM-dd");
  244. Date date = null;
  245. try {
  246. date = f_format.parse(time);
  247. return formater.format(date);
  248. } catch (ParseException e) {
  249. e.printStackTrace();
  250. return "";
  251. }
  252. }
  253. public static String getTimeHM(String time) {
  254. SimpleDateFormat formater = new SimpleDateFormat("HH:mm");
  255. Date date = null;
  256. try {
  257. date = f_format.parse(time);
  258. return formater.format(date);
  259. } catch (ParseException e) {
  260. e.printStackTrace();
  261. return "";
  262. }
  263. }
  264. public static SimpleDateFormat friendly_format1 = new SimpleDateFormat("HH:mm");
  265. public static SimpleDateFormat friendly_format2 = new SimpleDateFormat("MM-dd HH:mm");
  266. /**
  267. * 获取友好的时间显示
  268. *
  269. * @param time 秒级别的时间戳
  270. * @return
  271. */
  272. public static String getFriendlyTimeDesc(Context context, int time) {
  273. String desc = "";
  274. Date timeDate = new Date(time * 1000L);
  275. Date nowDate = new Date();
  276. long delaySeconds = nowDate.getTime() / 1000 - time;// 相差的秒数
  277. if (delaySeconds < 10) {// 小于10秒,显示刚刚
  278. desc = context.getString(R.string.friendly_time_just_now);// 显示刚刚
  279. } else if (delaySeconds <= 60) {// 小于1分钟,显示如“25秒前”
  280. desc = delaySeconds + context.getString(R.string.friendly_time_before_seconds);
  281. } else if (delaySeconds < 60 * 30) {// 小于30分钟,显示如“25分钟前”
  282. desc = (delaySeconds / 60) + context.getString(R.string.friendly_time_before_minute);
  283. } else if (delaySeconds < 60 * 60 * 24) {// 小于1天之内
  284. if (nowDate.getDay() - timeDate.getDay() == 0) {// 同一天
  285. desc = friendly_format1.format(timeDate);
  286. } else {// 前一天
  287. desc = context.getString(R.string.friendly_time_yesterday) + " " + friendly_format1.format(timeDate);
  288. }
  289. } else if (delaySeconds < 60 * 60 * 24 * 2) {// 小于2天之内
  290. if (nowDate.getDay() - timeDate.getDay() == 1 || nowDate.getDay() - timeDate.getDay() == -6) {// 昨天
  291. desc = context.getString(R.string.friendly_time_yesterday) + " " + friendly_format1.format(timeDate);
  292. } else {// 前天
  293. desc = context.getString(R.string.friendly_time_before_yesterday) + " " + friendly_format1.format(timeDate);
  294. }
  295. } else if (delaySeconds < 60 * 60 * 24 * 3) {// 小于三天
  296. if (nowDate.getDay() - timeDate.getDay() == 2 || nowDate.getDay() - timeDate.getDay() == -5) {// 前天
  297. desc = context.getString(R.string.friendly_time_before_yesterday) + " " + friendly_format1.format(timeDate);
  298. }
  299. // else 超过前天
  300. }
  301. if (TextUtils.isEmpty(desc)) {
  302. desc = friendly_format2.format(timeDate);
  303. }
  304. return desc;
  305. }
  306. public static String sk_time_friendly_format2(long time) {
  307. return friendly_format2.format(new Date(time * 1000));
  308. }
  309. public static String sk_time_s_long_2_str(long time) {
  310. return s_long_2_str(time * 1000);
  311. }
  312. public static String sk_time_ss_long_2_str(long time) {
  313. return ss_long_2_str(time * 1000);
  314. }
  315. public static long sk_time_s_str_2_long(String dateString) {
  316. return s_str_2_long(dateString) / 1000;
  317. }
  318. public static int sk_time_current_time() {
  319. return (int) (System.currentTimeMillis() / 1000);
  320. }
  321. private static SimpleDateFormat hm_formater = new SimpleDateFormat("HH:mm");
  322. public static String sk_time_long_to_hm_str(long time) {
  323. try {
  324. return hm_formater.format(new Date(time * 1000));
  325. } catch (Exception e) {
  326. e.printStackTrace();
  327. }
  328. return "";
  329. }
  330. public static String sk_time_long_to_chat_time_str(long time) {
  331. String date1 = sk_time_s_long_2_str(time);
  332. String date2 = sk_time_s_long_2_str(System.currentTimeMillis() / 1000);
  333. if (date1.compareToIgnoreCase(date2) == 0) {// 是同一天
  334. return sk_time_long_to_hm_str(time);
  335. } else {
  336. return long_to_yMdHm_str(time * 1000);
  337. }
  338. }
  339. public static final SimpleDateFormat sk_format_1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  340. // 日期加小时的字符串
  341. public static String long_to_yMdHm_str(long time) {
  342. return sk_format_1.format(new Date(time));
  343. }
  344. public static long sk_time_yMdHm_str_to_long(String time) {
  345. try {
  346. return sk_format_1.parse(time).getTime() / 1000;
  347. } catch (ParseException e) {
  348. e.printStackTrace();
  349. }
  350. return 0;
  351. }
  352. public static int yMdHm_getYear(String dateString) {
  353. try {
  354. Date d = sk_format_1.parse(dateString);
  355. return d.getYear() + 1900;// 年份是基于格林威治时间,所以加上1900
  356. } catch (ParseException e) {
  357. e.printStackTrace();
  358. }
  359. return 0;
  360. }
  361. public static int yMdHm_getMonth(String dateString) {
  362. try {
  363. Date d = sk_format_1.parse(dateString);
  364. return d.getMonth();// 月份从0-11
  365. } catch (ParseException e) {
  366. e.printStackTrace();
  367. }
  368. return 0;
  369. }
  370. public static int yMdHm_getDayOfMonth(String dateString) {
  371. try {
  372. Date d = sk_format_1.parse(dateString);
  373. return d.getDate();
  374. } catch (ParseException e) {
  375. e.printStackTrace();
  376. }
  377. return 0;
  378. }
  379. public static int yMdHm_getHours(String timeString) {
  380. try {
  381. Date date = sk_format_1.parse(timeString);
  382. return date.getHours();
  383. } catch (ParseException e) {
  384. e.printStackTrace();
  385. }
  386. return 0;
  387. }
  388. public static int yMdHm_getMinutes(String timeString) {
  389. try {
  390. Date date = sk_format_1.parse(timeString);
  391. return date.getMinutes();
  392. } catch (ParseException e) {
  393. e.printStackTrace();
  394. }
  395. return 0;
  396. }
  397. /**
  398. * @param textView
  399. * @param time 时间戳/1000
  400. * @return
  401. */
  402. public static long getSpecialBeginTime(TextView textView, long time) {
  403. long currentTime = System.currentTimeMillis() / 1000;
  404. if (time > currentTime) {
  405. time = currentTime;
  406. }
  407. textView.setText(sk_time_s_long_2_str(time));
  408. return time;
  409. }
  410. /**
  411. * @param textView
  412. * @param time 时间戳/1000
  413. * @return
  414. */
  415. public static long getSpecialEndTime(TextView textView, long time) {
  416. long currentTime = System.currentTimeMillis() / 1000;
  417. if (time == 0 || time > currentTime - 24 * 60 * 60) {
  418. textView.setText(R.string.to_this_day);
  419. return 0;
  420. }
  421. textView.setText(sk_time_s_long_2_str(time));
  422. return time;
  423. }
  424. public static int sk_time_age(long birthday) {
  425. int age = (new Date().getYear()) - (new Date(birthday * 1000).getYear());
  426. if (age < 0 || age > 100) {
  427. return 25;
  428. }
  429. return age;
  430. }
  431. }