TimeUtils.java 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  1. package com.cooleshow.base.utils;
  2. import android.annotation.SuppressLint;
  3. import android.os.Build;
  4. import android.provider.Settings;
  5. import com.cooleshow.base.constanst.TimeConstants;
  6. import java.text.DateFormat;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Calendar;
  10. import java.util.Date;
  11. import java.util.GregorianCalendar;
  12. import java.util.HashMap;
  13. import java.util.Locale;
  14. import java.util.Map;
  15. import androidx.annotation.NonNull;
  16. /**
  17. * <pre>
  18. * author: Blankj
  19. * blog : http://blankj.com
  20. * time : 2016/08/02
  21. * desc : utils about time
  22. * </pre>
  23. */
  24. public final class TimeUtils {
  25. private static final ThreadLocal<Map<String, SimpleDateFormat>> SDF_THREAD_LOCAL
  26. = new ThreadLocal<Map<String, SimpleDateFormat>>() {
  27. @Override
  28. protected Map<String, SimpleDateFormat> initialValue() {
  29. return new HashMap<>();
  30. }
  31. };
  32. private static SimpleDateFormat getDefaultFormat() {
  33. return getSafeDateFormat("yyyy-MM-dd HH:mm:ss");
  34. }
  35. public static SimpleDateFormat getSafeDateFormatForDay() {
  36. return getSafeDateFormat("yyyy-MM-dd");
  37. }
  38. public static SimpleDateFormat getSafeDateFormatForMinutes() {
  39. return getSafeDateFormat("yyyy-MM-dd HH:mm");
  40. }
  41. /**
  42. * Checks whether the device is using Network Provided Time or not.
  43. * Useful in situations where you want to verify that the device has a correct time set, to avoid fraud, or if you want to prevent the user from messing with the time and abusing your "one-time" and "expiring" features.
  44. *
  45. * @return {@code true}: yes<br>{@code false}: no
  46. */
  47. public static boolean isUsingNetworkProvidedTime() {
  48. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
  49. return Settings.Global.getInt(Utils.getApp().getContentResolver(), Settings.Global.AUTO_TIME, 0) == 1;
  50. } else {
  51. return android.provider.Settings.System.getInt(Utils.getApp().getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0) == 1;
  52. }
  53. }
  54. @SuppressLint("SimpleDateFormat")
  55. public static SimpleDateFormat getSafeDateFormat(String pattern) {
  56. Map<String, SimpleDateFormat> sdfMap = SDF_THREAD_LOCAL.get();
  57. //noinspection ConstantConditions
  58. SimpleDateFormat simpleDateFormat = sdfMap.get(pattern);
  59. if (simpleDateFormat == null) {
  60. simpleDateFormat = new SimpleDateFormat(pattern);
  61. sdfMap.put(pattern, simpleDateFormat);
  62. }
  63. return simpleDateFormat;
  64. }
  65. private TimeUtils() {
  66. throw new UnsupportedOperationException("u can't instantiate me...");
  67. }
  68. /**
  69. * Milliseconds to the formatted time string.
  70. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  71. *
  72. * @param millis The milliseconds.
  73. * @return the formatted time string
  74. */
  75. public static String millis2String(final long millis) {
  76. return millis2String(millis, getDefaultFormat());
  77. }
  78. /**
  79. * Milliseconds to the formatted time string.
  80. *
  81. * @param millis The milliseconds.
  82. * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
  83. * @return the formatted time string
  84. */
  85. public static String millis2String(long millis, @NonNull final String pattern) {
  86. return millis2String(millis, getSafeDateFormat(pattern));
  87. }
  88. /**
  89. * Milliseconds to the formatted time string.
  90. *
  91. * @param millis The milliseconds.
  92. * @param format The format.
  93. * @return the formatted time string
  94. */
  95. public static String millis2String(final long millis, @NonNull final DateFormat format) {
  96. return format.format(new Date(millis));
  97. }
  98. /**
  99. * Formatted time string to the milliseconds.
  100. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  101. *
  102. * @param time The formatted time string.
  103. * @return the milliseconds
  104. */
  105. public static long string2Millis(final String time) {
  106. return string2Millis(time, getDefaultFormat());
  107. }
  108. /**
  109. * Formatted time string to the milliseconds.
  110. *
  111. * @param time The formatted time string.
  112. * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
  113. * @return the milliseconds
  114. */
  115. public static long string2Millis(final String time, @NonNull final String pattern) {
  116. return string2Millis(time, getSafeDateFormat(pattern));
  117. }
  118. /**
  119. * Formatted time string to the milliseconds.
  120. *
  121. * @param time The formatted time string.
  122. * @param format The format.
  123. * @return the milliseconds
  124. */
  125. public static long string2Millis(final String time, @NonNull final DateFormat format) {
  126. try {
  127. return format.parse(time).getTime();
  128. } catch (ParseException e) {
  129. e.printStackTrace();
  130. }
  131. return -1;
  132. }
  133. /**
  134. * Formatted time string to the date.
  135. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  136. *
  137. * @param time The formatted time string.
  138. * @return the date
  139. */
  140. public static Date string2Date(final String time) {
  141. return string2Date(time, getDefaultFormat());
  142. }
  143. /**
  144. * Formatted time string to the date.
  145. *
  146. * @param time The formatted time string.
  147. * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
  148. * @return the date
  149. */
  150. public static Date string2Date(final String time, @NonNull final String pattern) {
  151. return string2Date(time, getSafeDateFormat(pattern));
  152. }
  153. /**
  154. * Formatted time string to the date.
  155. *
  156. * @param time The formatted time string.
  157. * @param format The format.
  158. * @return the date
  159. */
  160. public static Date string2Date(final String time, @NonNull final DateFormat format) {
  161. try {
  162. return format.parse(time);
  163. } catch (Exception e) {
  164. e.printStackTrace();
  165. }
  166. return null;
  167. }
  168. /**
  169. * Date to the formatted time string.
  170. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  171. *
  172. * @param date The date.
  173. * @return the formatted time string
  174. */
  175. public static String date2String(final Date date) {
  176. return date2String(date, getDefaultFormat());
  177. }
  178. /**
  179. * Date to the formatted time string.
  180. *
  181. * @param date The date.
  182. * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
  183. * @return the formatted time string
  184. */
  185. public static String date2String(final Date date, @NonNull final String pattern) {
  186. return getSafeDateFormat(pattern).format(date);
  187. }
  188. /**
  189. * Date to the formatted time string.
  190. *
  191. * @param date The date.
  192. * @param format The format.
  193. * @return the formatted time string
  194. */
  195. public static String date2String(final Date date, @NonNull final DateFormat format) {
  196. try {
  197. return format.format(date);
  198. } catch (Exception e) {
  199. e.printStackTrace();
  200. }
  201. return "";
  202. }
  203. /**
  204. * Date to the milliseconds.
  205. *
  206. * @param date The date.
  207. * @return the milliseconds
  208. */
  209. public static long date2Millis(final Date date) {
  210. return date.getTime();
  211. }
  212. /**
  213. * Milliseconds to the date.
  214. *
  215. * @param millis The milliseconds.
  216. * @return the date
  217. */
  218. public static Date millis2Date(final long millis) {
  219. return new Date(millis);
  220. }
  221. /**
  222. * Return the time span, in unit.
  223. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  224. *
  225. * @param time1 The first formatted time string.
  226. * @param time2 The second formatted time string.
  227. * @param unit The unit of time span.
  228. * <ul>
  229. * <li>{@link TimeConstants#MSEC}</li>
  230. * <li>{@link TimeConstants#SEC }</li>
  231. * <li>{@link TimeConstants#MIN }</li>
  232. * <li>{@link TimeConstants#HOUR}</li>
  233. * <li>{@link TimeConstants#DAY }</li>
  234. * </ul>
  235. * @return the time span, in unit
  236. */
  237. public static long getTimeSpan(final String time1,
  238. final String time2,
  239. @TimeConstants.Unit final int unit) {
  240. return getTimeSpan(time1, time2, getDefaultFormat(), unit);
  241. }
  242. /**
  243. * Return the time span, in unit.
  244. *
  245. * @param time1 The first formatted time string.
  246. * @param time2 The second formatted time string.
  247. * @param format The format.
  248. * @param unit The unit of time span.
  249. * <ul>
  250. * <li>{@link TimeConstants#MSEC}</li>
  251. * <li>{@link TimeConstants#SEC }</li>
  252. * <li>{@link TimeConstants#MIN }</li>
  253. * <li>{@link TimeConstants#HOUR}</li>
  254. * <li>{@link TimeConstants#DAY }</li>
  255. * </ul>
  256. * @return the time span, in unit
  257. */
  258. public static long getTimeSpan(final String time1,
  259. final String time2,
  260. @NonNull final DateFormat format,
  261. @TimeConstants.Unit final int unit) {
  262. return millis2TimeSpan(string2Millis(time1, format) - string2Millis(time2, format), unit);
  263. }
  264. /**
  265. * Return the time span, in unit.
  266. *
  267. * @param date1 The first date.
  268. * @param date2 The second date.
  269. * @param unit The unit of time span.
  270. * <ul>
  271. * <li>{@link TimeConstants#MSEC}</li>
  272. * <li>{@link TimeConstants#SEC }</li>
  273. * <li>{@link TimeConstants#MIN }</li>
  274. * <li>{@link TimeConstants#HOUR}</li>
  275. * <li>{@link TimeConstants#DAY }</li>
  276. * </ul>
  277. * @return the time span, in unit
  278. */
  279. public static long getTimeSpan(final Date date1,
  280. final Date date2,
  281. @TimeConstants.Unit final int unit) {
  282. return millis2TimeSpan(date2Millis(date1) - date2Millis(date2), unit);
  283. }
  284. /**
  285. * Return the time span, in unit.
  286. *
  287. * @param millis1 The first milliseconds.
  288. * @param millis2 The second milliseconds.
  289. * @param unit The unit of time span.
  290. * <ul>
  291. * <li>{@link TimeConstants#MSEC}</li>
  292. * <li>{@link TimeConstants#SEC }</li>
  293. * <li>{@link TimeConstants#MIN }</li>
  294. * <li>{@link TimeConstants#HOUR}</li>
  295. * <li>{@link TimeConstants#DAY }</li>
  296. * </ul>
  297. * @return the time span, in unit
  298. */
  299. public static long getTimeSpan(final long millis1,
  300. final long millis2,
  301. @TimeConstants.Unit final int unit) {
  302. return millis2TimeSpan(millis1 - millis2, unit);
  303. }
  304. /**
  305. * Return the fit time span.
  306. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  307. *
  308. * @param time1 The first formatted time string.
  309. * @param time2 The second formatted time string.
  310. * @param precision The precision of time span.
  311. * <ul>
  312. * <li>precision = 0, return null</li>
  313. * <li>precision = 1, return 天</li>
  314. * <li>precision = 2, return 天, 小时</li>
  315. * <li>precision = 3, return 天, 小时, 分钟</li>
  316. * <li>precision = 4, return 天, 小时, 分钟, 秒</li>
  317. * <li>precision &gt;= 5,return 天, 小时, 分钟, 秒, 毫秒</li>
  318. * </ul>
  319. * @return the fit time span
  320. */
  321. public static String getFitTimeSpan(final String time1,
  322. final String time2,
  323. final int precision) {
  324. long delta = string2Millis(time1, getDefaultFormat()) - string2Millis(time2, getDefaultFormat());
  325. return millis2FitTimeSpan(delta, precision);
  326. }
  327. /**
  328. * Return the fit time span.
  329. *
  330. * @param time1 The first formatted time string.
  331. * @param time2 The second formatted time string.
  332. * @param format The format.
  333. * @param precision The precision of time span.
  334. * <ul>
  335. * <li>precision = 0, return null</li>
  336. * <li>precision = 1, return 天</li>
  337. * <li>precision = 2, return 天, 小时</li>
  338. * <li>precision = 3, return 天, 小时, 分钟</li>
  339. * <li>precision = 4, return 天, 小时, 分钟, 秒</li>
  340. * <li>precision &gt;= 5,return 天, 小时, 分钟, 秒, 毫秒</li>
  341. * </ul>
  342. * @return the fit time span
  343. */
  344. public static String getFitTimeSpan(final String time1,
  345. final String time2,
  346. @NonNull final DateFormat format,
  347. final int precision) {
  348. long delta = string2Millis(time1, format) - string2Millis(time2, format);
  349. return millis2FitTimeSpan(delta, precision);
  350. }
  351. /**
  352. * Return the fit time span.
  353. *
  354. * @param date1 The first date.
  355. * @param date2 The second date.
  356. * @param precision The precision of time span.
  357. * <ul>
  358. * <li>precision = 0, return null</li>
  359. * <li>precision = 1, return 天</li>
  360. * <li>precision = 2, return 天, 小时</li>
  361. * <li>precision = 3, return 天, 小时, 分钟</li>
  362. * <li>precision = 4, return 天, 小时, 分钟, 秒</li>
  363. * <li>precision &gt;= 5,return 天, 小时, 分钟, 秒, 毫秒</li>
  364. * </ul>
  365. * @return the fit time span
  366. */
  367. public static String getFitTimeSpan(final Date date1, final Date date2, final int precision) {
  368. return millis2FitTimeSpan(date2Millis(date1) - date2Millis(date2), precision);
  369. }
  370. /**
  371. * Return the fit time span.
  372. *
  373. * @param millis1 The first milliseconds.
  374. * @param millis2 The second milliseconds.
  375. * @param precision The precision of time span.
  376. * <ul>
  377. * <li>precision = 0, return null</li>
  378. * <li>precision = 1, return 天</li>
  379. * <li>precision = 2, return 天, 小时</li>
  380. * <li>precision = 3, return 天, 小时, 分钟</li>
  381. * <li>precision = 4, return 天, 小时, 分钟, 秒</li>
  382. * <li>precision &gt;= 5,return 天, 小时, 分钟, 秒, 毫秒</li>
  383. * </ul>
  384. * @return the fit time span
  385. */
  386. public static String getFitTimeSpan(final long millis1,
  387. final long millis2,
  388. final int precision) {
  389. return millis2FitTimeSpan(millis1 - millis2, precision);
  390. }
  391. /**
  392. * Return the current time in milliseconds.
  393. *
  394. * @return the current time in milliseconds
  395. */
  396. public static long getNowMills() {
  397. return System.currentTimeMillis();
  398. }
  399. /**
  400. * Return the current formatted time string.
  401. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  402. *
  403. * @return the current formatted time string
  404. */
  405. public static String getNowString() {
  406. return millis2String(System.currentTimeMillis(), getDefaultFormat());
  407. }
  408. /**
  409. * Return the current formatted time string.
  410. *
  411. * @param format The format.
  412. * @return the current formatted time string
  413. */
  414. public static String getNowString(@NonNull final DateFormat format) {
  415. return millis2String(System.currentTimeMillis(), format);
  416. }
  417. /**
  418. * Return the current date.
  419. *
  420. * @return the current date
  421. */
  422. public static Date getNowDate() {
  423. return new Date();
  424. }
  425. /**
  426. * Return the time span by now, in unit.
  427. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  428. *
  429. * @param time The formatted time string.
  430. * @param unit The unit of time span.
  431. * <ul>
  432. * <li>{@link TimeConstants#MSEC}</li>
  433. * <li>{@link TimeConstants#SEC }</li>
  434. * <li>{@link TimeConstants#MIN }</li>
  435. * <li>{@link TimeConstants#HOUR}</li>
  436. * <li>{@link TimeConstants#DAY }</li>
  437. * </ul>
  438. * @return the time span by now, in unit
  439. */
  440. public static long getTimeSpanByNow(final String time, @TimeConstants.Unit final int unit) {
  441. return getTimeSpan(time, getNowString(), getDefaultFormat(), unit);
  442. }
  443. /**
  444. * Return the time span by now, in unit.
  445. *
  446. * @param time The formatted time string.
  447. * @param format The format.
  448. * @param unit The unit of time span.
  449. * <ul>
  450. * <li>{@link TimeConstants#MSEC}</li>
  451. * <li>{@link TimeConstants#SEC }</li>
  452. * <li>{@link TimeConstants#MIN }</li>
  453. * <li>{@link TimeConstants#HOUR}</li>
  454. * <li>{@link TimeConstants#DAY }</li>
  455. * </ul>
  456. * @return the time span by now, in unit
  457. */
  458. public static long getTimeSpanByNow(final String time,
  459. @NonNull final DateFormat format,
  460. @TimeConstants.Unit final int unit) {
  461. return getTimeSpan(time, getNowString(format), format, unit);
  462. }
  463. /**
  464. * Return the time span by now, in unit.
  465. *
  466. * @param date The date.
  467. * @param unit The unit of time span.
  468. * <ul>
  469. * <li>{@link TimeConstants#MSEC}</li>
  470. * <li>{@link TimeConstants#SEC }</li>
  471. * <li>{@link TimeConstants#MIN }</li>
  472. * <li>{@link TimeConstants#HOUR}</li>
  473. * <li>{@link TimeConstants#DAY }</li>
  474. * </ul>
  475. * @return the time span by now, in unit
  476. */
  477. public static long getTimeSpanByNow(final Date date, @TimeConstants.Unit final int unit) {
  478. return getTimeSpan(date, new Date(), unit);
  479. }
  480. /**
  481. * Return the time span by now, in unit.
  482. *
  483. * @param millis The milliseconds.
  484. * @param unit The unit of time span.
  485. * <ul>
  486. * <li>{@link TimeConstants#MSEC}</li>
  487. * <li>{@link TimeConstants#SEC }</li>
  488. * <li>{@link TimeConstants#MIN }</li>
  489. * <li>{@link TimeConstants#HOUR}</li>
  490. * <li>{@link TimeConstants#DAY }</li>
  491. * </ul>
  492. * @return the time span by now, in unit
  493. */
  494. public static long getTimeSpanByNow(final long millis, @TimeConstants.Unit final int unit) {
  495. return getTimeSpan(millis, System.currentTimeMillis(), unit);
  496. }
  497. /**
  498. * Return the fit time span by now.
  499. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  500. *
  501. * @param time The formatted time string.
  502. * @param precision The precision of time span.
  503. * <ul>
  504. * <li>precision = 0,返回 null</li>
  505. * <li>precision = 1,返回天</li>
  506. * <li>precision = 2,返回天和小时</li>
  507. * <li>precision = 3,返回天、小时和分钟</li>
  508. * <li>precision = 4,返回天、小时、分钟和秒</li>
  509. * <li>precision &gt;= 5,返回天、小时、分钟、秒和毫秒</li>
  510. * </ul>
  511. * @return the fit time span by now
  512. */
  513. public static String getFitTimeSpanByNow(final String time, final int precision) {
  514. return getFitTimeSpan(time, getNowString(), getDefaultFormat(), precision);
  515. }
  516. /**
  517. * Return the fit time span by now.
  518. *
  519. * @param time The formatted time string.
  520. * @param format The format.
  521. * @param precision The precision of time span.
  522. * <ul>
  523. * <li>precision = 0,返回 null</li>
  524. * <li>precision = 1,返回天</li>
  525. * <li>precision = 2,返回天和小时</li>
  526. * <li>precision = 3,返回天、小时和分钟</li>
  527. * <li>precision = 4,返回天、小时、分钟和秒</li>
  528. * <li>precision &gt;= 5,返回天、小时、分钟、秒和毫秒</li>
  529. * </ul>
  530. * @return the fit time span by now
  531. */
  532. public static String getFitTimeSpanByNow(final String time,
  533. @NonNull final DateFormat format,
  534. final int precision) {
  535. return getFitTimeSpan(time, getNowString(format), format, precision);
  536. }
  537. /**
  538. * Return the fit time span by now.
  539. *
  540. * @param date The date.
  541. * @param precision The precision of time span.
  542. * <ul>
  543. * <li>precision = 0,返回 null</li>
  544. * <li>precision = 1,返回天</li>
  545. * <li>precision = 2,返回天和小时</li>
  546. * <li>precision = 3,返回天、小时和分钟</li>
  547. * <li>precision = 4,返回天、小时、分钟和秒</li>
  548. * <li>precision &gt;= 5,返回天、小时、分钟、秒和毫秒</li>
  549. * </ul>
  550. * @return the fit time span by now
  551. */
  552. public static String getFitTimeSpanByNow(final Date date, final int precision) {
  553. return getFitTimeSpan(date, getNowDate(), precision);
  554. }
  555. /**
  556. * Return the fit time span by now.
  557. *
  558. * @param millis The milliseconds.
  559. * @param precision The precision of time span.
  560. * <ul>
  561. * <li>precision = 0,返回 null</li>
  562. * <li>precision = 1,返回天</li>
  563. * <li>precision = 2,返回天和小时</li>
  564. * <li>precision = 3,返回天、小时和分钟</li>
  565. * <li>precision = 4,返回天、小时、分钟和秒</li>
  566. * <li>precision &gt;= 5,返回天、小时、分钟、秒和毫秒</li>
  567. * </ul>
  568. * @return the fit time span by now
  569. */
  570. public static String getFitTimeSpanByNow(final long millis, final int precision) {
  571. return getFitTimeSpan(millis, System.currentTimeMillis(), precision);
  572. }
  573. /**
  574. * Return the friendly time span by now.
  575. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  576. *
  577. * @param time The formatted time string.
  578. * @return the friendly time span by now
  579. * <ul>
  580. * <li>如果小于 1 秒钟内,显示刚刚</li>
  581. * <li>如果在 1 分钟内,显示 XXX秒前</li>
  582. * <li>如果在 1 小时内,显示 XXX分钟前</li>
  583. * <li>如果在 1 小时外的今天内,显示今天15:32</li>
  584. * <li>如果是昨天的,显示昨天15:32</li>
  585. * <li>其余显示,2016-10-15</li>
  586. * <li>时间不合法的情况全部日期和时间信息,如星期六 十月 27 14:21:20 CST 2007</li>
  587. * </ul>
  588. */
  589. public static String getFriendlyTimeSpanByNow(final String time) {
  590. return getFriendlyTimeSpanByNow(time, getDefaultFormat());
  591. }
  592. /**
  593. * Return the friendly time span by now.
  594. *
  595. * @param time The formatted time string.
  596. * @param format The format.
  597. * @return the friendly time span by now
  598. * <ul>
  599. * <li>如果小于 1 秒钟内,显示刚刚</li>
  600. * <li>如果在 1 分钟内,显示 XXX秒前</li>
  601. * <li>如果在 1 小时内,显示 XXX分钟前</li>
  602. * <li>如果在 1 小时外的今天内,显示今天15:32</li>
  603. * <li>如果是昨天的,显示昨天15:32</li>
  604. * <li>其余显示,2016-10-15</li>
  605. * <li>时间不合法的情况全部日期和时间信息,如星期六 十月 27 14:21:20 CST 2007</li>
  606. * </ul>
  607. */
  608. public static String getFriendlyTimeSpanByNow(final String time,
  609. @NonNull final DateFormat format) {
  610. return getFriendlyTimeSpanByNow(string2Millis(time, format));
  611. }
  612. /**
  613. * Return the friendly time span by now.
  614. *
  615. * @param date The date.
  616. * @return the friendly time span by now
  617. * <ul>
  618. * <li>如果小于 1 秒钟内,显示刚刚</li>
  619. * <li>如果在 1 分钟内,显示 XXX秒前</li>
  620. * <li>如果在 1 小时内,显示 XXX分钟前</li>
  621. * <li>如果在 1 小时外的今天内,显示今天15:32</li>
  622. * <li>如果是昨天的,显示昨天15:32</li>
  623. * <li>其余显示,2016-10-15</li>
  624. * <li>时间不合法的情况全部日期和时间信息,如星期六 十月 27 14:21:20 CST 2007</li>
  625. * </ul>
  626. */
  627. public static String getFriendlyTimeSpanByNow(final Date date) {
  628. return getFriendlyTimeSpanByNow(date.getTime());
  629. }
  630. /**
  631. * Return the friendly time span by now.
  632. *
  633. * @param millis The milliseconds.
  634. * @return the friendly time span by now
  635. * <ul>
  636. * <li>如果小于 1 秒钟内,显示刚刚</li>
  637. * <li>如果在 1 分钟内,显示 XXX秒前</li>
  638. * <li>如果在 1 小时内,显示 XXX分钟前</li>
  639. * <li>如果在 1 小时外的今天内,显示今天15:32</li>
  640. * <li>如果是昨天的,显示昨天15:32</li>
  641. * <li>其余显示,2016-10-15</li>
  642. * <li>时间不合法的情况全部日期和时间信息,如星期六 十月 27 14:21:20 CST 2007</li>
  643. * </ul>
  644. */
  645. public static String getFriendlyTimeSpanByNow(final long millis) {
  646. long now = System.currentTimeMillis();
  647. long span = now - millis;
  648. if (span < 0)
  649. // U can read http://www.apihome.cn/api/java/Formatter.html to understand it.
  650. return String.format("%tc", millis);
  651. if (span < 1000) {
  652. return "刚刚";
  653. } else if (span < TimeConstants.MIN) {
  654. return String.format(Locale.getDefault(), "%d秒前", span / TimeConstants.SEC);
  655. } else if (span < TimeConstants.HOUR) {
  656. return String.format(Locale.getDefault(), "%d分钟前", span / TimeConstants.MIN);
  657. }
  658. // 获取当天 00:00
  659. long wee = getWeeOfToday();
  660. if (millis >= wee) {
  661. return String.format("今天%tR", millis);
  662. } else if (millis >= wee - TimeConstants.DAY) {
  663. return String.format("昨天%tR", millis);
  664. } else {
  665. return String.format("%tF", millis);
  666. }
  667. }
  668. private static long getWeeOfToday() {
  669. Calendar cal = Calendar.getInstance();
  670. cal.set(Calendar.HOUR_OF_DAY, 0);
  671. cal.set(Calendar.SECOND, 0);
  672. cal.set(Calendar.MINUTE, 0);
  673. cal.set(Calendar.MILLISECOND, 0);
  674. return cal.getTimeInMillis();
  675. }
  676. /**
  677. * Return the milliseconds differ time span.
  678. *
  679. * @param millis The milliseconds.
  680. * @param timeSpan The time span.
  681. * @param unit The unit of time span.
  682. * <ul>
  683. * <li>{@link TimeConstants#MSEC}</li>
  684. * <li>{@link TimeConstants#SEC }</li>
  685. * <li>{@link TimeConstants#MIN }</li>
  686. * <li>{@link TimeConstants#HOUR}</li>
  687. * <li>{@link TimeConstants#DAY }</li>
  688. * </ul>
  689. * @return the milliseconds differ time span
  690. */
  691. public static long getMillis(final long millis,
  692. final long timeSpan,
  693. @TimeConstants.Unit final int unit) {
  694. return millis + timeSpan2Millis(timeSpan, unit);
  695. }
  696. /**
  697. * Return the milliseconds differ time span.
  698. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  699. *
  700. * @param time The formatted time string.
  701. * @param timeSpan The time span.
  702. * @param unit The unit of time span.
  703. * <ul>
  704. * <li>{@link TimeConstants#MSEC}</li>
  705. * <li>{@link TimeConstants#SEC }</li>
  706. * <li>{@link TimeConstants#MIN }</li>
  707. * <li>{@link TimeConstants#HOUR}</li>
  708. * <li>{@link TimeConstants#DAY }</li>
  709. * </ul>
  710. * @return the milliseconds differ time span
  711. */
  712. public static long getMillis(final String time,
  713. final long timeSpan,
  714. @TimeConstants.Unit final int unit) {
  715. return getMillis(time, getDefaultFormat(), timeSpan, unit);
  716. }
  717. /**
  718. * Return the milliseconds differ time span.
  719. *
  720. * @param time The formatted time string.
  721. * @param format The format.
  722. * @param timeSpan The time span.
  723. * @param unit The unit of time span.
  724. * <ul>
  725. * <li>{@link TimeConstants#MSEC}</li>
  726. * <li>{@link TimeConstants#SEC }</li>
  727. * <li>{@link TimeConstants#MIN }</li>
  728. * <li>{@link TimeConstants#HOUR}</li>
  729. * <li>{@link TimeConstants#DAY }</li>
  730. * </ul>
  731. * @return the milliseconds differ time span.
  732. */
  733. public static long getMillis(final String time,
  734. @NonNull final DateFormat format,
  735. final long timeSpan,
  736. @TimeConstants.Unit final int unit) {
  737. return string2Millis(time, format) + timeSpan2Millis(timeSpan, unit);
  738. }
  739. /**
  740. * Return the milliseconds differ time span.
  741. *
  742. * @param date The date.
  743. * @param timeSpan The time span.
  744. * @param unit The unit of time span.
  745. * <ul>
  746. * <li>{@link TimeConstants#MSEC}</li>
  747. * <li>{@link TimeConstants#SEC }</li>
  748. * <li>{@link TimeConstants#MIN }</li>
  749. * <li>{@link TimeConstants#HOUR}</li>
  750. * <li>{@link TimeConstants#DAY }</li>
  751. * </ul>
  752. * @return the milliseconds differ time span.
  753. */
  754. public static long getMillis(final Date date,
  755. final long timeSpan,
  756. @TimeConstants.Unit final int unit) {
  757. return date2Millis(date) + timeSpan2Millis(timeSpan, unit);
  758. }
  759. /**
  760. * Return the formatted time string differ time span.
  761. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  762. *
  763. * @param millis The milliseconds.
  764. * @param timeSpan The time span.
  765. * @param unit The unit of time span.
  766. * <ul>
  767. * <li>{@link TimeConstants#MSEC}</li>
  768. * <li>{@link TimeConstants#SEC }</li>
  769. * <li>{@link TimeConstants#MIN }</li>
  770. * <li>{@link TimeConstants#HOUR}</li>
  771. * <li>{@link TimeConstants#DAY }</li>
  772. * </ul>
  773. * @return the formatted time string differ time span
  774. */
  775. public static String getString(final long millis,
  776. final long timeSpan,
  777. @TimeConstants.Unit final int unit) {
  778. return getString(millis, getDefaultFormat(), timeSpan, unit);
  779. }
  780. /**
  781. * Return the formatted time string differ time span.
  782. *
  783. * @param millis The milliseconds.
  784. * @param format The format.
  785. * @param timeSpan The time span.
  786. * @param unit The unit of time span.
  787. * <ul>
  788. * <li>{@link TimeConstants#MSEC}</li>
  789. * <li>{@link TimeConstants#SEC }</li>
  790. * <li>{@link TimeConstants#MIN }</li>
  791. * <li>{@link TimeConstants#HOUR}</li>
  792. * <li>{@link TimeConstants#DAY }</li>
  793. * </ul>
  794. * @return the formatted time string differ time span
  795. */
  796. public static String getString(final long millis,
  797. @NonNull final DateFormat format,
  798. final long timeSpan,
  799. @TimeConstants.Unit final int unit) {
  800. return millis2String(millis + timeSpan2Millis(timeSpan, unit), format);
  801. }
  802. /**
  803. * Return the formatted time string differ time span.
  804. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  805. *
  806. * @param time The formatted time string.
  807. * @param timeSpan The time span.
  808. * @param unit The unit of time span.
  809. * <ul>
  810. * <li>{@link TimeConstants#MSEC}</li>
  811. * <li>{@link TimeConstants#SEC }</li>
  812. * <li>{@link TimeConstants#MIN }</li>
  813. * <li>{@link TimeConstants#HOUR}</li>
  814. * <li>{@link TimeConstants#DAY }</li>
  815. * </ul>
  816. * @return the formatted time string differ time span
  817. */
  818. public static String getString(final String time,
  819. final long timeSpan,
  820. @TimeConstants.Unit final int unit) {
  821. return getString(time, getDefaultFormat(), timeSpan, unit);
  822. }
  823. /**
  824. * Return the formatted time string differ time span.
  825. *
  826. * @param time The formatted time string.
  827. * @param format The format.
  828. * @param timeSpan The time span.
  829. * @param unit The unit of time span.
  830. * <ul>
  831. * <li>{@link TimeConstants#MSEC}</li>
  832. * <li>{@link TimeConstants#SEC }</li>
  833. * <li>{@link TimeConstants#MIN }</li>
  834. * <li>{@link TimeConstants#HOUR}</li>
  835. * <li>{@link TimeConstants#DAY }</li>
  836. * </ul>
  837. * @return the formatted time string differ time span
  838. */
  839. public static String getString(final String time,
  840. @NonNull final DateFormat format,
  841. final long timeSpan,
  842. @TimeConstants.Unit final int unit) {
  843. return millis2String(string2Millis(time, format) + timeSpan2Millis(timeSpan, unit), format);
  844. }
  845. /**
  846. * Return the formatted time string differ time span.
  847. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  848. *
  849. * @param date The date.
  850. * @param timeSpan The time span.
  851. * @param unit The unit of time span.
  852. * <ul>
  853. * <li>{@link TimeConstants#MSEC}</li>
  854. * <li>{@link TimeConstants#SEC }</li>
  855. * <li>{@link TimeConstants#MIN }</li>
  856. * <li>{@link TimeConstants#HOUR}</li>
  857. * <li>{@link TimeConstants#DAY }</li>
  858. * </ul>
  859. * @return the formatted time string differ time span
  860. */
  861. public static String getString(final Date date,
  862. final long timeSpan,
  863. @TimeConstants.Unit final int unit) {
  864. return getString(date, getDefaultFormat(), timeSpan, unit);
  865. }
  866. /**
  867. * Return the formatted time string differ time span.
  868. *
  869. * @param date The date.
  870. * @param format The format.
  871. * @param timeSpan The time span.
  872. * @param unit The unit of time span.
  873. * <ul>
  874. * <li>{@link TimeConstants#MSEC}</li>
  875. * <li>{@link TimeConstants#SEC }</li>
  876. * <li>{@link TimeConstants#MIN }</li>
  877. * <li>{@link TimeConstants#HOUR}</li>
  878. * <li>{@link TimeConstants#DAY }</li>
  879. * </ul>
  880. * @return the formatted time string differ time span
  881. */
  882. public static String getString(final Date date,
  883. @NonNull final DateFormat format,
  884. final long timeSpan,
  885. @TimeConstants.Unit final int unit) {
  886. return millis2String(date2Millis(date) + timeSpan2Millis(timeSpan, unit), format);
  887. }
  888. /**
  889. * Return the date differ time span.
  890. *
  891. * @param millis The milliseconds.
  892. * @param timeSpan The time span.
  893. * @param unit The unit of time span.
  894. * <ul>
  895. * <li>{@link TimeConstants#MSEC}</li>
  896. * <li>{@link TimeConstants#SEC }</li>
  897. * <li>{@link TimeConstants#MIN }</li>
  898. * <li>{@link TimeConstants#HOUR}</li>
  899. * <li>{@link TimeConstants#DAY }</li>
  900. * </ul>
  901. * @return the date differ time span
  902. */
  903. public static Date getDate(final long millis,
  904. final long timeSpan,
  905. @TimeConstants.Unit final int unit) {
  906. return millis2Date(millis + timeSpan2Millis(timeSpan, unit));
  907. }
  908. /**
  909. * Return the date differ time span.
  910. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  911. *
  912. * @param time The formatted time string.
  913. * @param timeSpan The time span.
  914. * @param unit The unit of time span.
  915. * <ul>
  916. * <li>{@link TimeConstants#MSEC}</li>
  917. * <li>{@link TimeConstants#SEC }</li>
  918. * <li>{@link TimeConstants#MIN }</li>
  919. * <li>{@link TimeConstants#HOUR}</li>
  920. * <li>{@link TimeConstants#DAY }</li>
  921. * </ul>
  922. * @return the date differ time span
  923. */
  924. public static Date getDate(final String time,
  925. final long timeSpan,
  926. @TimeConstants.Unit final int unit) {
  927. return getDate(time, getDefaultFormat(), timeSpan, unit);
  928. }
  929. public static Date getDate(String time) {
  930. try {
  931. Date parse = getDefaultFormat().parse(time);
  932. return parse;
  933. } catch (Exception e) {
  934. e.printStackTrace();
  935. }
  936. return getNowDate();
  937. }
  938. /**
  939. * Return the date differ time span.
  940. *
  941. * @param time The formatted time string.
  942. * @param format The format.
  943. * @param timeSpan The time span.
  944. * @param unit The unit of time span.
  945. * <ul>
  946. * <li>{@link TimeConstants#MSEC}</li>
  947. * <li>{@link TimeConstants#SEC }</li>
  948. * <li>{@link TimeConstants#MIN }</li>
  949. * <li>{@link TimeConstants#HOUR}</li>
  950. * <li>{@link TimeConstants#DAY }</li>
  951. * </ul>
  952. * @return the date differ time span
  953. */
  954. public static Date getDate(final String time,
  955. @NonNull final DateFormat format,
  956. final long timeSpan,
  957. @TimeConstants.Unit final int unit) {
  958. return millis2Date(string2Millis(time, format) + timeSpan2Millis(timeSpan, unit));
  959. }
  960. /**
  961. * Return the date differ time span.
  962. *
  963. * @param date The date.
  964. * @param timeSpan The time span.
  965. * @param unit The unit of time span.
  966. * <ul>
  967. * <li>{@link TimeConstants#MSEC}</li>
  968. * <li>{@link TimeConstants#SEC }</li>
  969. * <li>{@link TimeConstants#MIN }</li>
  970. * <li>{@link TimeConstants#HOUR}</li>
  971. * <li>{@link TimeConstants#DAY }</li>
  972. * </ul>
  973. * @return the date differ time span
  974. */
  975. public static Date getDate(final Date date,
  976. final long timeSpan,
  977. @TimeConstants.Unit final int unit) {
  978. return millis2Date(date2Millis(date) + timeSpan2Millis(timeSpan, unit));
  979. }
  980. /**
  981. * Return the milliseconds differ time span by now.
  982. *
  983. * @param timeSpan The time span.
  984. * @param unit The unit of time span.
  985. * <ul>
  986. * <li>{@link TimeConstants#MSEC}</li>
  987. * <li>{@link TimeConstants#SEC }</li>
  988. * <li>{@link TimeConstants#MIN }</li>
  989. * <li>{@link TimeConstants#HOUR}</li>
  990. * <li>{@link TimeConstants#DAY }</li>
  991. * </ul>
  992. * @return the milliseconds differ time span by now
  993. */
  994. public static long getMillisByNow(final long timeSpan, @TimeConstants.Unit final int unit) {
  995. return getMillis(getNowMills(), timeSpan, unit);
  996. }
  997. /**
  998. * Return the formatted time string differ time span by now.
  999. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  1000. *
  1001. * @param timeSpan The time span.
  1002. * @param unit The unit of time span.
  1003. * <ul>
  1004. * <li>{@link TimeConstants#MSEC}</li>
  1005. * <li>{@link TimeConstants#SEC }</li>
  1006. * <li>{@link TimeConstants#MIN }</li>
  1007. * <li>{@link TimeConstants#HOUR}</li>
  1008. * <li>{@link TimeConstants#DAY }</li>
  1009. * </ul>
  1010. * @return the formatted time string differ time span by now
  1011. */
  1012. public static String getStringByNow(final long timeSpan, @TimeConstants.Unit final int unit) {
  1013. return getStringByNow(timeSpan, getDefaultFormat(), unit);
  1014. }
  1015. /**
  1016. * Return the formatted time string differ time span by now.
  1017. *
  1018. * @param timeSpan The time span.
  1019. * @param format The format.
  1020. * @param unit The unit of time span.
  1021. * <ul>
  1022. * <li>{@link TimeConstants#MSEC}</li>
  1023. * <li>{@link TimeConstants#SEC }</li>
  1024. * <li>{@link TimeConstants#MIN }</li>
  1025. * <li>{@link TimeConstants#HOUR}</li>
  1026. * <li>{@link TimeConstants#DAY }</li>
  1027. * </ul>
  1028. * @return the formatted time string differ time span by now
  1029. */
  1030. public static String getStringByNow(final long timeSpan,
  1031. @NonNull final DateFormat format,
  1032. @TimeConstants.Unit final int unit) {
  1033. return getString(getNowMills(), format, timeSpan, unit);
  1034. }
  1035. /**
  1036. * Return the date differ time span by now.
  1037. *
  1038. * @param timeSpan The time span.
  1039. * @param unit The unit of time span.
  1040. * <ul>
  1041. * <li>{@link TimeConstants#MSEC}</li>
  1042. * <li>{@link TimeConstants#SEC }</li>
  1043. * <li>{@link TimeConstants#MIN }</li>
  1044. * <li>{@link TimeConstants#HOUR}</li>
  1045. * <li>{@link TimeConstants#DAY }</li>
  1046. * </ul>
  1047. * @return the date differ time span by now
  1048. */
  1049. public static Date getDateByNow(final long timeSpan, @TimeConstants.Unit final int unit) {
  1050. return getDate(getNowMills(), timeSpan, unit);
  1051. }
  1052. /**
  1053. * Return whether it is today.
  1054. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  1055. *
  1056. * @param time The formatted time string.
  1057. * @return {@code true}: yes<br>{@code false}: no
  1058. */
  1059. public static boolean isToday(final String time) {
  1060. return isToday(string2Millis(time, getDefaultFormat()));
  1061. }
  1062. /**
  1063. * Return whether it is today.
  1064. *
  1065. * @param time The formatted time string.
  1066. * @param format The format.
  1067. * @return {@code true}: yes<br>{@code false}: no
  1068. */
  1069. public static boolean isToday(final String time, @NonNull final DateFormat format) {
  1070. return isToday(string2Millis(time, format));
  1071. }
  1072. /**
  1073. * Return whether it is today.
  1074. *
  1075. * @param date The date.
  1076. * @return {@code true}: yes<br>{@code false}: no
  1077. */
  1078. public static boolean isToday(final Date date) {
  1079. return isToday(date.getTime());
  1080. }
  1081. /**
  1082. * Return whether it is today.
  1083. *
  1084. * @param millis The milliseconds.
  1085. * @return {@code true}: yes<br>{@code false}: no
  1086. */
  1087. public static boolean isToday(final long millis) {
  1088. long wee = getWeeOfToday();
  1089. return millis >= wee && millis < wee + TimeConstants.DAY;
  1090. }
  1091. /**
  1092. * Return whether it is leap year.
  1093. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  1094. *
  1095. * @param time The formatted time string.
  1096. * @return {@code true}: yes<br>{@code false}: no
  1097. */
  1098. public static boolean isLeapYear(final String time) {
  1099. return isLeapYear(string2Date(time, getDefaultFormat()));
  1100. }
  1101. /**
  1102. * Return whether it is leap year.
  1103. *
  1104. * @param time The formatted time string.
  1105. * @param format The format.
  1106. * @return {@code true}: yes<br>{@code false}: no
  1107. */
  1108. public static boolean isLeapYear(final String time, @NonNull final DateFormat format) {
  1109. return isLeapYear(string2Date(time, format));
  1110. }
  1111. /**
  1112. * Return whether it is leap year.
  1113. *
  1114. * @param date The date.
  1115. * @return {@code true}: yes<br>{@code false}: no
  1116. */
  1117. public static boolean isLeapYear(final Date date) {
  1118. Calendar cal = Calendar.getInstance();
  1119. cal.setTime(date);
  1120. int year = cal.get(Calendar.YEAR);
  1121. return isLeapYear(year);
  1122. }
  1123. /**
  1124. * Return whether it is leap year.
  1125. *
  1126. * @param millis The milliseconds.
  1127. * @return {@code true}: yes<br>{@code false}: no
  1128. */
  1129. public static boolean isLeapYear(final long millis) {
  1130. return isLeapYear(millis2Date(millis));
  1131. }
  1132. /**
  1133. * Return whether it is leap year.
  1134. *
  1135. * @param year The year.
  1136. * @return {@code true}: yes<br>{@code false}: no
  1137. */
  1138. public static boolean isLeapYear(final int year) {
  1139. return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
  1140. }
  1141. /**
  1142. * Return the day of week in Chinese.
  1143. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  1144. *
  1145. * @param time The formatted time string.
  1146. * @return the day of week in Chinese
  1147. */
  1148. public static String getChineseWeek(final String time) {
  1149. return getChineseWeek(string2Date(time, getDefaultFormat()));
  1150. }
  1151. /**
  1152. * Return the day of week in Chinese.
  1153. *
  1154. * @param time The formatted time string.
  1155. * @param format The format.
  1156. * @return the day of week in Chinese
  1157. */
  1158. public static String getChineseWeek(final String time, @NonNull final DateFormat format) {
  1159. return getChineseWeek(string2Date(time, format));
  1160. }
  1161. /**
  1162. * Return the day of week in Chinese.
  1163. *
  1164. * @param date The date.
  1165. * @return the day of week in Chinese
  1166. */
  1167. public static String getChineseWeek(final Date date) {
  1168. return new SimpleDateFormat("E", Locale.CHINA).format(date);
  1169. }
  1170. /**
  1171. * Return the day of week in Chinese.
  1172. *
  1173. * @param millis The milliseconds.
  1174. * @return the day of week in Chinese
  1175. */
  1176. public static String getChineseWeek(final long millis) {
  1177. return getChineseWeek(new Date(millis));
  1178. }
  1179. /**
  1180. * Return the day of week in US.
  1181. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  1182. *
  1183. * @param time The formatted time string.
  1184. * @return the day of week in US
  1185. */
  1186. public static String getUSWeek(final String time) {
  1187. return getUSWeek(string2Date(time, getDefaultFormat()));
  1188. }
  1189. /**
  1190. * Return the day of week in US.
  1191. *
  1192. * @param time The formatted time string.
  1193. * @param format The format.
  1194. * @return the day of week in US
  1195. */
  1196. public static String getUSWeek(final String time, @NonNull final DateFormat format) {
  1197. return getUSWeek(string2Date(time, format));
  1198. }
  1199. /**
  1200. * Return the day of week in US.
  1201. *
  1202. * @param date The date.
  1203. * @return the day of week in US
  1204. */
  1205. public static String getUSWeek(final Date date) {
  1206. return new SimpleDateFormat("EEEE", Locale.US).format(date);
  1207. }
  1208. /**
  1209. * Return the day of week in US.
  1210. *
  1211. * @param millis The milliseconds.
  1212. * @return the day of week in US
  1213. */
  1214. public static String getUSWeek(final long millis) {
  1215. return getUSWeek(new Date(millis));
  1216. }
  1217. /**
  1218. * Return whether it is am.
  1219. *
  1220. * @return {@code true}: yes<br>{@code false}: no
  1221. */
  1222. public static boolean isAm() {
  1223. Calendar cal = Calendar.getInstance();
  1224. return cal.get(GregorianCalendar.AM_PM) == 0;
  1225. }
  1226. /**
  1227. * Return whether it is am.
  1228. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  1229. *
  1230. * @param time The formatted time string.
  1231. * @return {@code true}: yes<br>{@code false}: no
  1232. */
  1233. public static boolean isAm(final String time) {
  1234. return getValueByCalendarField(time, getDefaultFormat(), GregorianCalendar.AM_PM) == 0;
  1235. }
  1236. /**
  1237. * Return whether it is am.
  1238. *
  1239. * @param time The formatted time string.
  1240. * @param format The format.
  1241. * @return {@code true}: yes<br>{@code false}: no
  1242. */
  1243. public static boolean isAm(final String time,
  1244. @NonNull final DateFormat format) {
  1245. return getValueByCalendarField(time, format, GregorianCalendar.AM_PM) == 0;
  1246. }
  1247. /**
  1248. * Return whether it is am.
  1249. *
  1250. * @param date The date.
  1251. * @return {@code true}: yes<br>{@code false}: no
  1252. */
  1253. public static boolean isAm(final Date date) {
  1254. return getValueByCalendarField(date, GregorianCalendar.AM_PM) == 0;
  1255. }
  1256. /**
  1257. * Return whether it is am.
  1258. *
  1259. * @param millis The milliseconds.
  1260. * @return {@code true}: yes<br>{@code false}: no
  1261. */
  1262. public static boolean isAm(final long millis) {
  1263. return getValueByCalendarField(millis, GregorianCalendar.AM_PM) == 0;
  1264. }
  1265. /**
  1266. * Return whether it is am.
  1267. *
  1268. * @return {@code true}: yes<br>{@code false}: no
  1269. */
  1270. public static boolean isPm() {
  1271. return !isAm();
  1272. }
  1273. /**
  1274. * Return whether it is am.
  1275. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  1276. *
  1277. * @param time The formatted time string.
  1278. * @return {@code true}: yes<br>{@code false}: no
  1279. */
  1280. public static boolean isPm(final String time) {
  1281. return !isAm(time);
  1282. }
  1283. /**
  1284. * Return whether it is am.
  1285. *
  1286. * @param time The formatted time string.
  1287. * @param format The format.
  1288. * @return {@code true}: yes<br>{@code false}: no
  1289. */
  1290. public static boolean isPm(final String time,
  1291. @NonNull final DateFormat format) {
  1292. return !isAm(time, format);
  1293. }
  1294. /**
  1295. * Return whether it is am.
  1296. *
  1297. * @param date The date.
  1298. * @return {@code true}: yes<br>{@code false}: no
  1299. */
  1300. public static boolean isPm(final Date date) {
  1301. return !isAm(date);
  1302. }
  1303. /**
  1304. * Return whether it is am.
  1305. *
  1306. * @param millis The milliseconds.
  1307. * @return {@code true}: yes<br>{@code false}: no
  1308. */
  1309. public static boolean isPm(final long millis) {
  1310. return !isAm(millis);
  1311. }
  1312. /**
  1313. * Returns the value of the given calendar field.
  1314. *
  1315. * @param field The given calendar field.
  1316. * <ul>
  1317. * <li>{@link Calendar#ERA}</li>
  1318. * <li>{@link Calendar#YEAR}</li>
  1319. * <li>{@link Calendar#MONTH}</li>
  1320. * <li>...</li>
  1321. * <li>{@link Calendar#DST_OFFSET}</li>
  1322. * </ul>
  1323. * @return the value of the given calendar field
  1324. */
  1325. public static int getValueByCalendarField(final int field) {
  1326. Calendar cal = Calendar.getInstance();
  1327. return cal.get(field);
  1328. }
  1329. /**
  1330. * Returns the value of the given calendar field.
  1331. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  1332. *
  1333. * @param time The formatted time string.
  1334. * @param field The given calendar field.
  1335. * <ul>
  1336. * <li>{@link Calendar#ERA}</li>
  1337. * <li>{@link Calendar#YEAR}</li>
  1338. * <li>{@link Calendar#MONTH}</li>
  1339. * <li>...</li>
  1340. * <li>{@link Calendar#DST_OFFSET}</li>
  1341. * </ul>
  1342. * @return the value of the given calendar field
  1343. */
  1344. public static int getValueByCalendarField(final String time, final int field) {
  1345. return getValueByCalendarField(string2Date(time, getDefaultFormat()), field);
  1346. }
  1347. /**
  1348. * Returns the value of the given calendar field.
  1349. *
  1350. * @param time The formatted time string.
  1351. * @param format The format.
  1352. * @param field The given calendar field.
  1353. * <ul>
  1354. * <li>{@link Calendar#ERA}</li>
  1355. * <li>{@link Calendar#YEAR}</li>
  1356. * <li>{@link Calendar#MONTH}</li>
  1357. * <li>...</li>
  1358. * <li>{@link Calendar#DST_OFFSET}</li>
  1359. * </ul>
  1360. * @return the value of the given calendar field
  1361. */
  1362. public static int getValueByCalendarField(final String time,
  1363. @NonNull final DateFormat format,
  1364. final int field) {
  1365. return getValueByCalendarField(string2Date(time, format), field);
  1366. }
  1367. /**
  1368. * Returns the value of the given calendar field.
  1369. *
  1370. * @param date The date.
  1371. * @param field The given calendar field.
  1372. * <ul>
  1373. * <li>{@link Calendar#ERA}</li>
  1374. * <li>{@link Calendar#YEAR}</li>
  1375. * <li>{@link Calendar#MONTH}</li>
  1376. * <li>...</li>
  1377. * <li>{@link Calendar#DST_OFFSET}</li>
  1378. * </ul>
  1379. * @return the value of the given calendar field
  1380. */
  1381. public static int getValueByCalendarField(final Date date, final int field) {
  1382. Calendar cal = Calendar.getInstance();
  1383. cal.setTime(date);
  1384. return cal.get(field);
  1385. }
  1386. /**
  1387. * Returns the value of the given calendar field.
  1388. *
  1389. * @param millis The milliseconds.
  1390. * @param field The given calendar field.
  1391. * <ul>
  1392. * <li>{@link Calendar#ERA}</li>
  1393. * <li>{@link Calendar#YEAR}</li>
  1394. * <li>{@link Calendar#MONTH}</li>
  1395. * <li>...</li>
  1396. * <li>{@link Calendar#DST_OFFSET}</li>
  1397. * </ul>
  1398. * @return the value of the given calendar field
  1399. */
  1400. public static int getValueByCalendarField(final long millis, final int field) {
  1401. Calendar cal = Calendar.getInstance();
  1402. cal.setTimeInMillis(millis);
  1403. return cal.get(field);
  1404. }
  1405. private static final String[] CHINESE_ZODIAC =
  1406. {"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};
  1407. /**
  1408. * Return the Chinese zodiac.
  1409. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  1410. *
  1411. * @param time The formatted time string.
  1412. * @return the Chinese zodiac
  1413. */
  1414. public static String getChineseZodiac(final String time) {
  1415. return getChineseZodiac(string2Date(time, getDefaultFormat()));
  1416. }
  1417. /**
  1418. * Return the Chinese zodiac.
  1419. *
  1420. * @param time The formatted time string.
  1421. * @param format The format.
  1422. * @return the Chinese zodiac
  1423. */
  1424. public static String getChineseZodiac(final String time, @NonNull final DateFormat format) {
  1425. return getChineseZodiac(string2Date(time, format));
  1426. }
  1427. /**
  1428. * Return the Chinese zodiac.
  1429. *
  1430. * @param date The date.
  1431. * @return the Chinese zodiac
  1432. */
  1433. public static String getChineseZodiac(final Date date) {
  1434. Calendar cal = Calendar.getInstance();
  1435. cal.setTime(date);
  1436. return CHINESE_ZODIAC[cal.get(Calendar.YEAR) % 12];
  1437. }
  1438. /**
  1439. * Return the Chinese zodiac.
  1440. *
  1441. * @param millis The milliseconds.
  1442. * @return the Chinese zodiac
  1443. */
  1444. public static String getChineseZodiac(final long millis) {
  1445. return getChineseZodiac(millis2Date(millis));
  1446. }
  1447. /**
  1448. * Return the Chinese zodiac.
  1449. *
  1450. * @param year The year.
  1451. * @return the Chinese zodiac
  1452. */
  1453. public static String getChineseZodiac(final int year) {
  1454. return CHINESE_ZODIAC[year % 12];
  1455. }
  1456. private static final int[] ZODIAC_FLAGS = {20, 19, 21, 21, 21, 22, 23, 23, 23, 24, 23, 22};
  1457. private static final String[] ZODIAC = {
  1458. "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座",
  1459. "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"
  1460. };
  1461. /**
  1462. * Return the zodiac.
  1463. * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
  1464. *
  1465. * @param time The formatted time string.
  1466. * @return the zodiac
  1467. */
  1468. public static String getZodiac(final String time) {
  1469. return getZodiac(string2Date(time, getDefaultFormat()));
  1470. }
  1471. /**
  1472. * Return the zodiac.
  1473. *
  1474. * @param time The formatted time string.
  1475. * @param format The format.
  1476. * @return the zodiac
  1477. */
  1478. public static String getZodiac(final String time, @NonNull final DateFormat format) {
  1479. return getZodiac(string2Date(time, format));
  1480. }
  1481. /**
  1482. * Return the zodiac.
  1483. *
  1484. * @param date The date.
  1485. * @return the zodiac
  1486. */
  1487. public static String getZodiac(final Date date) {
  1488. Calendar cal = Calendar.getInstance();
  1489. cal.setTime(date);
  1490. int month = cal.get(Calendar.MONTH) + 1;
  1491. int day = cal.get(Calendar.DAY_OF_MONTH);
  1492. return getZodiac(month, day);
  1493. }
  1494. /**
  1495. * Return the zodiac.
  1496. *
  1497. * @param millis The milliseconds.
  1498. * @return the zodiac
  1499. */
  1500. public static String getZodiac(final long millis) {
  1501. return getZodiac(millis2Date(millis));
  1502. }
  1503. /**
  1504. * Return the zodiac.
  1505. *
  1506. * @param month The month.
  1507. * @param day The day.
  1508. * @return the zodiac
  1509. */
  1510. public static String getZodiac(final int month, final int day) {
  1511. return ZODIAC[day >= ZODIAC_FLAGS[month - 1]
  1512. ? month - 1
  1513. : (month + 10) % 12];
  1514. }
  1515. private static long timeSpan2Millis(final long timeSpan, @TimeConstants.Unit final int unit) {
  1516. return timeSpan * unit;
  1517. }
  1518. private static long millis2TimeSpan(final long millis, @TimeConstants.Unit final int unit) {
  1519. return millis / unit;
  1520. }
  1521. static String millis2FitTimeSpan(long millis, int precision) {
  1522. if (precision <= 0) return null;
  1523. precision = Math.min(precision, 5);
  1524. String[] units = {"天", "小时", "分钟", "秒", "毫秒"};
  1525. if (millis == 0) return 0 + units[precision - 1];
  1526. StringBuilder sb = new StringBuilder();
  1527. if (millis < 0) {
  1528. sb.append("-");
  1529. millis = -millis;
  1530. }
  1531. int[] unitLen = {86400000, 3600000, 60000, 1000, 1};
  1532. for (int i = 0; i < precision; i++) {
  1533. if (millis >= unitLen[i]) {
  1534. long mode = millis / unitLen[i];
  1535. millis -= mode * unitLen[i];
  1536. sb.append(mode).append(units[i]);
  1537. }
  1538. }
  1539. return sb.toString();
  1540. }
  1541. public static Date stringToDate(String strTime)
  1542. throws ParseException {
  1543. SimpleDateFormat formatter = getSafeDateFormat("yyyy-MM-dd");
  1544. Date date = null;
  1545. date = formatter.parse(strTime);
  1546. return date;
  1547. }
  1548. public static long[] getHmsFromSecond(long timeSecond) {
  1549. long[] result = new long[3];
  1550. try{
  1551. result[0] = timeSecond / 3600;
  1552. timeSecond = timeSecond % 3600;
  1553. long minute = timeSecond / 60;//得到分
  1554. result[1] = minute;
  1555. result[2] = timeSecond % 60;
  1556. }catch (Exception e){
  1557. e.printStackTrace();
  1558. }
  1559. return result;
  1560. }
  1561. public static String secondToTime(int second) {
  1562. // int hour = second / 3600; // 得到分钟数
  1563. // second = second % 3600;//剩余的秒数
  1564. int minute = second / 60;//得到分
  1565. second = second % 60;//剩余的秒
  1566. return String.format(Locale.getDefault(), "%02d:%02d", minute, second);
  1567. }
  1568. public static String msToTime(int ms) {
  1569. return secondToTime(ms / 1000);
  1570. }
  1571. public static String msToTime(long ms) {
  1572. return secondToTime(ms / 1000);
  1573. }
  1574. public static String secondToTime(long second) {
  1575. // int hour = second / 3600; // 得到分钟数
  1576. // second = second % 3600;//剩余的秒数
  1577. long minute = second / 60;//得到分
  1578. second = second % 60;//剩余的秒
  1579. return String.format(Locale.getDefault(), "%02d:%02d", minute, second);
  1580. }
  1581. public static boolean isLessThanTargetTime(String targetMinutes, String startTime) {
  1582. try {
  1583. long time = TimeUtils.string2Date(startTime).getTime();
  1584. long cTime = System.currentTimeMillis();
  1585. if (cTime >= time) {
  1586. return true;
  1587. }
  1588. long limitMinutes = Long.parseLong(targetMinutes);
  1589. if (Math.abs(time - cTime) < limitMinutes * 60 * 1000) {
  1590. return true;
  1591. }
  1592. } catch (Exception e) {
  1593. e.printStackTrace();
  1594. }
  1595. return false;
  1596. }
  1597. }