• Java获取某天、某月或某年的开始结束日期或开始结束时间戳


    1. @Slf4j
    2. public class DateTimeUtil {
    3. /**
    4. * 默认日期格式
    5. */
    6. public static String DEFAULT_FORMAT = "yyyy-MM-dd";
    7. /**
    8. * 获取上一年年份
    9. *
    10. * @return
    11. */
    12. public static Integer getLastYear() {
    13. SimpleDateFormat formats = new SimpleDateFormat("yyyy");
    14. Calendar c = Calendar.getInstance();
    15. c.add(Calendar.YEAR, -1);
    16. Date date = c.getTime();
    17. // Date类型转String类型
    18. String format = formats.format(date);
    19. // String类型转int类型
    20. int parseInt = Integer.parseInt(format);
    21. log.info("去年为{}年", parseInt);
    22. return parseInt;
    23. }
    24. /**
    25. * 格式化日期
    26. *
    27. * @param date 日期对象
    28. * @return String 日期字符串
    29. */
    30. public static String formatDate(Date date) {
    31. SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT);
    32. return f.format(date);
    33. }
    34. /**
    35. * 获取当年的第一天
    36. *
    37. * @param
    38. * @return
    39. */
    40. public static Date getCurrYearFirst() {
    41. Calendar currCal = Calendar.getInstance();
    42. int currentYear = currCal.get(Calendar.YEAR);
    43. return getYearFirst(currentYear);
    44. }
    45. /**
    46. * 获取当年的最后一天
    47. *
    48. * @param
    49. * @return
    50. */
    51. public static Date getCurrYearLast() {
    52. Calendar currCal = Calendar.getInstance();
    53. int currentYear = currCal.get(Calendar.YEAR);
    54. return getYearLast(currentYear);
    55. }
    56. /**
    57. * 获取某年第一天日期
    58. *
    59. * @param year 年份
    60. * @return Date
    61. */
    62. public static Date getYearFirst(int year) {
    63. Calendar calendar = Calendar.getInstance();
    64. calendar.clear();
    65. calendar.set(Calendar.YEAR, year);
    66. Date currYearFirst = calendar.getTime();
    67. String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currYearFirst);
    68. log.info("去年第一天为{}", nowTime);
    69. return currYearFirst;
    70. }
    71. /**
    72. * 获取某年最后一天日期
    73. *
    74. * @param year 年份
    75. * @return Date
    76. */
    77. public static Date getYearLast(int year) {
    78. Calendar calendar = Calendar.getInstance();
    79. calendar.clear();
    80. calendar.set(Calendar.YEAR, year);
    81. calendar.roll(Calendar.DAY_OF_YEAR, -1);
    82. return calendar.getTime();
    83. }
    84. /**
    85. * 获取指定某一天的开始时间戳
    86. *
    87. * @param timeStamp 毫秒级时间戳
    88. * @param timeZone 如 GMT+8:00
    89. * @return
    90. */
    91. public static Long getDailyStartTime(Long timeStamp, String timeZone) {
    92. Calendar calendar = Calendar.getInstance();
    93. calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
    94. calendar.setTimeInMillis(timeStamp);
    95. calendar.set(Calendar.HOUR_OF_DAY, 0);
    96. calendar.set(Calendar.SECOND, 0);
    97. calendar.set(Calendar.MINUTE, 0);
    98. calendar.set(Calendar.MILLISECOND, 0);
    99. return calendar.getTimeInMillis();
    100. }
    101. /**
    102. * 获取指定某一天的结束时间戳
    103. *
    104. * @param timeStamp 毫秒级时间戳
    105. * @param timeZone 如 GMT+8:00
    106. * @return
    107. */
    108. public static Long getDailyEndTime(Long timeStamp, String timeZone) {
    109. Calendar calendar = Calendar.getInstance();
    110. calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
    111. calendar.setTimeInMillis(timeStamp);
    112. calendar.set(Calendar.HOUR_OF_DAY, 23);
    113. calendar.set(Calendar.MINUTE, 59);
    114. calendar.set(Calendar.SECOND, 59);
    115. calendar.set(Calendar.MILLISECOND, 999);
    116. return calendar.getTimeInMillis();
    117. }
    118. /**
    119. * 获取当月开始时间戳
    120. *
    121. * @param timeStamp 毫秒级时间戳
    122. * @param timeZone 如 GMT+8:00
    123. * @return
    124. */
    125. public static Long getMonthStartTime(Long timeStamp, String timeZone) {
    126. Calendar calendar = Calendar.getInstance();// 获取当前日期
    127. calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
    128. calendar.setTimeInMillis(timeStamp);
    129. calendar.add(Calendar.YEAR, 0);
    130. calendar.add(Calendar.MONTH, 0);
    131. calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
    132. calendar.set(Calendar.HOUR_OF_DAY, 0);
    133. calendar.set(Calendar.MINUTE, 0);
    134. calendar.set(Calendar.SECOND, 0);
    135. calendar.set(Calendar.MILLISECOND, 0);
    136. return calendar.getTimeInMillis();
    137. }
    138. /**
    139. * 获取当月的结束时间戳
    140. *
    141. * @param timeStamp 毫秒级时间戳
    142. * @param timeZone 如 GMT+8:00
    143. * @return
    144. */
    145. public static Long getMonthEndTime(Long timeStamp, String timeZone) {
    146. Calendar calendar = Calendar.getInstance();// 获取当前日期
    147. calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
    148. calendar.setTimeInMillis(timeStamp);
    149. calendar.add(Calendar.YEAR, 0);
    150. calendar.add(Calendar.MONTH, 0);
    151. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天
    152. calendar.set(Calendar.HOUR_OF_DAY, 23);
    153. calendar.set(Calendar.MINUTE, 59);
    154. calendar.set(Calendar.SECOND, 59);
    155. calendar.set(Calendar.MILLISECOND, 999);
    156. return calendar.getTimeInMillis();
    157. }
    158. /**
    159. * 获取当年的开始时间戳
    160. *
    161. * @param timeStamp 毫秒级时间戳
    162. * @param "GMT+8:00" 如 GMT+8:00
    163. * @return
    164. */
    165. public static Long getYearStartTime(Long timeStamp) {
    166. Calendar calendar = Calendar.getInstance();// 获取当前日期
    167. calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
    168. calendar.setTimeInMillis(timeStamp);
    169. calendar.add(Calendar.YEAR, 0);
    170. calendar.add(Calendar.DATE, 0);
    171. calendar.add(Calendar.MONTH, 0);
    172. calendar.set(Calendar.DAY_OF_YEAR, 1);
    173. calendar.set(Calendar.HOUR_OF_DAY, 0);
    174. calendar.set(Calendar.MINUTE, 0);
    175. calendar.set(Calendar.SECOND, 0);
    176. calendar.set(Calendar.MILLISECOND, 0);
    177. return calendar.getTimeInMillis();
    178. }
    179. /**
    180. * 获取当年的最后时间戳
    181. *
    182. * @param timeStamp 毫秒级时间戳
    183. * @param "GMT+8:00" 如 GMT+8:00
    184. * @return
    185. */
    186. public static Long getYearEndTime(Long timeStamp) {
    187. Calendar calendar = Calendar.getInstance();// 获取当前日期
    188. calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
    189. calendar.setTimeInMillis(timeStamp);
    190. int year = calendar.get(Calendar.YEAR);
    191. calendar.clear();
    192. calendar.set(Calendar.YEAR, year);
    193. calendar.set(Calendar.HOUR_OF_DAY, 23);
    194. calendar.set(Calendar.MINUTE, 59);
    195. calendar.set(Calendar.SECOND, 59);
    196. calendar.set(Calendar.MILLISECOND, 999);
    197. calendar.roll(Calendar.DAY_OF_YEAR, -1);
    198. return calendar.getTimeInMillis();
    199. }
    200. /**
    201. * 获取当天的开始时间和结束时间
    202. */
    203. public static void main(String[] args) {
    204. Date yearFirst = getYearFirst(2023);
    205. System.out.println("今年第一天:" + yearFirst.getTime());
    206. Date yearLast = getYearLast(2023);
    207. System.out.println("今年最后一天:" + yearLast.getTime());
    208. // 获取当天开始时间-00:00:00
    209. LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
    210. String todayStartStr = todayStart.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    211. // 获取当天结束时间-23:59:59
    212. LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
    213. String todayEndStr = todayEnd.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    214. }
    215. }

  • 相关阅读:
    【JavaEE初阶】 Thread类及常见方法
    什么是IDE?新手用哪个IDE比较好?
    sqlserver中查询每个人最新一条记录语法
    ubuntu18.04 terminal打不开的解决方法
    MySQL——MySQL的基础操作部分
    【阿里云天池大赛赛题解析】 刷书笔记 Lesson 1 数据探索基础知识
    代理IP和Socks5代理:跨界电商与全球爬虫的关键技术
    Ubuntu配置FTP服务
    Python:实现merge sort归并排序算法(附完整源码)
    JS实时获取录音数据并播放
  • 原文地址:https://blog.csdn.net/Just_do_it_HZF/article/details/134528275