java统计报表日期工具类
生活随笔
收集整理的這篇文章主要介紹了
java统计报表日期工具类
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package com.test.common;import com.coyee.core.util.DateUtil;import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;/*** 時(shí)間工具類*/
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>*//*** 時(shí)間類型*/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;}}}/*** 根據(jù)時(shí)間類型獲取一個(gè)開(kāi)始時(shí)間,一個(gè)結(jié)束時(shí)間* @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;}/*** 獲取當(dāng)天的開(kāi)始時(shí)間* @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();}/*** 獲取當(dāng)天的結(jié)束時(shí)間* @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();}/*** 獲取昨天的開(kāi)始時(shí)間* @return*/public static Date getBeginDayOfYesterday() {Calendar cal = new GregorianCalendar();cal.setTime(getDayBegin());cal.add(Calendar.DAY_OF_MONTH, -1);return cal.getTime();}/*** 獲取昨天的結(jié)束時(shí)間* @return*/public static Date getEndDayOfYesterDay() {Calendar cal = new GregorianCalendar();cal.setTime(getDayEnd());cal.add(Calendar.DAY_OF_MONTH, -1);return cal.getTime();}/*** 獲取明天的開(kāi)始時(shí)間* @return*/public static Date getBeginDayOfTomorrow() {Calendar cal = new GregorianCalendar();cal.setTime(getDayBegin());cal.add(Calendar.DAY_OF_MONTH, 1);return cal.getTime();}/*** 獲取明天的結(jié)束時(shí)間* @return*/public static Date getEndDayOfTomorrow() {Calendar cal = new GregorianCalendar();cal.setTime(getDayEnd());cal.add(Calendar.DAY_OF_MONTH, 1);return cal.getTime();}/*** 獲取本周的開(kāi)始時(shí)間* @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());}/*** 獲取本周的結(jié)束時(shí)間* @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);}/*** 獲取上周的開(kāi)始時(shí)間* @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());}/*** 獲取上周的結(jié)束時(shí)間* @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);}/*** 獲取本月的開(kāi)始時(shí)間* @return*/public static Date getBeginDayOfMonth() {Calendar calendar = Calendar.getInstance();calendar.set(getNowYear(), getNowMonth() - 1, 1);return getDayStartTime(calendar.getTime());}/*** 獲取本月的結(jié)束時(shí)間* @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());}/*** 獲取上月的開(kāi)始時(shí)間* @return*/public static Date getBeginDayOfLastMonth() {Calendar calendar = Calendar.getInstance();calendar.set(getNowYear(), getNowMonth() - 2, 1);return getDayStartTime(calendar.getTime());}/*** 獲取上月的結(jié)束時(shí)間* @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());}/*** 獲取本年的開(kāi)始時(shí)間* @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());}/*** 獲取本年的結(jié)束時(shí)間* @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());}/*** 獲取上一年的開(kāi)始時(shí)間* @return*/public static Date getLastYearStartTime() {Calendar cal = Calendar.getInstance();cal.setTime(getBeginDayOfYear());cal.add(Calendar.YEAR, -1);return cal.getTime();}/*** 獲取某個(gè)日期的開(kāi)始時(shí)間* @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());}/*** 獲取某個(gè)日期的結(jié)束時(shí)間* @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());}/*** 獲取本季度的開(kāi)始時(shí)間* @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();}/*** 獲取本季度結(jié)束時(shí)間* @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;}/*** 兩個(gè)日期相減得到的天數(shù)* @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;}/*** 兩個(gè)日期相減得到的毫秒數(shù)* @param beginDate* @param endDate* @return*/public static long dateDiff(Date beginDate, Date endDate) {long date1ms = beginDate.getTime();long date2ms = endDate.getTime();return date2ms - date1ms;}/*** 獲取兩個(gè)日期中的最大日期* @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;}/*** 獲取兩個(gè)日期中的最小日期* @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;}/*** 返回某月該季度的第一個(gè)月* @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();}/*** 返回某個(gè)日期下幾天的日期* @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();}/*** 返回某個(gè)日期前幾天的日期* @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();}/*** 獲取某年某月到某年某月按天的切片日期集合(間隔天數(shù)的集合)* @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;}/*** 獲取某年某月按天切片日期集合(某個(gè)月間隔多少天的日期集合)* @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;}
}
總結(jié)
以上是生活随笔為你收集整理的java统计报表日期工具类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CE修改器教程总结1
- 下一篇: 错题集03