当前位置:   article > 正文

Java中时间方法大全01(持续更新)_system.currenttimemillis() / 1000

system.currenttimemillis() / 1000

下面这些方法都可以封装到一个工具类中

  1.   /**
  2. * 获取当前时间的时间戳
  3. */
  4. public static int getCurrentTimeIntValue() {
  5. return (int) (System.currentTimeMillis() / 1000);
  6. }
  7. /**
  8. * 获取days天后的当前时间 时间戳
  9. */
  10. public static int addDaysTimeStamp(int days) {
  11. Calendar cal = Calendar.getInstance();
  12. cal.setTime(new Date());
  13. cal.add(Calendar.DATE, days);
  14. return (int) (cal.getTimeInMillis() / 1000);
  15. }
  16. /**
  17. * 获取今日零点的时间戳
  18. */
  19. public static int getStartTimeStamp() {
  20. Calendar todayStart = Calendar.getInstance();
  21. todayStart.set(Calendar.HOUR_OF_DAY, 0);
  22. todayStart.set(Calendar.MINUTE, 0);
  23. todayStart.set(Calendar.SECOND, 0);
  24. todayStart.set(Calendar.MILLISECOND, 0);
  25. long time = todayStart.getTimeInMillis()/1000;
  26. return (int)time;
  27. }
  28. /**
  29. * 获取今日23:59:59的时间戳
  30. */
  31. public static int getEndTimeStamp() {
  32. Calendar todayEnd = Calendar.getInstance();
  33. todayEnd.set(Calendar.HOUR_OF_DAY, 23);
  34. todayEnd.set(Calendar.MINUTE, 59);
  35. todayEnd.set(Calendar.SECOND, 59);
  36. todayEnd.set(Calendar.MILLISECOND, 999);
  37. long time = todayEnd.getTimeInMillis() / 1000;
  38. return (int) time;
  39. }
  40. /**
  41. * 获取指定时间零点的时间戳
  42. */
  43. public static int getStartTimeStamp(Date date) {
  44. Calendar todayStart = Calendar.getInstance();
  45. todayStart.setTime(date);
  46. todayStart.set(Calendar.HOUR_OF_DAY, 0);
  47. todayStart.set(Calendar.MINUTE, 0);
  48. todayStart.set(Calendar.SECOND, 0);
  49. todayStart.set(Calendar.MILLISECOND, 0);
  50. long time = todayStart.getTimeInMillis()/1000;
  51. return (int)time;
  52. }
  53. /**
  54. * 获取指定时间23:59:59的时间戳
  55. */
  56. public static int getEndTimeStamp(Date date) {
  57. Calendar todayEnd = Calendar.getInstance();
  58. todayEnd.setTime(date);
  59. todayEnd.set(Calendar.HOUR_OF_DAY, 23);
  60. todayEnd.set(Calendar.MINUTE, 59);
  61. todayEnd.set(Calendar.SECOND, 59);
  62. todayEnd.set(Calendar.MILLISECOND, 999);
  63. long time = todayEnd.getTimeInMillis() / 1000;
  64. return (int) time;
  65. }
  66. /**
  67. * 获取昨天零点的时间(字符串)
  68. */
  69. public static String getYesterDayStartTimeStamp() {
  70. SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  71. Calendar calendar = Calendar.getInstance();
  72. calendar.set(Calendar.SECOND,0); //这是将【秒】设置为0
  73. calendar.set(Calendar.MINUTE,0); //这是将【分】设置为0
  74. calendar.set(Calendar.HOUR_OF_DAY,0); //这是将【时】设置为0
  75. calendar.add(Calendar.DATE,-1); //当前日期加一
  76. String yesterday = sdfYMD.format(calendar.getTime()); //获取昨天的时间 如2021-02-25 00:00:00
  77. return yesterday;
  78. }
  79. /**
  80. * 获取昨天零点的时间戳
  81. */
  82. public static Integer getBeforeStartTime(){
  83. Calendar todayStart = Calendar.getInstance();
  84. todayStart.set(Calendar.HOUR_OF_DAY, 0);
  85. todayStart.set(Calendar.MINUTE, 0);
  86. todayStart.set(Calendar.SECOND, 0);
  87. todayStart.set(Calendar.MILLISECOND, 0);
  88. long time = todayStart.getTimeInMillis()/1000;
  89. return (int)time-86400;
  90. }
  91. /**
  92. * 获取昨天23:59:59的时间(字符串)
  93. */
  94. public static String getYesterDayEndTimeStamp() {
  95. SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  96. Calendar calendar = Calendar.getInstance();
  97. calendar.set(Calendar.SECOND,59); //这是将当天的【秒】设置为0
  98. calendar.set(Calendar.MINUTE,59); //这是将当天的【分】设置为0
  99. calendar.set(Calendar.HOUR_OF_DAY,23); //这是将当天的【时】设置为0
  100. calendar.add(Calendar.DATE,-1); //当前日期加一
  101. String yesterday = sdfYMD.format(calendar.getTime()); //获取第二天的时间 2021-02-25 00:00:00
  102. return yesterday;
  103. }
  104. /**
  105. * 获取明天零点的时间戳
  106. */
  107. public static Integer getAfterStartime()
  108. {
  109. Calendar todayStart = Calendar.getInstance();
  110. todayStart.set(Calendar.HOUR_OF_DAY, 0);
  111. todayStart.set(Calendar.MINUTE, 0);
  112. todayStart.set(Calendar.SECOND, 0);
  113. todayStart.set(Calendar.MILLISECOND, 0);
  114. long time = todayStart.getTimeInMillis()/1000;
  115. return (int)time+86400;
  116. }
  117. /**
  118. * 将昨天凌晨时间转换为Date类型(下面设置成这样 new simpleDateFormat("yyyy-MM-dd HH:mm:ss") 数据中对应的字段类型就是DateTime)
  119.    * 这个方法中调用了上面的getYesterDayStartTimeStamp()方法哦
  120. */
  121. public static Date getDateTime()
  122. {
  123. Date dateTime = null;
  124. String yesterDayStartTimeStamp = DateUtil.getYesterDayStartTimeStamp();
  125. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  126. try
  127. {
  128. dateTime = formatter.parse(yesterDayStartTimeStamp);
  129. }
  130. catch (ParseException e)
  131. {
  132. e.printStackTrace();
  133. }
  134. //Sun May 09 00:00:00 CST 2021
  135. return dateTime;
  136. }
  1. /**
  2. * @param begin 开始时间
  3. * @param end 结束时间
  4. * 获取begin - end 时间段内的每一天的日期 比如获取 2021-4-15到2021-5-10内每一天的日期
  5. */
  6. public static List<String> getDayBetweenDates(String begin, String end) throws ParseException
  7. {
  8. SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
  9. Date dBegin = sd.parse(begin); //将字符串参数转为Date类型 比如参数为 "2021-4-14" 将其转为Date类型
  10. Date dEnd = sd.parse(end);
  11. List<String> lDate = new ArrayList<>(); //创建集合, 存储begin - end 时间段内的时间
  12. lDate.add(sd.format(dBegin));
  13. Calendar calBegin = Calendar.getInstance();
  14. calBegin.setTime(dBegin);
  15. Calendar calEnd = Calendar.getInstance();
  16. calEnd.setTime(dEnd);
  17. while (dEnd.after(calBegin.getTime()))
  18. {
  19. calBegin.add(Calendar.DAY_OF_MONTH, 1);
  20. lDate.add(sd.format(calBegin.getTime()));
  21. }
  22. return lDate;
  23. }
  1. // 时分秒和时间戳互相转换
  2. public static void main(String[] args) throws ParseException {
  3. String start_time = "10800000";
  4. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
  5. Long aLong = new Long(start_time);
  6. Date date = new Date(aLong);
  7. String format = simpleDateFormat.format(date);
  8. System.out.println(format);
  9. start_time = "11:00:00";
  10. Date parse = simpleDateFormat.parse(start_time);
  11. long time = parse.getTime();
  12. System.out.println(time);
  13. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/107039
推荐阅读
相关标签
  

闽ICP备14008679号