java统计报表日期工具类

package com.test.common;import com.coyee.core.util.DateUtil;import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;/*** 时间工具类*/
public class DateUtils {/*<option value="today">今天</option><option value="thisWeek">本周</option><option value="thisMonth">本月</option><option value="lastMonth">上月</option><option value="thisSeason">本季</option><option value="thisYear">今年</option><option value="lastYear">上一年</option>*//*** 时间类型*/public enum DateTypeEnum{今天("today"),本周("thisWeek"),本月("thisMonth"),上月("lastMonth"),本季("thisSeason"),今年("thisYear"),上一年("lastYear");private String value;private DateTypeEnum(String value) {this.value = value;}public String value() {return value;}public static DateTypeEnum  initEnum(String value){if(value.equals(DateTypeEnum.今天.value())){return DateTypeEnum.今天;}else if(value.equals(DateTypeEnum.本周.value())){return DateTypeEnum.本周;}else if(value.equals(DateTypeEnum.本月.value())){return DateTypeEnum.本月;}else if(value.equals(DateTypeEnum.上月.value())){return DateTypeEnum.上月;}else if(value.equals(DateTypeEnum.本季.value())){return DateTypeEnum.本季;}else if(value.equals(DateTypeEnum.今年.value())){return DateTypeEnum.今年;}else if(value.equals(DateTypeEnum.上一年.value())){return DateTypeEnum.上一年;}else{return null;}}}/*** 根据时间类型获取一个开始时间,一个结束时间* @param dateTypeEnum* @return*/public static Map<String,Date> getStartAndEndDate(DateTypeEnum dateTypeEnum){Map<String,Date> map = new HashMap<>();if(dateTypeEnum == null){return map;}Date start = null;Date end = null;if(dateTypeEnum.equals(DateTypeEnum.今天)){start = DateUtils.getDayBegin();end = DateUtils.getDayEnd();}else if(dateTypeEnum.equals(DateTypeEnum.本周)){start = DateUtils.getBeginDayOfWeek();end = DateUtils.getEndDayOfWeek();}else if(dateTypeEnum.equals(DateTypeEnum.本月)){start = DateUtils.getBeginDayOfMonth();end = DateUtils.getEndDayOfMonth();}else if(dateTypeEnum.equals(DateTypeEnum.上月)){start = DateUtils.getBeginDayOfLastMonth();end = DateUtils.getEndDayOfLastMonth();}else if(dateTypeEnum.equals(DateTypeEnum.本季)){start = DateUtils.getSeasonDateStartTime();end = DateUtils.getSeasonDateEndTime();}else if(dateTypeEnum.equals(DateTypeEnum.今年)){start = DateUtils.getBeginDayOfYear();end = DateUtils.getEndDayOfYear();}else if(dateTypeEnum.equals(DateTypeEnum.上一年)){start = DateUtils.getLastYearStartTime();end = DateUtils.getBeginDayOfYear();}map.put("start",start);map.put("end",end);return map;}/*** 获取当天的开始时间* @return*/public static java.util.Date getDayBegin() {Calendar cal = new GregorianCalendar();cal.set(Calendar.HOUR_OF_DAY, 0);cal.set(Calendar.MINUTE, 0);cal.set(Calendar.SECOND, 0);cal.set(Calendar.MILLISECOND, 0);return cal.getTime();}/*** 获取当天的结束时间* @return*/public static java.util.Date getDayEnd() {Calendar cal = new GregorianCalendar();cal.set(Calendar.HOUR_OF_DAY, 23);cal.set(Calendar.MINUTE, 59);cal.set(Calendar.SECOND, 59);return cal.getTime();}/*** 获取昨天的开始时间* @return*/public static Date getBeginDayOfYesterday() {Calendar cal = new GregorianCalendar();cal.setTime(getDayBegin());cal.add(Calendar.DAY_OF_MONTH, -1);return cal.getTime();}/*** 获取昨天的结束时间* @return*/public static Date getEndDayOfYesterDay() {Calendar cal = new GregorianCalendar();cal.setTime(getDayEnd());cal.add(Calendar.DAY_OF_MONTH, -1);return cal.getTime();}/*** 获取明天的开始时间* @return*/public static Date getBeginDayOfTomorrow() {Calendar cal = new GregorianCalendar();cal.setTime(getDayBegin());cal.add(Calendar.DAY_OF_MONTH, 1);return cal.getTime();}/*** 获取明天的结束时间* @return*/public static Date getEndDayOfTomorrow() {Calendar cal = new GregorianCalendar();cal.setTime(getDayEnd());cal.add(Calendar.DAY_OF_MONTH, 1);return cal.getTime();}/*** 获取本周的开始时间* @return*/@SuppressWarnings("unused")public static Date getBeginDayOfWeek() {Date date = new Date();if (date == null) {return null;}Calendar cal = Calendar.getInstance();cal.setTime(date);int dayofweek = cal.get(Calendar.DAY_OF_WEEK);if (dayofweek == 1) {dayofweek += 7;}cal.add(Calendar.DATE, 2 - dayofweek);return getDayStartTime(cal.getTime());}/*** 获取本周的结束时间* @return*/public static Date getEndDayOfWeek(){Calendar cal = Calendar.getInstance();cal.setTime(getBeginDayOfWeek());cal.add(Calendar.DAY_OF_WEEK, 6);Date weekEndSta = cal.getTime();return getDayEndTime(weekEndSta);}/*** 获取上周的开始时间* @return*/@SuppressWarnings("unused")public static Date getBeginDayOfLastWeek() {Date date = new Date();if (date == null) {return null;}Calendar cal = Calendar.getInstance();cal.setTime(date);int dayofweek = cal.get(Calendar.DAY_OF_WEEK);if (dayofweek == 1) {dayofweek += 7;}cal.add(Calendar.DATE, 2 - dayofweek - 7);return getDayStartTime(cal.getTime());}/*** 获取上周的结束时间* @return*/public static Date getEndDayOfLastWeek(){Calendar cal = Calendar.getInstance();cal.setTime(getBeginDayOfLastWeek());cal.add(Calendar.DAY_OF_WEEK, 6);Date weekEndSta = cal.getTime();return getDayEndTime(weekEndSta);}/*** 获取本月的开始时间* @return*/public static Date getBeginDayOfMonth() {Calendar calendar = Calendar.getInstance();calendar.set(getNowYear(), getNowMonth() - 1, 1);return getDayStartTime(calendar.getTime());}/*** 获取本月的结束时间* @return*/public static Date getEndDayOfMonth() {Calendar calendar = Calendar.getInstance();calendar.set(getNowYear(), getNowMonth() - 1, 1);int day = calendar.getActualMaximum(5);calendar.set(getNowYear(), getNowMonth() - 1, day);return getDayEndTime(calendar.getTime());}/*** 获取上月的开始时间* @return*/public static Date getBeginDayOfLastMonth() {Calendar calendar = Calendar.getInstance();calendar.set(getNowYear(), getNowMonth() - 2, 1);return getDayStartTime(calendar.getTime());}/*** 获取上月的结束时间* @return*/public static Date getEndDayOfLastMonth() {Calendar calendar = Calendar.getInstance();calendar.set(getNowYear(), getNowMonth() - 2, 1);int day = calendar.getActualMaximum(5);calendar.set(getNowYear(), getNowMonth() - 2, day);return getDayEndTime(calendar.getTime());}/*** 获取本年的开始时间* @return*/public static java.util.Date getBeginDayOfYear() {Calendar cal = Calendar.getInstance();cal.set(Calendar.YEAR, getNowYear());// cal.setcal.set(Calendar.MONTH, Calendar.JANUARY);cal.set(Calendar.DATE, 1);return getDayStartTime(cal.getTime());}/*** 获取本年的结束时间* @return*/public static java.util.Date getEndDayOfYear() {Calendar cal = Calendar.getInstance();cal.set(Calendar.YEAR, getNowYear());cal.set(Calendar.MONTH, Calendar.DECEMBER);cal.set(Calendar.DATE, 31);return getDayEndTime(cal.getTime());}/*** 获取上一年的开始时间* @return*/public static Date getLastYearStartTime() {Calendar cal = Calendar.getInstance();cal.setTime(getBeginDayOfYear());cal.add(Calendar.YEAR, -1);return cal.getTime();}/*** 获取某个日期的开始时间* @param d* @return*/public static Timestamp getDayStartTime(Date d) {Calendar calendar = Calendar.getInstance();if(null != d) calendar.setTime(d);calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);calendar.set(Calendar.MILLISECOND, 0);return new Timestamp(calendar.getTimeInMillis());}/*** 获取某个日期的结束时间* @param d* @return*/public static Timestamp getDayEndTime(Date d) {Calendar calendar = Calendar.getInstance();if(null != d) calendar.setTime(d);calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);calendar.set(Calendar.MILLISECOND, 999);return new Timestamp(calendar.getTimeInMillis());}/*** 获取本季度的开始时间* @return*/public static Date getSeasonDateStartTime() {final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };Calendar cal = Calendar.getInstance();cal.setTime(getBeginDayOfMonth());int sean = SEASON[cal.get(Calendar.MONTH)];cal.set(Calendar.MONTH, sean * 3 - 3);return cal.getTime();}/*** 获取本季度结束时间* @return*/public static Date getSeasonDateEndTime() {final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };Calendar cal = Calendar.getInstance();cal.setTime(getSeasonDateStartTime());cal.add(Calendar.MONTH, 3);cal.add(Calendar.DAY_OF_MONTH, -1);return cal.getTime();}/*** 获取今年是哪一年* @return*/public static Integer getNowYear() {Date date = new Date();GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();gc.setTime(date);return Integer.valueOf(gc.get(1));}/*** 获取本月是哪一月* @return*/public static int getNowMonth() {Date date = new Date();GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();gc.setTime(date);return gc.get(2) + 1;}/*** 两个日期相减得到的天数* @param beginDate* @param endDate* @return*/public static int getDiffDays(Date beginDate, Date endDate) {if (beginDate == null || endDate == null) {throw new IllegalArgumentException("getDiffDays param is null!");}long diff = (endDate.getTime() - beginDate.getTime())/ (1000 * 60 * 60 * 24);int days = new Long(diff).intValue();return days;}/*** 两个日期相减得到的毫秒数* @param beginDate* @param endDate* @return*/public static long dateDiff(Date beginDate, Date endDate) {long date1ms = beginDate.getTime();long date2ms = endDate.getTime();return date2ms - date1ms;}/*** 获取两个日期中的最大日期* @param beginDate* @param endDate* @return*/public static Date max(Date beginDate, Date endDate) {if (beginDate == null) {return endDate;}if (endDate == null) {return beginDate;}if (beginDate.after(endDate)) {return beginDate;}return endDate;}/*** 获取两个日期中的最小日期* @param beginDate* @param endDate* @return*/public static Date min(Date beginDate, Date endDate) {if (beginDate == null) {return endDate;}if (endDate == null) {return beginDate;}if (beginDate.after(endDate)) {return endDate;}return beginDate;}/*** 返回某月该季度的第一个月* @param date* @return*/public static Date getFirstSeasonDate(Date date) {final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };Calendar cal = Calendar.getInstance();cal.setTime(date);int sean = SEASON[cal.get(Calendar.MONTH)];cal.set(Calendar.MONTH, sean * 3 - 3);return cal.getTime();}/*** 返回某个日期下几天的日期* @param date* @param i* @return*/public static Date getNextDay(Date date, int i) {Calendar cal = new GregorianCalendar();cal.setTime(date);cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);return cal.getTime();}/*** 返回某个日期前几天的日期* @param date* @param i* @return*/public static Date getFrontDay(Date date, int i) {Calendar cal = new GregorianCalendar();cal.setTime(date);cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);return cal.getTime();}/*** 获取某年某月到某年某月按天的切片日期集合(间隔天数的集合)* @param beginYear* @param beginMonth* @param endYear* @param endMonth* @param k* @return*/@SuppressWarnings({ "rawtypes", "unchecked" })public static List getTimeList(int beginYear, int beginMonth, int endYear,int endMonth, int k) {List list = new ArrayList();if (beginYear == endYear) {for (int j = beginMonth; j <= endMonth; j++) {list.add(getTimeList(beginYear, j, k));}} else {{for (int j = beginMonth; j < 12; j++) {list.add(getTimeList(beginYear, j, k));}for (int i = beginYear + 1; i < endYear; i++) {for (int j = 0; j < 12; j++) {list.add(getTimeList(i, j, k));}}for (int j = 0; j <= endMonth; j++) {list.add(getTimeList(endYear, j, k));}}}return list;}/*** 获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)* @param beginYear* @param beginMonth* @param k* @return*/@SuppressWarnings({ "unchecked", "rawtypes" })public static List getTimeList(int beginYear, int beginMonth, int k) {List list = new ArrayList();Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);int max = begincal.getActualMaximum(Calendar.DATE);for (int i = 1; i < max; i = i + k) {list.add(begincal.getTime());begincal.add(Calendar.DATE, k);}begincal = new GregorianCalendar(beginYear, beginMonth, max);list.add(begincal.getTime());return list;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/412960.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

springboot mybatis 热加载mapper.xml文件(最简单)

大家好&#xff0c;我是烤鸭: 今天介绍一下springboot mybatis 热加载mapper.xml文件。 本来不打算写的&#xff0c;看到网上比较流行的方式都比较麻烦&#xff0c;想着简化一下。 网上流行的版本。 https://www.cnblogs.com/oskyhg/p/8587701.html 总结一下需要&#xff1a;my…

vue cli vue 3.x

vue cli & vue 3.x https://cli.vuejs.org/dev-guide/ui-api.html#ui-api https://cli.vuejs.org/zh/guide/#cli vue cli & how to select the option in cmd ? vue cli & 选中 option a select all & i select all 1,2,3,4,5,6,7,8,9,0 分别对应 order 转载…

[css] 如果css文件过大时,如何异步加载它?

[css] 如果css文件过大时&#xff0c;如何异步加载它&#xff1f; 分割成多个CSS文件进行Gzip压缩link preload个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录 与歌谣一起通关前端面试题

jenkins svn/git sonarqube scanner 代码集成测试

大家好&#xff0c;我是烤鸭&#xff1a;今天分享一个代码检测工具sonar&#xff0c;在jenkins集成的时候使用。 环境:sonarqube 7.1jenkins 2.12xsonarqube scanner &#xff08;官网最新版3.2.0.1227&#xff09;1. jenkins svn/git 搭建项目https://blog.csdn.net/Angry…

[css] 你有使用过字体图标吗?它有什么好处?

[css] 你有使用过字体图标吗&#xff1f;它有什么好处&#xff1f; 代替图片&#xff0c;可以减少http请求次数&#xff0c;提高页面加载性能。个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录…

Jqgried树形列表

**************************************Jqgrid树列表***************************************function initGrid_test() {//必要字段&#xff1a;id,name,level,parent,isLeaf,expandedvar topicjson{"response": [{"id": "1", "name&qu…

射频与微波测量之S参数

转自&#xff1a;https://www.cnblogs.com/lyh523329053/p/9128577.html S参数 S散射也叫散射参数。是微波传输中的一组重要参数。由于我们很难在高频率时测量电流或电压&#xff0c;因此我们要测量散射参数或 S 参数。这些参数用来表征RF 元件或网络的电气属性或性能&#xff…

JAVA构造对象的几种方式(构建器、构造器)

大家好&#xff0c;我是烤鸭&#xff1a;今天说一下初始化对象的几种方式&#xff1a;1. 多参数构造器2. 构建器3. 构造器后 get/set方法举个例子:这里有个机构entity&#xff0c;提供一个默认构造器 package com.xxx.xxx.modules.sys.entity;/*** 机构Entity* versi…

[css] 请说说你对vh、vw的理解以及它们的运用场景是什么?

[css] 请说说你对vh、vw的理解以及它们的运用场景是什么&#xff1f; vw: 100vw为视窗的宽度&#xff0c;即1vw是视窗宽度的1%vh: 100vh为视窗的高度&#xff0c;即1vh是视窗高度的1%运用场景图片查看大图&#xff1a;img { max-height: 90vh; }代替rem实现移动端布局个人简介 …

Django框架(十二)-- Djang与Ajax

一、什么是Ajax AJAX&#xff08;Asynchronous Javascript And XML&#xff09;翻译成中文就是“异步Javascript和XML”。即使用Javascript语言与服务器进行异步交互&#xff0c;传输的数据为XML&#xff08;当然&#xff0c;传输的数据不只是XML,现在更多使用json数据&#xf…

javascript 将table导出 Excel ,可跨行跨列

原文地址&#xff1a;https://www.cnblogs.com/hailexuexi/p/10795887.html <script language"JavaScript" type"text/javascript">//jQuery HTML导出Excel文件(兼容IE及所有浏览器)function HtmlExportToExcel(tableid,file_name) {var filename fi…

[css] css怎么更改表单的单选框或下拉框的默认样式?

[css] css怎么更改表单的单选框或下拉框的默认样式&#xff1f; 下拉框select可以通过appearance:none去除默认样式&#xff0c;然后进行自定义&#xff0c;但是option标签不能通过CSS自定义&#xff0c;所以最佳方案是自定义标签重写select单选框隐藏input标签&#xff0c;自定…

wampserver 搭建 php环境 运行方法

大家好&#xff0c;我是烤鸭&#xff1a;今天分享的是如何用wamp 运行 php代码。1. wampserver下载&#xff1a;下载地址&#xff1a;https://sourceforge.net/projects/wampserver/files/WampServer%203/WampServer%203.0.0/Addons/Php/wampserver3_x64_addon_php7.2.7.exe…

Mysql数据库查询当前操作的数据库名

查询数据库名&#xff1a; select database()查询表结构&#xff1a; select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME表名 and table_schema (select database())

redis的学习使用,第二章

在IDEA里面使用redis&#xff0c;使用架包 jedis-2.4.2.jar 1 public class Main {2 3 public static void main(String[] args) {4 Jedis jedis new Jedis("127.0.0.1",6379);5 //string类型6 jedis.set("java","aga…

[css] 你了解css3的currentColor吗?举例说明它的作用是什么?

[css] 你了解css3的currentColor吗&#xff1f;举例说明它的作用是什么&#xff1f; currentColor是 color 属性的值&#xff0c;具体意思是指&#xff1a;currentColor关键字的使用值是 color 属性值的计算值。如果currentColor关键字被应用在 color 属性自身&#xff0c;则相…

java php des加密 byte数组16进制 DESTools

大家好&#xff0c;我是烤鸭:今天分享的是java 和 php des 加密。因为接口对接&#xff0c;难免不同语言&#xff0c;加密又是必不可少的。作为接口的提供方&#xff0c;必须把加密规则写好&#xff0c;最好有不同语言的加密demo。1. java版本的des加密解密工具类DESTools.j…

BDD框架之Cucumber研究

BDD框架之Cucumber研究 引用链接&#xff1a;http://kongqingyun123.blog.163.com/blog/static/6377283520134158437813/ Cucumber是BDD&#xff08;行为驱动开发&#xff09;中成熟的一个框架&#xff0c;官方网址: http://cukes.info/1、cucumber安装1、安装ruby2、gem insta…

Oracle修改字段的顺序

一&#xff1a;简单粗暴 1,Oracle: create table CFORM_COULUMN_2 as &#xff08;select 字段A,字段B from CFORM_COULUMN);2,Sqlserver select 字段A,字段B.... into CFORM_COULUMN_2 from CFORM_COULUMN二&#xff1a;通过SYS数据库更新字段顺序 1&#xff0c;查询表…

[css] 怎么去掉点击a链接或者图片出现的边框?

[css] 怎么去掉点击a链接或者图片出现的边框&#xff1f; a{text-decoration:none} img{border:0 none}个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录 与歌谣一起通关前端面试题