1. package com.dada.test;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. public class TestDate {
  6. public static void main(String[] args) {
  7. System.out.println("当前小时开始:"+getCurrentHourStartTime().toString());
  8. System.out.println("当前小时结束:"+getCurrentHourEndTime().toString());
  9. System.out.println("当前天开始:"+getCurrentDayStartTime().toString());
  10. System.out.println("当前天时结束:"+getCurrentDayEndTime().toString());
  11. System.out.println("当前周开始:"+getCurrentWeekDayStartTime().toString());
  12. System.out.println("当前周结束:"+getCurrentWeekDayEndTime().toString());
  13. System.out.println("当前月开始:"+getCurrentMonthStartTime().toString());
  14. System.out.println("当前月结束:"+getCurrentMonthEndTime().toString());
  15. System.out.println("当前季度开始:"+getCurrentQuarterStartTime().toString());
  16. System.out.println("当前季度结束:"+getCurrentQuarterEndTime().toString());
  17. System.out.println("当前半年/后半年开始:"+getHalfYearStartTime().toString());
  18. System.out.println("当前半年/后半年结束:"+getHalfYearEndTime().toString());
  19. System.out.println("当前年开始:"+getCurrentYearStartTime().toString());
  20. System.out.println("当前年结束:"+getCurrentYearEndTime().toString());
  21. }
  22. /**
  23. * 获取 当前年、半年、季度、月、日、小时 开始结束时间
  24. */
  25. private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
  26. private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");;
  27. private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");;
  28. /**
  29. * 获得本周的第一天,周一
  30. *
  31. * @return
  32. */
  33. public static Date getCurrentWeekDayStartTime() {
  34. Calendar c = Calendar.getInstance();
  35. try {
  36. int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
  37. c.add(Calendar.DATE, -weekday);
  38. c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. return c.getTime();
  43. }
  44. /**
  45. * 获得本周的最后一天,周日
  46. *
  47. * @return
  48. */
  49. public static Date getCurrentWeekDayEndTime() {
  50. Calendar c = Calendar.getInstance();
  51. try {
  52. int weekday = c.get(Calendar.DAY_OF_WEEK);
  53. c.add(Calendar.DATE, 8 - weekday);
  54. c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. return c.getTime();
  59. }
  60. /**
  61. * 获得本天的开始时间,即2012-01-01 00:00:00
  62. *
  63. * @return
  64. */
  65. public static Date getCurrentDayStartTime() {
  66. Date now = new Date();
  67. try {
  68. now = shortSdf.parse(shortSdf.format(now));
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. return now;
  73. }
  74. /**
  75. * 获得本天的结束时间,即2012-01-01 23:59:59
  76. *
  77. * @return
  78. */
  79. public static Date getCurrentDayEndTime() {
  80. Date now = new Date();
  81. try {
  82. now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
  83. } catch (Exception e) {
  84. e.printStackTrace();
  85. }
  86. return now;
  87. }
  88. /**
  89. * 获得本小时的开始时间,即2012-01-01 23:59:59
  90. *
  91. * @return
  92. */
  93. public static Date getCurrentHourStartTime() {
  94. Date now = new Date();
  95. try {
  96. now = longHourSdf.parse(longHourSdf.format(now));
  97. } catch (Exception e) {
  98. e.printStackTrace();
  99. }
  100. return now;
  101. }
  102. /**
  103. * 获得本小时的结束时间,即2012-01-01 23:59:59
  104. *
  105. * @return
  106. */
  107. public static Date getCurrentHourEndTime() {
  108. Date now = new Date();
  109. try {
  110. now = longSdf.parse(longHourSdf.format(now) + ":59:59");
  111. } catch (Exception e) {
  112. e.printStackTrace();
  113. }
  114. return now;
  115. }
  116. /**
  117. * 获得本月的开始时间,即2012-01-01 00:00:00
  118. *
  119. * @return
  120. */
  121. public static Date getCurrentMonthStartTime() {
  122. Calendar c = Calendar.getInstance();
  123. Date now = null;
  124. try {
  125. c.set(Calendar.DATE, 1);
  126. now = shortSdf.parse(shortSdf.format(c.getTime()));
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. }
  130. return now;
  131. }
  132. /**
  133. * 当前月的结束时间,即2012-01-31 23:59:59
  134. *
  135. * @return
  136. */
  137. public static Date getCurrentMonthEndTime() {
  138. Calendar c = Calendar.getInstance();
  139. Date now = null;
  140. try {
  141. c.set(Calendar.DATE, 1);
  142. c.add(Calendar.MONTH, 1);
  143. c.add(Calendar.DATE, -1);
  144. now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
  145. } catch (Exception e) {
  146. e.printStackTrace();
  147. }
  148. return now;
  149. }
  150. /**
  151. * 当前年的开始时间,即2012-01-01 00:00:00
  152. *
  153. * @return
  154. */
  155. public static Date getCurrentYearStartTime() {
  156. Calendar c = Calendar.getInstance();
  157. Date now = null;
  158. try {
  159. c.set(Calendar.MONTH, 0);
  160. c.set(Calendar.DATE, 1);
  161. now = shortSdf.parse(shortSdf.format(c.getTime()));
  162. } catch (Exception e) {
  163. e.printStackTrace();
  164. }
  165. return now;
  166. }
  167. /**
  168. * 当前年的结束时间,即2012-12-31 23:59:59
  169. *
  170. * @return
  171. */
  172. public static Date getCurrentYearEndTime() {
  173. Calendar c = Calendar.getInstance();
  174. Date now = null;
  175. try {
  176. c.set(Calendar.MONTH, 11);
  177. c.set(Calendar.DATE, 31);
  178. now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
  179. } catch (Exception e) {
  180. e.printStackTrace();
  181. }
  182. return now;
  183. }
  184. /**
  185. * 当前季度的开始时间,即2012-01-1 00:00:00
  186. *
  187. * @return
  188. */
  189. public static Date getCurrentQuarterStartTime() {
  190. Calendar c = Calendar.getInstance();
  191. int currentMonth = c.get(Calendar.MONTH) + 1;
  192. Date now = null;
  193. try {
  194. if (currentMonth >= 1 && currentMonth <= 3)
  195. c.set(Calendar.MONTH, 0);
  196. else if (currentMonth >= 4 && currentMonth <= 6)
  197. c.set(Calendar.MONTH, 3);
  198. else if (currentMonth >= 7 && currentMonth <= 9)
  199. c.set(Calendar.MONTH, 4);
  200. else if (currentMonth >= 10 && currentMonth <= 12)
  201. c.set(Calendar.MONTH, 9);
  202. c.set(Calendar.DATE, 1);
  203. now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
  204. } catch (Exception e) {
  205. e.printStackTrace();
  206. }
  207. return now;
  208. }
  209. /**
  210. * 当前季度的结束时间,即2012-03-31 23:59:59
  211. *
  212. * @return
  213. */
  214. public static Date getCurrentQuarterEndTime() {
  215. Calendar c = Calendar.getInstance();
  216. int currentMonth = c.get(Calendar.MONTH) + 1;
  217. Date now = null;
  218. try {
  219. if (currentMonth >= 1 && currentMonth <= 3) {
  220. c.set(Calendar.MONTH, 2);
  221. c.set(Calendar.DATE, 31);
  222. } else if (currentMonth >= 4 && currentMonth <= 6) {
  223. c.set(Calendar.MONTH, 5);
  224. c.set(Calendar.DATE, 30);
  225. } else if (currentMonth >= 7 && currentMonth <= 9) {
  226. c.set(Calendar.MONTH, 8);
  227. c.set(Calendar.DATE, 30);
  228. } else if (currentMonth >= 10 && currentMonth <= 12) {
  229. c.set(Calendar.MONTH, 11);
  230. c.set(Calendar.DATE, 31);
  231. }
  232. now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
  233. } catch (Exception e) {
  234. e.printStackTrace();
  235. }
  236. return now;
  237. }
  238. /**
  239. * 获取前/后半年的开始时间
  240. *
  241. * @return
  242. */
  243. public static Date getHalfYearStartTime() {
  244. Calendar c = Calendar.getInstance();
  245. int currentMonth = c.get(Calendar.MONTH) + 1;
  246. Date now = null;
  247. try {
  248. if (currentMonth >= 1 && currentMonth <= 6) {
  249. c.set(Calendar.MONTH, 0);
  250. } else if (currentMonth >= 7 && currentMonth <= 12) {
  251. c.set(Calendar.MONTH, 6);
  252. }
  253. c.set(Calendar.DATE, 1);
  254. now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
  255. } catch (Exception e) {
  256. e.printStackTrace();
  257. }
  258. return now;
  259. }
  260. /**
  261. * 获取前/后半年的结束时间
  262. *
  263. * @return
  264. */
  265. public static Date getHalfYearEndTime() {
  266. Calendar c = Calendar.getInstance();
  267. int currentMonth = c.get(Calendar.MONTH) + 1;
  268. Date now = null;
  269. try {
  270. if (currentMonth >= 1 && currentMonth <= 6) {
  271. c.set(Calendar.MONTH, 5);
  272. c.set(Calendar.DATE, 30);
  273. } else if (currentMonth >= 7 && currentMonth <= 12) {
  274. c.set(Calendar.MONTH, 11);
  275. c.set(Calendar.DATE, 31);
  276. }
  277. now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
  278. } catch (Exception e) {
  279. e.printStackTrace();
  280. }
  281. return now;
  282. }
  283. }

转载于:https://www.cnblogs.com/telwanggs/p/4533252.html

获取当前时间的天、周、月、季度、半年度、年度开始和结束时间相关推荐

  1. layui时间选择器选择周和季度

    layui时间选择器选择周和季度 最近一个后台管理页面数据查询时间间隔要有:日,周,月,季度,年:UI组件我们用的layui,官方dome只有日月年(直接去ctrl+c ctrl+v就好了,在这就不多 ...

  2. java8 日期范围内 日/周/月/季度/年 的日期结果集

    java8 日期范围内 日/周/月/季度/年 的日期结果集 /*** 根据时间范围列出所有日/周/月/季/年** @Author Moqi* @Date 2020/4/30* @Version V1. ...

  3. mysql分季度统计数据,mysql按日周月季度统计数据

    mysql按日周月季度统计数据 mysql按日.周.月.季度统计数据 1.使用DATE_FORMAT做等值条件查询 2.DATE_FORMAT函数语法,参考w3school 3.可使用格式 4.使用示 ...

  4. 英雄联盟7月23日维修服务器,英雄联盟7月23日更新维护到几点结束 7月23日lol10.15版本更新维护结束时间...

    英雄联盟7月23日更新维护到几点结束?英雄联盟在2020年7月23日停机更新10.15版本内容,在lol10.15版本中不仅有新英雄莉莉娅和新系列皮肤灵魂莲华推出,极限闪击模式也回归了.想体验新版本的 ...

  5. FullCalendar在月视图中定义日程/事件显示结束时间

    目录 FullCalendar在月视图中定义日程/事件显示结束时间 配置 换行 FullCalendar在月视图中定义日程/事件显示结束时间 在默认条件下月视图中日程只显示开始时间如下图所示: 配置 ...

  6. lol服务器维护8月6日,英雄联盟8月6日更新到几点结束 8月6日lol10.16版本更新维护结束时间...

    英雄联盟8月6日更新到几点结束?2020年8月6日英雄联盟停机进行版本更新,本次更新维护后来到10.16版本,新英雄永恩.五款灵魂莲华皮肤等内容将上线.在进行版本更新的时候全区将停机维护,不知道本次停 ...

  7. lol服务器维护8月6日,lol8月6日维护到几点 英雄联盟2020年8月6日10.16版本维护结束时间...

    lol8月6日维护到几点?英雄联盟在2020年8月6日停机更新10.16版本,因此全服进行维护,在lol10.16版本中带来了新英雄永恩.新皮肤灵魂莲华五款,还有大量的英雄改动,待更新维护结束后即可体 ...

  8. Sql 根据当前日期获取当天、周、月、季度、年的开始和结束时间

    declare @NowDT datetime = getdate() select 当前时间=getdate(),今天开始时间 = dateadd(ms, 0,dateadd(day, datedi ...

  9. Java8获取年、月、周数据和某一天的开始结束时间

    在Java8中使用 LocalDate .LocalDateTime.TemporalAdjusters.TemporalField 获取 上周.上个月.去年.本周.本月.今年.下周.下个月.明年的相 ...

  10. 获取年、季度、月份的开始时间和结束时间,到时分秒

    根据传入的日期获取开始时间和结束时间,到时分秒级别 如果不要时分秒,SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd" ...

最新文章

  1. Accurate circular consensus long-read sequencing improves variant detection and assembly of a human
  2. python request post json_python接口之request测试:以json格式发送post请求,.json方法,查看响应结果的情况...
  3. VS2017/2019 F12无法导航到定义
  4. android:background大小,小Demo小知识-android:foreground与android:background
  5. 计算机三级信息管理技术考试大纲
  6. Atitit 图像处理之编程之类库调用的接口api cli gui ws rest  attilax大总结.docx
  7. 为羊哥点赞,利用云服务器搭建私人云笔记
  8. 企业支付宝 异名网银u盾快速充值功能开通方式(支付宝企业商户资金充值方法)
  9. 2022考研资料每日更新(2021.07.28)
  10. class_view_decorate
  11. wifi共享大师开启失败发射功能失败
  12. ExcelToOracle:批量导入Excel文件到Oracle数据库的自动化工具
  13. Flutter 平移动画 — 4种实现方式
  14. java十进制转化为二进制
  15. Pandas数据处理项目----好莱坞评分数据分析
  16. 信号完整性基础04:串扰(2)
  17. 软件测试常问100道面试题(含答案以及案例解析),全网最全最新
  18. JavaScript 计算时间差并格式化输出
  19. ganache私链部署智能合约+本地网络Dapp
  20. 人工智能如何加速共享无人驾驶产业发展?

热门文章

  1. (100)详细描述一个你做过的项目, 面试必问(二十四)(第20天)
  2. (3)FPGA开发语言介绍(第1天)
  3. (77)FPGA时钟激励(always)
  4. 1005打印任务取消不了 hp_惠普HP M1136 MFP多功能打印机 一年半使用感受(学生打印机)...
  5. jquery 逗号分割截取字符串_JS/JQUERY字符串截取分割匹配等处理汇总
  6. Python文件的操作2
  7. mysql嵌套loop循环_mysql游标嵌套循环
  8. 【Pre蓝桥杯嵌入式】如何直接使用LCD例程来作为赛场使用的工程
  9. Linux内核分析 - 网络[二]:网卡驱动接收报文
  10. Particle Filter Tutorial 粒子滤波:从推导到应用(三)