123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- package com.fdkankan.tk.util;
- import cn.hutool.core.date.DateTime;
- import cn.hutool.core.date.DateUnit;
- import cn.hutool.core.date.DateUtil;
- import org.apache.commons.lang3.StringUtils;
- import java.text.SimpleDateFormat;
- import java.time.Duration;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- public class Dateutils {
- public static int DAY = Calendar.DAY_OF_MONTH;
- public static int WEEK = Calendar.WEEK_OF_MONTH;
- public static int MONTH = Calendar.MONTH;
- /**
- * 获取指定时间区间的所有数据(包含日期和月份)
- * @param dBegin
- * @param dEnd
- * @param rule 日历规则 如:Calendar.DAY_OF_MONTH
- * @return
- */
- public static List<Date> findDates(Date dBegin, Date dEnd, int rule) {
- List<Date> lDate = new ArrayList<>();
- if (dEnd.before(dBegin)){
- return lDate;
- }
- Calendar calBegin = Calendar.getInstance();
- // 使用给定的 Date 设置此 Calendar 的时间
- calBegin.setTime(dBegin);
- Calendar calEnd = Calendar.getInstance();
- // 使用给定的 Date 设置此 Calendar 的时间
- calEnd.setTime(dEnd);
- // 测试此日期是否在指定日期之后
- while (dEnd.after(calBegin.getTime())) {
- // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
- Date time = calBegin.getTime();
- if(rule == Calendar.WEEK_OF_MONTH){
- time = getMonday(time);
- }
- lDate.add(time);
- calBegin.add(rule, 1);
- }
- return lDate;
- }
- public static List<String> findDatesStr(Date dBegin, Date dEnd, int rule) {
- List<String> datesStr = new ArrayList<>();
- List<Date> dates = findDates(dBegin, dEnd, rule);
- for (Date date : dates) {
- String day ;
- if(rule == MONTH){
- day =getMonthDate(date);
- }else {
- day = getDayDate(date);
- }
- datesStr.add(day);
- }
- return datesStr;
- }
- public static Date getDate(String date) {
- try {
- return new SimpleDateFormat("yyyy-MM-dd").parse(date);
- }catch (Exception e){
- e.printStackTrace();
- }
- return new Date();
- }
- public static Date getDateTime(String date) {
- try {
- return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
- }catch (Exception e){
- e.printStackTrace();
- }
- return new Date();
- }
- public static Date getMonday(Date date) {
- Calendar calBegin = Calendar.getInstance();
- calBegin.setTime(date);
- calBegin.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //周一
- return calBegin.getTime();
- }
- public static String getDate(Date date) {
- return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
- }
- public static String getDateDay(Date date) {
- return new SimpleDateFormat("yyyy-MM-dd").format(date);
- }
- public static String getDateN(Date date) {
- return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
- }
- public static String getMonthDate(Date date) {
- return new SimpleDateFormat("yyyy-MM").format(date);
- }
- public static Date getMonthDate(String date) {
- try {
- return new SimpleDateFormat("yyyy-MM").parse(date);
- }catch (Exception e){
- e.printStackTrace();
- }
- return new Date();
- }
- public static String getDayDate(Date date) {
- return new SimpleDateFormat("yyyy-MM-dd").format(date);
- }
- public static String getDayZeroDate(Date date) {
- return new SimpleDateFormat("yyyy-MM-dd").format(date) +" 00:00:00";
- }
- public static String getHalfYearStr() {
- return new SimpleDateFormat("yyyy-MM-dd").format(getHalfYear())+" 00:00:00";
- }
- public static String getHalfYearStrDay() {
- return new SimpleDateFormat("yyyy-MM-dd").format(getHalfYear());
- }
- public static Date getHalfYear() {
- Calendar c = Calendar.getInstance();
- c.setTime(new Date());
- c.add(Calendar.MONTH, -6);
- return c.getTime();
- }
- public static String getLastMonTh(Date date){
- Calendar ca = Calendar.getInstance();
- ca.setTime(date);
- ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
- return new SimpleDateFormat("yyyy-MM-dd").format(ca.getTime()) +" 23:59:59";
- }
- public static String formatStartTime(String startTime) {
- if(StringUtils.isBlank(startTime)){
- startTime = Dateutils.getHalfYearStr();
- }
- return startTime;
- }
- public static String formatEndTime(String endTime) {
- if(StringUtils.isBlank(endTime)){
- endTime = Dateutils.getDate(new Date());
- }
- return endTime;
- }
- public static String formatStartTimeDay(String startTime) {
- if(StringUtils.isBlank(startTime)){
- return Dateutils.getHalfYearStrDay();
- }
- Date date = getDateTime(startTime);
- return Dateutils.getDateDay(date);
- }
- public static String formatEndTimeDay(String endTime) {
- if(StringUtils.isBlank(endTime)){
- return Dateutils.getDateDay(new Date());
- }
- Date date = getDateTime(endTime);
- return Dateutils.getDateDay(date);
- }
- public static Long getLongTime(Date useStartTime, Date useEndTime,DateUnit dateUnit) {
- if(useStartTime == null || useEndTime == null){
- return 0L;
- }
- return DateUtil.between(useStartTime, useEndTime, dateUnit);
- }
- }
|