DateTimeUtil.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.hy.common.tools;
  2. import java.lang.management.ManagementFactory;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. /**
  6. * Describe: 日 期 工 具 类
  7. * Author: 伍 月
  8. * CreateTime: 2019/10/23
  9. */
  10. public class DateTimeUtil {
  11. /**
  12. * 获启动时间
  13. *
  14. * @return {@link Date}
  15. */
  16. public static Date getServerStartDate() {
  17. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  18. return new Date(time);
  19. }
  20. /**
  21. * 计算时间差
  22. *
  23. * {@link String}
  24. * */
  25. public static String getDatePoor(Date endDate, Date nowDate) {
  26. long nd = 1000 * 24 * 60 * 60;
  27. long nh = 1000 * 60 * 60;
  28. long nm = 1000 * 60;
  29. long diff = endDate.getTime() - nowDate.getTime();
  30. // 计算差多少天
  31. long day = diff / nd;
  32. // 计算差多少小时
  33. long hour = diff % nd / nh;
  34. // 计算差多少分钟
  35. long min = diff % nd % nh / nm;
  36. // 计算差多少秒//输出结果
  37. // long sec = diff % nd % nh % nm / ns;
  38. return day + "天" + hour + "小时" + min + "分钟";
  39. }
  40. public static Date getMonthStartDate(Date date) {
  41. Calendar calendar = Calendar.getInstance();
  42. calendar.setTime(date);
  43. calendar.set(Calendar.DAY_OF_MONTH, 1);
  44. calendar.set(Calendar.HOUR_OF_DAY, 0);
  45. calendar.set(Calendar.MINUTE, 0);
  46. calendar.set(Calendar.SECOND, 0);
  47. calendar.set(Calendar.MILLISECOND, 0);
  48. return calendar.getTime();
  49. }
  50. }