BLE_WIFI_Scale_Server_Api 服务器与wifi秤交互只需要实现3个接口:设备注册、获取用户、上传记录
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DateUtils.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. package com.inet.ailink.receiver.common.utils;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.apache.commons.lang.time.FastDateFormat;
  4. import java.math.BigDecimal;
  5. import java.text.DateFormat;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Calendar;
  9. import java.util.Date;
  10. import java.util.GregorianCalendar;
  11. /**
  12. * @ClassName: DateUtils
  13. * @Description: 日期处理工具类
  14. * @author david
  15. * @date 2013-8-20 下午3:11:57
  16. *
  17. */
  18. public class DateUtils {
  19. /**
  20. * ISO8601 formatter for date without time zone. The format used is
  21. * <tt>yyyy-MM-dd</tt>.
  22. */
  23. public static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");
  24. /**
  25. * ISO8601 date format: yyyy-MM-dd
  26. */
  27. public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
  28. public static final String DATE_FORMAT_PATTERN_NO_SEPARATOR = "yyyyMMdd";
  29. /**
  30. * ISO8601 date-time format: yyyy-MM-dd HH:mm:ss
  31. */
  32. public static final String DATETIME_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";
  33. /**
  34. * for bet view format: yyyy-MM-dd HH:mm:ss
  35. */
  36. public static final String DATETIME_JSVIEW_FORMAT_PATTERN = "yyyy/MM/dd HH:mm:ss";
  37. public static final String DATETIME_SENDTIME_FORMAT_PATTERN = "yyyyMMddHHmmssSSS";
  38. /**
  39. * 日期时间格式:yyyy-MM-dd HH:mm,不显示秒
  40. */
  41. public static final String DATETIME_WITHOUT_SECOND_FORMAT_PATTERN = "yyyy-MM-dd HH:mm";
  42. /**
  43. * 日期时间格式:yyyy-MM-dd HH
  44. */
  45. public static final String DATETIME_WITHOUT_MINUTES_FORMAT_PATTERN = "yyyy-MM-dd HH";
  46. public static final String DATE_TIME_START00 = " 00:00:00";
  47. public static final String DATE_TIME_END23 = " 23:59:59";
  48. /**
  49. * 获得当前时间
  50. */
  51. public static Date currentDate() {
  52. return new Date();
  53. }
  54. public static Date getDate(Long time) {
  55. return new Date(time);
  56. }
  57. public static Long getCurTime() {
  58. return System.currentTimeMillis();
  59. }
  60. public static Date parse(String str) {
  61. return parse(str, DATE_FORMAT_PATTERN);
  62. }
  63. public static Date parse(String str, String pattern) {
  64. if (StringUtils.isBlank(str)) {
  65. return null;
  66. }
  67. DateFormat parser = new SimpleDateFormat(pattern);
  68. try {
  69. return parser.parse(str);
  70. } catch (ParseException e) {
  71. throw new IllegalArgumentException("Can't parse " + str + " using " + pattern);
  72. }
  73. }
  74. /**
  75. * 根据时间变量返回时间字符串
  76. */
  77. public static String format(Date date, String pattern) {
  78. if (date == null) {
  79. return null;
  80. }
  81. FastDateFormat df = FastDateFormat.getInstance(pattern);
  82. return df.format(date);
  83. }
  84. public static String format(Long time, String pattern) {
  85. if (time == null) {
  86. return null;
  87. }
  88. FastDateFormat df = FastDateFormat.getInstance(pattern);
  89. return df.format(new Date(time));
  90. }
  91. /**
  92. * return date format is <code>yyyy-MM-dd</code>
  93. */
  94. public static String format(Date date) {
  95. return date == null ? null : DATE_FORMAT.format(date);
  96. }
  97. public static Date getEndDateTimeOfCurrentYear() {
  98. Calendar cal = Calendar.getInstance();
  99. cal.setTime(currentDate());
  100. cal.set(Calendar.MONTH, Calendar.DECEMBER);
  101. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  102. cal.set(Calendar.HOUR_OF_DAY, 23);
  103. cal.set(Calendar.MINUTE, 59);
  104. cal.set(Calendar.SECOND, 59);
  105. return cal.getTime();
  106. }
  107. public static Date getStartDateTimeOfCurrentYear() {
  108. Calendar cal = Calendar.getInstance();
  109. cal.setTime(currentDate());
  110. cal.set(Calendar.MONTH, Calendar.JANUARY);
  111. cal.set(Calendar.DAY_OF_MONTH, 1);
  112. cal.set(Calendar.HOUR_OF_DAY, 0);
  113. cal.set(Calendar.MINUTE, 0);
  114. cal.set(Calendar.SECOND, 0);
  115. return parse(format(cal.getTime()));
  116. }
  117. public static Date getStartDateTimeOfYear(int year) {
  118. Calendar cal = Calendar.getInstance();
  119. cal.set(Calendar.YEAR, year);
  120. cal.set(Calendar.MONTH, Calendar.JANUARY);
  121. cal.set(Calendar.DAY_OF_MONTH, 1);
  122. cal.set(Calendar.HOUR_OF_DAY, 0);
  123. cal.set(Calendar.MINUTE, 0);
  124. cal.set(Calendar.SECOND, 0);
  125. return parse(format(cal.getTime()));
  126. }
  127. public static Date getEndDateTimeOfYear(int year) {
  128. Calendar cal = Calendar.getInstance();
  129. cal.set(Calendar.YEAR, year);
  130. cal.set(Calendar.MONTH, Calendar.DECEMBER);
  131. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  132. cal.set(Calendar.HOUR_OF_DAY, 23);
  133. cal.set(Calendar.MINUTE, 59);
  134. cal.set(Calendar.SECOND, 59);
  135. return cal.getTime();
  136. }
  137. public static Date getStartTimeOfCurrentDate() {
  138. return getStartTimeOfDate(currentDate());
  139. }
  140. public static Date getEndTimeOfCurrentDate() {
  141. return getEndTimeOfDate(currentDate());
  142. }
  143. public static Date getStartTimeOfCurrentMonth() {
  144. return getStartDateTimeOfMonth(DateUtils.currentDate());
  145. }
  146. public static Date getEndTimeOfCurrentMonth() {
  147. return getEndDateTimeOfMonth(DateUtils.currentDate());
  148. }
  149. public static Date getStartTimeOfDate(Date date) {
  150. Calendar cal = Calendar.getInstance();
  151. cal.setTime(date);
  152. cal.set(Calendar.HOUR_OF_DAY, 0);
  153. cal.set(Calendar.MINUTE, 0);
  154. cal.set(Calendar.SECOND, 0);
  155. return parse(format(cal.getTime()));
  156. }
  157. public static Date getEndTimeOfDate(Date date) {
  158. Calendar cal = Calendar.getInstance();
  159. cal.setTime(date);
  160. cal.set(Calendar.HOUR_OF_DAY, 23);
  161. cal.set(Calendar.MINUTE, 59);
  162. cal.set(Calendar.SECOND, 59);
  163. return cal.getTime();
  164. }
  165. public static Date getSpecialEndTimeOfDate(Date date) {
  166. Calendar cal = Calendar.getInstance();
  167. cal.setTime(date);
  168. cal.set(Calendar.HOUR_OF_DAY, 24);
  169. cal.set(Calendar.MINUTE, 00);
  170. cal.set(Calendar.SECOND, 00);
  171. return cal.getTime();
  172. }
  173. public static Date getStartDateTimeOfMonth(Date date) {
  174. Calendar cal = Calendar.getInstance();
  175. cal.setTime(date);
  176. cal.set(Calendar.DAY_OF_MONTH, 1);
  177. cal.set(Calendar.HOUR_OF_DAY, 0);
  178. cal.set(Calendar.MINUTE, 0);
  179. cal.set(Calendar.SECOND, 0);
  180. return parse(format(cal.getTime()));
  181. }
  182. public static Date getStartDateTimeOfMonth(int year, int month) {
  183. Calendar cal = Calendar.getInstance();
  184. cal.set(Calendar.YEAR, year);
  185. cal.set(Calendar.MONTH, month - 1);
  186. cal.set(Calendar.DAY_OF_MONTH, 1);
  187. cal.set(Calendar.HOUR_OF_DAY, 0);
  188. cal.set(Calendar.MINUTE, 0);
  189. cal.set(Calendar.SECOND, 0);
  190. return parse(format(cal.getTime()));
  191. }
  192. public static Date getEndDateTimeOfMonth(Date date) {
  193. Calendar cal = Calendar.getInstance();
  194. cal.setTime(date);
  195. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  196. cal.set(Calendar.HOUR_OF_DAY, 23);
  197. cal.set(Calendar.MINUTE, 59);
  198. cal.set(Calendar.SECOND, 59);
  199. return cal.getTime();
  200. }
  201. public static Date addHours(Date date, int hours) {
  202. return add(date, Calendar.HOUR_OF_DAY, hours);
  203. }
  204. public static Date addMinutes(Date date, int minutes) {
  205. return add(date, Calendar.MINUTE, minutes);
  206. }
  207. public static Date addSeconds(Date date, int seconds) {
  208. return add(date, Calendar.SECOND, seconds);
  209. }
  210. public static Date addDays(Date date, int days) {
  211. return add(date, Calendar.DATE, days);
  212. }
  213. public static Date addMonths(Date date, int months) {
  214. return add(date, Calendar.MONTH, months);
  215. }
  216. public static Date addYears(Date date, int years) {
  217. return add(date, Calendar.YEAR, years);
  218. }
  219. private static Date add(Date date, int field, int amount) {
  220. Calendar cal = Calendar.getInstance();
  221. cal.setTime(date);
  222. cal.add(field, amount);
  223. return cal.getTime();
  224. }
  225. public static long calcDateBetween(Date start, Date end) {
  226. if (start == null || end == null) {
  227. return 0;
  228. }
  229. return ((end.getTime() - start.getTime()) / 86400001) + 1;
  230. }
  231. public static long calcHoursBetween(Date start, Date end) {
  232. if (start == null || end == null) {
  233. return 0;
  234. }
  235. return ((end.getTime() - start.getTime()) / (60000 * 60));
  236. }
  237. public static Double calcHoursDoubleBetween(Date start, Date end) {
  238. if (start == null || end == null) {
  239. return 0d;
  240. }
  241. Double time = ((end.getTime() - start.getTime()) / (60000.0 * 60.0));
  242. return Double.valueOf(new java.text.DecimalFormat("#.0").format(time));
  243. }
  244. public static Double calcHoursDouble(Date start, Date end) {
  245. if (start == null || end == null) {
  246. return 0d;
  247. }
  248. // (new BigDecimal(end.getTime()).subtract(new
  249. // BigDecimal(start.getTime()))).divide(new BigDecimal(60000.0*60.0), 7,
  250. // BigDecimal.ROUND_HALF_UP).doubleValue();
  251. Double time = (new BigDecimal(end.getTime()).subtract(new BigDecimal(start.getTime())))
  252. .divide(new BigDecimal(60000.0 * 60.0), 7, BigDecimal.ROUND_HALF_UP).doubleValue();// ((end.getTime() -
  253. // start.getTime())
  254. // /
  255. // (60000.0*60.0));
  256. return time;
  257. }
  258. public static long calcMinutesBetween(Date start, Date end) {
  259. if (start == null || end == null) {
  260. return 0;
  261. }
  262. return ((end.getTime() - start.getTime()) / 60000);
  263. }
  264. public static long calcSecondsBetween(Date start, Date end) {
  265. if (start == null || end == null) {
  266. return 0;
  267. }
  268. return ((end.getTime() - start.getTime()) / 1000);
  269. }
  270. public static long calcSecondsBetween(Long start, Long end) {
  271. return (end - start) / 1000;
  272. }
  273. /**
  274. * 获得日期是否为星期天。
  275. *
  276. * @param date 日期
  277. * @return
  278. */
  279. public static boolean isSunday(Date date) {
  280. return getDate(date) == Calendar.SUNDAY;
  281. }
  282. /**
  283. * 获得日期是否为星期一。
  284. *
  285. * @param date 日期
  286. * @return
  287. */
  288. public static boolean isMonday(Date date) {
  289. return getDate(date) == Calendar.MONDAY;
  290. }
  291. /**
  292. * 获得日期是否为星期二。
  293. *
  294. * @param date 日期
  295. * @return
  296. */
  297. public static boolean isTuesday(Date date) {
  298. return getDate(date) == Calendar.TUESDAY;
  299. }
  300. /**
  301. * 获得日期是否为星期三。
  302. *
  303. * @param date 日期
  304. * @return
  305. */
  306. public static boolean isWednesday(Date date) {
  307. return getDate(date) == Calendar.WEDNESDAY;
  308. }
  309. /**
  310. * 获得日期是否为星期四。
  311. *
  312. * @param date 日期
  313. * @return
  314. */
  315. public static boolean isThursday(Date date) {
  316. return getDate(date) == Calendar.THURSDAY;
  317. }
  318. /**
  319. * 获得日期是否为星期五。
  320. *
  321. * @param date 日期
  322. * @return
  323. */
  324. public static boolean isFriday(Date date) {
  325. return getDate(date) == Calendar.FRIDAY;
  326. }
  327. /**
  328. * 获得日期是否为星期六。
  329. *
  330. * @param date 日期
  331. * @return
  332. */
  333. public static boolean isSaturday(Date date) {
  334. return getDate(date) == Calendar.SATURDAY;
  335. }
  336. public static int getDate(Date date) {
  337. Calendar cal = Calendar.getInstance();
  338. cal.setTime(date);
  339. return cal.get(Calendar.DAY_OF_WEEK);
  340. }
  341. /**
  342. * 获得相对于今天的昨天的时间。
  343. *
  344. * @return 昨天此时。
  345. */
  346. public static Date getYesterday() {
  347. return addDays(currentDate(), -1);
  348. }
  349. /**
  350. * 获得月份
  351. *
  352. * @param date
  353. * @return
  354. */
  355. public static int getMonth(Date date) {
  356. Calendar calendar = Calendar.getInstance();
  357. calendar.setTime(date);
  358. return calendar.get(Calendar.MONTH) + 1;
  359. }
  360. /**
  361. * 获得年份
  362. *
  363. * @param date
  364. * @return
  365. */
  366. public static int getYear(Date date) {
  367. Calendar calendar = Calendar.getInstance();
  368. calendar.setTime(date);
  369. return calendar.get(Calendar.YEAR);
  370. }
  371. /**
  372. * 获得天
  373. *
  374. * @param date
  375. * @return
  376. */
  377. public static int getDay(Date date) {
  378. Calendar calendar = Calendar.getInstance();
  379. calendar.setTime(date);
  380. return calendar.get(Calendar.DATE);
  381. }
  382. /**
  383. * 获得天
  384. *
  385. * @param date
  386. * @return
  387. */
  388. public static int getDayOfYear(Date date) {
  389. Calendar calendar = Calendar.getInstance();
  390. calendar.setTime(date);
  391. return calendar.get(Calendar.DAY_OF_YEAR);
  392. }
  393. /**
  394. * 获得小时
  395. *
  396. * @param date
  397. * @return
  398. */
  399. public static int getHours(Date date) {
  400. Calendar calendar = Calendar.getInstance();
  401. calendar.setTime(date);
  402. return calendar.get(Calendar.HOUR_OF_DAY);
  403. }
  404. public static int getMinutes(Date date) {
  405. Calendar calendar = Calendar.getInstance();
  406. calendar.setTime(date);
  407. return calendar.get(Calendar.MINUTE);
  408. }
  409. public static int getSeconds(Date date) {
  410. Calendar calendar = Calendar.getInstance();
  411. calendar.setTime(date);
  412. return calendar.get(Calendar.SECOND);
  413. }
  414. public static int getMILLISECOND(Date date) {
  415. Calendar calendar = Calendar.getInstance();
  416. calendar.setTime(date);
  417. return calendar.get(Calendar.MILLISECOND);
  418. }
  419. public static long getTimeDiff(Date beginDate, Date endDate) {
  420. return (endDate.getTime() - beginDate.getTime()) / 1000;
  421. }
  422. public static int getDaysOfMonth(int year, int month) {
  423. Calendar cal = Calendar.getInstance();
  424. cal.set(Calendar.YEAR, year);
  425. cal.set(Calendar.MONTH, month);// 7月
  426. return cal.getActualMaximum(Calendar.DATE);
  427. }
  428. public static long convertDate2Long(Date date) {
  429. if (date == null) {
  430. return 0l;
  431. }
  432. return date.getTime();
  433. }
  434. public static Date convertLong2Date(Long unixTimestamp) {
  435. if (unixTimestamp != null) {
  436. return new Date(unixTimestamp);
  437. }
  438. return null;
  439. }
  440. /**
  441. * 判断时间是否在指定的时间范围内。
  442. *
  443. * @param low
  444. * @param high
  445. * @return
  446. */
  447. public static boolean between(Date low, Date high) {
  448. return currentDate().after(low) && currentDate().before(high);
  449. }
  450. public static int getDayValue(Date date) {
  451. Calendar cal = Calendar.getInstance();
  452. cal.setTime(date);
  453. int i = cal.get(Calendar.DAY_OF_WEEK);
  454. if (i == 1) {
  455. return 7;
  456. } else {
  457. return i - 1;
  458. }
  459. }
  460. public static boolean isBefore(Date date1, Date date2) {
  461. return date1.getTime() < date2.getTime();
  462. }
  463. public static Date average(Date date1, Date date2) {
  464. return new Date((date1.getTime() + date2.getTime()) / 2);
  465. }
  466. public static int getDaysOfMonth(Date date) {
  467. Calendar cal = Calendar.getInstance();
  468. cal.setTime(date);
  469. return cal.get(Calendar.DAY_OF_MONTH);
  470. }
  471. public static int getCurrentRemainingSeconds() {
  472. // *** 计算过期时间 一整天 获取 时:分:秒
  473. Calendar calendar = Calendar.getInstance();
  474. int hours = calendar.get(Calendar.HOUR_OF_DAY); // 时
  475. int minutes = calendar.get(Calendar.MINUTE); // 分
  476. int seconds = calendar.get(Calendar.SECOND); // 秒
  477. return 24 * 60 * 60 - hours * 60 * 60 - minutes * 60 - seconds;
  478. }
  479. public static Long getTimeLong(String str) {
  480. return parse(str, DATETIME_FORMAT_PATTERN).getTime();
  481. }
  482. public static String getStringTodayC() {
  483. Date currentTime = new Date();
  484. SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  485. String dateString = formatter.format(currentTime);
  486. return dateString;
  487. }
  488. ///////////////////////////////////////////////////
  489. // 引用s https://blog.csdn.net/tz_gx/article/details/25284237
  490. // s 上月第一天
  491. public static Date getPreviousMonthDayBegin() {
  492. Calendar lastDate = Calendar.getInstance();
  493. lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
  494. lastDate.add(Calendar.MONTH, -1);// 减一个月,变为上月的1号
  495. return lastDate.getTime();
  496. }
  497. // s 获得上月最后一天的日期
  498. public static Date getPreviousMonthDayEnd() {
  499. Calendar lastDate = Calendar.getInstance();
  500. lastDate.add(Calendar.MONTH, -1);// 减一个月
  501. lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
  502. lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天
  503. return lastDate.getTime();
  504. }
  505. // s 获取当月第一天
  506. public static Date getCurrentMonthDayBegin() {
  507. Calendar lastDate = Calendar.getInstance();
  508. lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
  509. return lastDate.getTime();
  510. }
  511. // s 计算当月最后一天,返回字符串
  512. public static Date getCurrentMonthDayEnd() {
  513. Calendar lastDate = Calendar.getInstance();
  514. lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
  515. lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号
  516. lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天
  517. return lastDate.getTime();
  518. }
  519. // s 获得本周一的日期
  520. public static Date getCurrentWeekDayBegin() {
  521. int mondayPlus = getMondayPlus();
  522. GregorianCalendar currentDate = new GregorianCalendar();
  523. currentDate.add(GregorianCalendar.DATE, mondayPlus);
  524. return currentDate.getTime();
  525. }
  526. // s 获得本周星期日的日期
  527. public static Date getCurrentWeekDayEnd() {
  528. int mondayPlus = getMondayPlus();
  529. GregorianCalendar currentDate = new GregorianCalendar();
  530. currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
  531. Date monday = currentDate.getTime();
  532. return monday;
  533. }
  534. // s 获得当前日期与本周一相差的天数
  535. private static int getMondayPlus() {
  536. Calendar cd = Calendar.getInstance();
  537. // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
  538. int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // s 因为按中国礼拜一作为第一天所以这里减1
  539. if (dayOfWeek == 1) {
  540. return 0;
  541. } else {
  542. return 1 - dayOfWeek;
  543. }
  544. }
  545. // s 获得上周星期一的日期
  546. public static Date getPreviousWeekDayBegin() {
  547. int mondayPlus = getMondayPlus();
  548. GregorianCalendar currentDate = new GregorianCalendar();
  549. currentDate.add(GregorianCalendar.DATE, mondayPlus - 7);
  550. return currentDate.getTime();
  551. }
  552. // s 获得上周星期日的日期
  553. public static Date getPreviousWeekDayEnd() {
  554. int mondayPlus = getMondayPlus();
  555. GregorianCalendar currentDate = new GregorianCalendar();
  556. currentDate.add(GregorianCalendar.DATE, mondayPlus - 1);
  557. return currentDate.getTime();
  558. }
  559. public static int getAge(Date birthDay){
  560. Calendar cal = Calendar.getInstance();
  561. if (cal.before(birthDay)) { //出生日期晚于当前时间,无法计算
  562. return 0;
  563. }
  564. int yearNow = cal.get(Calendar.YEAR); //当前年份
  565. int monthNow = cal.get(Calendar.MONTH); //当前月份
  566. int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
  567. cal.setTime(birthDay);
  568. int yearBirth = cal.get(Calendar.YEAR);
  569. int monthBirth = cal.get(Calendar.MONTH);
  570. int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
  571. int age = yearNow - yearBirth; //计算整岁数
  572. if (monthNow <= monthBirth) {
  573. if (monthNow == monthBirth) {
  574. if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前,年龄减一
  575. }else{
  576. age--;//当前月份在生日之前,年龄减一
  577. }
  578. }
  579. return age;
  580. }
  581. ///////////////////////////////////
  582. public static void main(String[] args) {
  583. // System.out.println(getMILLISECOND(new Date()));
  584. System.out.println(format(getPreviousWeekDayEnd()));
  585. }
  586. }