获取当前时间的天、周、月、季度、半年度、年度开始和结束时间
- package com.dada.test;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- public class TestDate {
- public static void main(String[] args) {
- System.out.println("当前小时开始:"+getCurrentHourStartTime().toString());
- System.out.println("当前小时结束:"+getCurrentHourEndTime().toString());
- System.out.println("当前天开始:"+getCurrentDayStartTime().toString());
- System.out.println("当前天时结束:"+getCurrentDayEndTime().toString());
- System.out.println("当前周开始:"+getCurrentWeekDayStartTime().toString());
- System.out.println("当前周结束:"+getCurrentWeekDayEndTime().toString());
- System.out.println("当前月开始:"+getCurrentMonthStartTime().toString());
- System.out.println("当前月结束:"+getCurrentMonthEndTime().toString());
- System.out.println("当前季度开始:"+getCurrentQuarterStartTime().toString());
- System.out.println("当前季度结束:"+getCurrentQuarterEndTime().toString());
- System.out.println("当前半年/后半年开始:"+getHalfYearStartTime().toString());
- System.out.println("当前半年/后半年结束:"+getHalfYearEndTime().toString());
- System.out.println("当前年开始:"+getCurrentYearStartTime().toString());
- System.out.println("当前年结束:"+getCurrentYearEndTime().toString());
- }
- /**
- * 获取 当前年、半年、季度、月、日、小时 开始结束时间
- */
- private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
- private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");;
- private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");;
- /**
- * 获得本周的第一天,周一
- *
- * @return
- */
- public static Date getCurrentWeekDayStartTime() {
- Calendar c = Calendar.getInstance();
- try {
- int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
- c.add(Calendar.DATE, -weekday);
- c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return c.getTime();
- }
- /**
- * 获得本周的最后一天,周日
- *
- * @return
- */
- public static Date getCurrentWeekDayEndTime() {
- Calendar c = Calendar.getInstance();
- try {
- int weekday = c.get(Calendar.DAY_OF_WEEK);
- c.add(Calendar.DATE, 8 - weekday);
- c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return c.getTime();
- }
- /**
- * 获得本天的开始时间,即2012-01-01 00:00:00
- *
- * @return
- */
- public static Date getCurrentDayStartTime() {
- Date now = new Date();
- try {
- now = shortSdf.parse(shortSdf.format(now));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return now;
- }
- /**
- * 获得本天的结束时间,即2012-01-01 23:59:59
- *
- * @return
- */
- public static Date getCurrentDayEndTime() {
- Date now = new Date();
- try {
- now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return now;
- }
- /**
- * 获得本小时的开始时间,即2012-01-01 23:59:59
- *
- * @return
- */
- public static Date getCurrentHourStartTime() {
- Date now = new Date();
- try {
- now = longHourSdf.parse(longHourSdf.format(now));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return now;
- }
- /**
- * 获得本小时的结束时间,即2012-01-01 23:59:59
- *
- * @return
- */
- public static Date getCurrentHourEndTime() {
- Date now = new Date();
- try {
- now = longSdf.parse(longHourSdf.format(now) + ":59:59");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return now;
- }
- /**
- * 获得本月的开始时间,即2012-01-01 00:00:00
- *
- * @return
- */
- public static Date getCurrentMonthStartTime() {
- Calendar c = Calendar.getInstance();
- Date now = null;
- try {
- c.set(Calendar.DATE, 1);
- now = shortSdf.parse(shortSdf.format(c.getTime()));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return now;
- }
- /**
- * 当前月的结束时间,即2012-01-31 23:59:59
- *
- * @return
- */
- public static Date getCurrentMonthEndTime() {
- Calendar c = Calendar.getInstance();
- Date now = null;
- try {
- c.set(Calendar.DATE, 1);
- c.add(Calendar.MONTH, 1);
- c.add(Calendar.DATE, -1);
- now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return now;
- }
- /**
- * 当前年的开始时间,即2012-01-01 00:00:00
- *
- * @return
- */
- public static Date getCurrentYearStartTime() {
- Calendar c = Calendar.getInstance();
- Date now = null;
- try {
- c.set(Calendar.MONTH, 0);
- c.set(Calendar.DATE, 1);
- now = shortSdf.parse(shortSdf.format(c.getTime()));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return now;
- }
- /**
- * 当前年的结束时间,即2012-12-31 23:59:59
- *
- * @return
- */
- public static Date getCurrentYearEndTime() {
- Calendar c = Calendar.getInstance();
- Date now = null;
- try {
- c.set(Calendar.MONTH, 11);
- c.set(Calendar.DATE, 31);
- now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return now;
- }
- /**
- * 当前季度的开始时间,即2012-01-1 00:00:00
- *
- * @return
- */
- public static Date getCurrentQuarterStartTime() {
- Calendar c = Calendar.getInstance();
- int currentMonth = c.get(Calendar.MONTH) + 1;
- Date now = null;
- try {
- if (currentMonth >= 1 && currentMonth <= 3)
- c.set(Calendar.MONTH, 0);
- else if (currentMonth >= 4 && currentMonth <= 6)
- c.set(Calendar.MONTH, 3);
- else if (currentMonth >= 7 && currentMonth <= 9)
- c.set(Calendar.MONTH, 4);
- else if (currentMonth >= 10 && currentMonth <= 12)
- c.set(Calendar.MONTH, 9);
- c.set(Calendar.DATE, 1);
- now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return now;
- }
- /**
- * 当前季度的结束时间,即2012-03-31 23:59:59
- *
- * @return
- */
- public static Date getCurrentQuarterEndTime() {
- Calendar c = Calendar.getInstance();
- int currentMonth = c.get(Calendar.MONTH) + 1;
- Date now = null;
- try {
- if (currentMonth >= 1 && currentMonth <= 3) {
- c.set(Calendar.MONTH, 2);
- c.set(Calendar.DATE, 31);
- } else if (currentMonth >= 4 && currentMonth <= 6) {
- c.set(Calendar.MONTH, 5);
- c.set(Calendar.DATE, 30);
- } else if (currentMonth >= 7 && currentMonth <= 9) {
- c.set(Calendar.MONTH, 8);
- c.set(Calendar.DATE, 30);
- } else if (currentMonth >= 10 && currentMonth <= 12) {
- c.set(Calendar.MONTH, 11);
- c.set(Calendar.DATE, 31);
- }
- now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return now;
- }
- /**
- * 获取前/后半年的开始时间
- *
- * @return
- */
- public static Date getHalfYearStartTime() {
- Calendar c = Calendar.getInstance();
- int currentMonth = c.get(Calendar.MONTH) + 1;
- Date now = null;
- try {
- if (currentMonth >= 1 && currentMonth <= 6) {
- c.set(Calendar.MONTH, 0);
- } else if (currentMonth >= 7 && currentMonth <= 12) {
- c.set(Calendar.MONTH, 6);
- }
- c.set(Calendar.DATE, 1);
- now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return now;
- }
- /**
- * 获取前/后半年的结束时间
- *
- * @return
- */
- public static Date getHalfYearEndTime() {
- Calendar c = Calendar.getInstance();
- int currentMonth = c.get(Calendar.MONTH) + 1;
- Date now = null;
- try {
- if (currentMonth >= 1 && currentMonth <= 6) {
- c.set(Calendar.MONTH, 5);
- c.set(Calendar.DATE, 30);
- } else if (currentMonth >= 7 && currentMonth <= 12) {
- c.set(Calendar.MONTH, 11);
- c.set(Calendar.DATE, 31);
- }
- now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return now;
- }
- }
转载于:https://www.cnblogs.com/telwanggs/p/4533252.html
获取当前时间的天、周、月、季度、半年度、年度开始和结束时间相关推荐
- layui时间选择器选择周和季度
layui时间选择器选择周和季度 最近一个后台管理页面数据查询时间间隔要有:日,周,月,季度,年:UI组件我们用的layui,官方dome只有日月年(直接去ctrl+c ctrl+v就好了,在这就不多 ...
- java8 日期范围内 日/周/月/季度/年 的日期结果集
java8 日期范围内 日/周/月/季度/年 的日期结果集 /*** 根据时间范围列出所有日/周/月/季/年** @Author Moqi* @Date 2020/4/30* @Version V1. ...
- mysql分季度统计数据,mysql按日周月季度统计数据
mysql按日周月季度统计数据 mysql按日.周.月.季度统计数据 1.使用DATE_FORMAT做等值条件查询 2.DATE_FORMAT函数语法,参考w3school 3.可使用格式 4.使用示 ...
- 英雄联盟7月23日维修服务器,英雄联盟7月23日更新维护到几点结束 7月23日lol10.15版本更新维护结束时间...
英雄联盟7月23日更新维护到几点结束?英雄联盟在2020年7月23日停机更新10.15版本内容,在lol10.15版本中不仅有新英雄莉莉娅和新系列皮肤灵魂莲华推出,极限闪击模式也回归了.想体验新版本的 ...
- FullCalendar在月视图中定义日程/事件显示结束时间
目录 FullCalendar在月视图中定义日程/事件显示结束时间 配置 换行 FullCalendar在月视图中定义日程/事件显示结束时间 在默认条件下月视图中日程只显示开始时间如下图所示: 配置 ...
- lol服务器维护8月6日,英雄联盟8月6日更新到几点结束 8月6日lol10.16版本更新维护结束时间...
英雄联盟8月6日更新到几点结束?2020年8月6日英雄联盟停机进行版本更新,本次更新维护后来到10.16版本,新英雄永恩.五款灵魂莲华皮肤等内容将上线.在进行版本更新的时候全区将停机维护,不知道本次停 ...
- lol服务器维护8月6日,lol8月6日维护到几点 英雄联盟2020年8月6日10.16版本维护结束时间...
lol8月6日维护到几点?英雄联盟在2020年8月6日停机更新10.16版本,因此全服进行维护,在lol10.16版本中带来了新英雄永恩.新皮肤灵魂莲华五款,还有大量的英雄改动,待更新维护结束后即可体 ...
- Sql 根据当前日期获取当天、周、月、季度、年的开始和结束时间
declare @NowDT datetime = getdate() select 当前时间=getdate(),今天开始时间 = dateadd(ms, 0,dateadd(day, datedi ...
- Java8获取年、月、周数据和某一天的开始结束时间
在Java8中使用 LocalDate .LocalDateTime.TemporalAdjusters.TemporalField 获取 上周.上个月.去年.本周.本月.今年.下周.下个月.明年的相 ...
- 获取年、季度、月份的开始时间和结束时间,到时分秒
根据传入的日期获取开始时间和结束时间,到时分秒级别 如果不要时分秒,SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd" ...
最新文章
- Accurate circular consensus long-read sequencing improves variant detection and assembly of a human
- python request post json_python接口之request测试:以json格式发送post请求,.json方法,查看响应结果的情况...
- VS2017/2019 F12无法导航到定义
- android:background大小,小Demo小知识-android:foreground与android:background
- 计算机三级信息管理技术考试大纲
- Atitit 图像处理之编程之类库调用的接口api cli gui ws rest attilax大总结.docx
- 为羊哥点赞,利用云服务器搭建私人云笔记
- 企业支付宝 异名网银u盾快速充值功能开通方式(支付宝企业商户资金充值方法)
- 2022考研资料每日更新(2021.07.28)
- class_view_decorate
- wifi共享大师开启失败发射功能失败
- ExcelToOracle:批量导入Excel文件到Oracle数据库的自动化工具
- Flutter 平移动画 — 4种实现方式
- java十进制转化为二进制
- Pandas数据处理项目----好莱坞评分数据分析
- 信号完整性基础04:串扰(2)
- 软件测试常问100道面试题(含答案以及案例解析),全网最全最新
- JavaScript 计算时间差并格式化输出
- ganache私链部署智能合约+本地网络Dapp
- 人工智能如何加速共享无人驾驶产业发展?
热门文章
- (100)详细描述一个你做过的项目, 面试必问(二十四)(第20天)
- (3)FPGA开发语言介绍(第1天)
- (77)FPGA时钟激励(always)
- 1005打印任务取消不了 hp_惠普HP M1136 MFP多功能打印机 一年半使用感受(学生打印机)...
- jquery 逗号分割截取字符串_JS/JQUERY字符串截取分割匹配等处理汇总
- Python文件的操作2
- mysql嵌套loop循环_mysql游标嵌套循环
- 【Pre蓝桥杯嵌入式】如何直接使用LCD例程来作为赛场使用的工程
- Linux内核分析 - 网络[二]:网卡驱动接收报文
- Particle Filter Tutorial 粒子滤波:从推导到应用(三)