java时间工具类
<div class="cnblogs_Highlighter"><pre class="brush:csharp;gutter:true;">
package com.zmj.sy.modules.platfrom.util;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 获取两个日期字符串之间的所有日期
*/
public class CountDate {
private static transient int gregorianCutoverYear = 1582;
/**
* 闰年中每月天数
*/
private static final int[] DAYS_P_MONTH_LY = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/**
* 非闰年中每月天数
*/
private static final int[] DAYS_P_MONTH_CY = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/**
* 代表数组里的年、月、日
*/
private static final int Y = 0, M = 1, D = 2;
/**
* 将代表日期的字符串分割为代表年月日的整形数组
*
* @param date
* @return
*/
public static Integer[] splitYMD(String date) {
date = date.replace("-", "");
Integer[] ymd = {0, 0, 0};
ymd[Y] = Integer.parseInt(date.substring(0, 4));
ymd[M] = Integer.parseInt(date.substring(4, 6));
ymd[D] = Integer.parseInt(date.substring(6, 8));
return ymd;
}
/**
* 将代表日期的字符串分割为代表年月的整形数组
*
* @param date
* @return
*/
public static Integer[] splitYM(String date) {
date = date.replace("-", "");
Integer[] ym = {0, 0, 0};
ym[Y] = Integer.parseInt(date.substring(0, 4));
ym[M] = Integer.parseInt(date.substring(4, 6));
return ym;
}
/**
* 检查传入的参数代表的年份是否为闰年
*
* @param year
* @return
*/
public static boolean isLeapYear(int year) {
return year >= gregorianCutoverYear ? ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) : (year % 4 == 0);
}
/**
* 日期加1天
*
* @param year
* @param month
* @param day
* @return
*/
private static Integer[] addOneDay(int year, int month, int day) {
if (isLeapYear(year)) {
day++;
if (day > DAYS_P_MONTH_LY[month - 1]) {
month++;
if (month > 12) {
year++;
month = 1;
}
day = 1;
}
} else {
day++;
if (day > DAYS_P_MONTH_CY[month - 1]) {
month++;
if (month > 12) {
year++;
month = 1;
}
day = 1;
}
}
Integer[] ymd = {year, month, day};
return ymd;
}
/**
* 将不足两位的月份或日期补足为两位
*
* @param decimal
* @return
*/
public static String formatMonthDay(int decimal) {
DecimalFormat df = new DecimalFormat("00");
return df.format(decimal);
}
/**
* 将不足四位的年份补足为四位
*
* @param decimal
* @return
*/
public static String formatYear(int decimal) {
DecimalFormat df = new DecimalFormat("0000");
return df.format(decimal);
}
/**
* 计算两个日期之间相隔的天数
*
* @param begin
* @param end
* @return
* @throws ParseException
*/
public static long countDay(String begin, String end) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date beginDate, endDate;
long day = 0;
try {
beginDate = format.parse(begin);
endDate = format.parse(end);
day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
} catch (ParseException e) {
e.printStackTrace();
}
return day;
}
/**
* 计算两个月份 相差多少个月 yyyy-MM
* @param date1
* @param date2
* @return
* @throws ParseException
*/
public static int getMonthSpace(String date1, String date2)
throws ParseException {
int result = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(sdf.parse(date1));
c2.setTime(sdf.parse(date2));
result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH);
return result == 0 ? 1 : Math.abs(result);
}
/**
* 以循环的方式计算日期 格式 YYYY-MM-dd
*
* @param beginDate
* @param endDate
* @return
*/
public static List<String> getEveryday(String beginDate, String endDate) {
long days = countDay(beginDate, endDate);
Integer[] ymd = splitYMD(beginDate);
List<String> everyDays = new ArrayList<String>();
everyDays.add(beginDate);
for (int i = 0; i < days; i++) {
ymd = addOneDay(ymd[Y], ymd[M], ymd[D]);
everyDays.add(formatYear(ymd[Y]) + "-" + formatMonthDay(ymd[M]) + "-" + formatMonthDay(ymd[D]));
}
return everyDays;
}
/**
* 以循环的方式计算日期 格式 YYYYMMdd
*
* @param beginDate
* @param endDate
* @return
*/
public static List<String> getEverydayList(String beginDate, String endDate) {
long days = countDay(beginDate, endDate);
Integer[] ymd = splitYMD(beginDate);
List<String> everyDays = new ArrayList<String>();
everyDays.add(formatYear(ymd[Y]) + formatMonthDay(ymd[M]) + formatMonthDay(ymd[D]));
for (int i = 0; i < days - 1; i++) {
ymd = addOneDay(ymd[Y], ymd[M], ymd[D]);
everyDays.add(formatYear(ymd[Y]) + formatMonthDay(ymd[M]) + formatMonthDay(ymd[D]));
}
return everyDays;
}
/**
* 计算日期内所有月份
*
* @param beginDate
* @param endDate
* @return
*/
public static List<String> getEveryMonth(String beginDate, String endDate) {
Date start = null;
Date end = null;
try {
start = new SimpleDateFormat("yyyy-MM").parse(beginDate);
end = new SimpleDateFormat("yyyy-MM").parse(endDate);
} catch (Exception e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(start);
calendar.add(Calendar.MONTH, 1);
// 获取开始年份和开始月份
int startYear = calendar.get(Calendar.YEAR);
int startMonth = calendar.get(Calendar.MONTH);
// 获取结束年份和结束月份
Calendar cal = Calendar.getInstance();
cal.setTime(end);
cal.add(Calendar.MONTH, 1);
int endYear = cal.get(Calendar.YEAR);
int endMonth = cal.get(Calendar.MONTH);
//
List<String> list = new ArrayList<String>();
for (int i = startYear; i <= endYear; i++) {
String date = "";
if (startYear == endYear) {
for (int j = startMonth; j < endMonth - 1; j++) {
if (j < 9) {
date = i + "0" + (j + 1);
} else {
date = i + "" + (j + 1);
}
list.add(date);
}
} else {
if (i == startYear) {
for (int j = startMonth; j < 11; j++) {
if (j < 9) {
date = i + "0" + (j + 1);
} else {
date = i + "" + (j + 1);
}
list.add(date);
}
} else if (i == endYear) {
for (int j = 0; j < endMonth - 1; j++) {
if (j < 9) {
date = i + "0" + (j + 1);
} else {
date = i + "" + (j + 1);
}
list.add(date);
}
} else {
for (int j = 0; j < 11; j++) {
if (j < 9) {
date = i + "0" + (j + 1);
} else {
date = i + "" + (j + 1);
}
list.add(date);
}
}
}
}
return list;
}
/**
* @param time 支持 2017-01的时间字符串格式
* @return
*/
public static String[] getLast12Months(String time) {
//处理月份输入条件
if (time.length() == 7) {
time = time + "-01 06:00:00";
} else if (time.length() == 110) {
time = time.substring(0, 7) + "-01 06:00:00";
}
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
date = sdf.parse(time);
} catch (Exception e) {
return null;
}
String[] last12Months = new String[12];
Calendar cal = Calendar.getInstance();
//设置输入条件时间
cal.setTime(date);
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1); //要先+1,才能把本月的算进去
for (int i = 0; i < 12; i++) {
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1); //逐次往前推1个月
last12Months[11 - i] = cal.get(Calendar.YEAR) + addZeroForNum(String.valueOf(cal.get(Calendar.MONTH) + 1), 2);
}
return last12Months;
}
public static String addZeroForNum(String str, int strLength) {
int strLen = str.length();
if (strLen < strLength) {
while (strLen < strLength) {
StringBuffer sb = new StringBuffer();
sb.append("0").append(str);// 左补0
// sb.append(str).append("0");//右补0
str = sb.toString();
strLen = str.length();
}
}
return str;
}
/**
* 获取当前时间往前往后n的日期
* eg: date ==null 默认是系统当前时间 否则以date为时间起点
* n > 0 往后
* n = 0 当前时间
* n < 0 往前
*
* @param date
* @param n
* @return
* @author zhangheng5@lenovo.com
*/
public static List<String> getBeforeDate(String date, int n) {
DateTime dateTime = DateUtil.parseDate(date);
List<String> list = new ArrayList<>();
Calendar c = Calendar.getInstance();
Date today = new Date();
if (null != dateTime) {
today = dateTime;
}
c.setTime(today);
if (n > 0) {
for (int i = 0; i <= n; i++) {
c.add(Calendar.DATE, i - 1);
list.add(new SimpleDateFormat("yyyyMMdd").format(c.getTime()));
c.setTime(today);
}
} else if (n < 0) {
for (int i = n; i <= 0; i++) {
c.add(Calendar.DATE, i);
list.add(new SimpleDateFormat("yyyyMMdd").format(c.getTime()));
c.setTime(today);
}
} else {
c.add(Calendar.DATE, 1);
list.add(new SimpleDateFormat("yyyyMMdd").format(c.getTime()));
c.setTime(today);
}
return list;
}
/**
* 拆分年月日
* YEARN年
* MONTH月
* DAYANDMONTH 带日的或者不带日的月分
*
* @return
*/
public static int splitDate(String dateType, String oldDate) {
int i = 0;
if (StringUtils.equals(dateType, "YEAR")) {
i = NumberUtil.parseInt(oldDate.substring(0, 4));
}
if (StringUtils.equals(dateType, "MONTH")) {
String substring = oldDate.substring(oldDate.length() - 4);
i = NumberUtil.parseInt(substring.substring(0, 2));
}
if (StringUtils.equals(dateType, "DAYANDMONTH")) {
i = NumberUtil.parseInt(oldDate.substring(oldDate.length() - 2));
}
return i;
}
/* public static void main(String[] args) {
List<String> list = CountDate.getEveryday("2020-08-29", "2022-09-02");
for (String result : list) {
System.out.println(result);
}
}*/
/**
* 校验时间格式
*
* @param type
* @param timeStamp
* @return
*/
public static boolean checkDateFormat(String type, String timeStamp) {
SimpleDateFormat format;
boolean dateflag = true;
if (StringUtils.equals(type, "MONTH")) {
format = new SimpleDateFormat("yyyy-MM");
} else {
format = new SimpleDateFormat("yyyy-MM-dd");
}
try {
format.setLenient(false);
format.parse(timeStamp.intern());
} catch (Exception e) {
dateflag = false;
}
if (!dateflag || type.equals("MONTH") ? timeStamp.length() > 7 : timeStamp.length() > 10) {
dateflag = false;
}
return dateflag;
}
/**
* 获取传入时间的 起点时间 和终点时间
*
* @param MONTH
* @return
*/
public static Map<String, Object> getDateDifference(String MONTH) {
String newMonth = MONTH.substring(0, 4) + "-" + MONTH.substring(MONTH.length() - 2);
Map<String, Object> resuleMap = new HashMap<>();
//指定时间往前
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Calendar oldCal = Calendar.getInstance();
String endTime, startTime;
if (StringUtils.isBlank(newMonth)) {
if (1 == cal.get(Calendar.DAY_OF_MONTH)) { //第一天展示上月整月数据
endTime = sdf.format(cal.getTime()) + "-01 06:00:00";
oldCal.setTime(date);
oldCal.add(Calendar.MONTH, -1);
startTime = sdf.format(oldCal.getTime()) + "-01 06:00:00";
} else {
//当前月
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
endTime = format.format(cal.getTime()) + " 06:00:00";
oldCal.setTime(date);
startTime = sdf.format(oldCal.getTime()) + "-01 06:00:00";
}
} else {
startTime = newMonth + "-01 06:00:00";
String[] str = newMonth.split("-");
if (Integer.valueOf(str[1]) == 12) {
endTime = (Integer.valueOf(str[0]) + 1) + "-01-01 06:00:00";
} else {
endTime = str[0] + "-" + (Integer.valueOf(str[1]) + 1) + "-01 06:00:00";
}
}
resuleMap.put("startTime", startTime);
resuleMap.put("endTime", endTime);
return resuleMap;
}
/**
* 获取当前天数时间差
*
* @return
*/
public static long getDateTimesDay(String newMonth) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String year = newMonth.substring(0, 4);
String month = newMonth.substring(newMonth.length() - 2);
String cDate = year + "-" + month;
DateTime date = DateUtil.date();
String olddater = date.toString().substring(0, 7);
if (cDate.equals(olddater)) {
Date startDate = format.parse(cDate + "-01 06:00:00");
Date endDate = format.parse(date.toString().substring(0, 10) + " 06:00:00");
long count = DateUtil.between(startDate, endDate, DateUnit.DAY);
return count;
} else {
return 30;
}
}
/**
* @param time 第几天
* @param
* @return java.lang.String
* @description:获取当前日期前 多少 天
* @author: yc
* @date: 2022-08-11 13:30:49
*/
public static List<String> getLastDays(String dateTime, int time) {
String[] strings = dateTime.split("-");
List<String> frontDaysList = new ArrayList<String>();
for (int i = 0; i < time; i++) {
Calendar calendar = Calendar.getInstance();
calendar.set(NumberUtils.toInt(strings[0]),NumberUtils.toInt(strings[1])-1,NumberUtils.toInt(strings[2]));
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - i);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
frontDaysList.add(format.format(today));
}
Collections.reverse(frontDaysList);
return frontDaysList;
}
/**
* 获取指定时间 前 多少 月的日期集合
* @param dateTime
* @param size
* @return
*/
public static List<String> getLastMonths(String dateTime, int size) {
String[] strings = dateTime.split("-");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar c = Calendar.getInstance();
c.set(NumberUtils.toInt(strings[0]), NumberUtils.toInt(strings[1]), 1);
List<String> list = new ArrayList(size);
for (int i = 0; i < size; i++) {
//c.setTime(new Date());
c.add(Calendar.MONTH, -1);
Date m = c.getTime();
list.add(sdf.format(m));
}
Collections.reverse(list);
return list;
}
public static void main(String[] args) {
String newMonth = "202302";
String year = newMonth.substring(0, 4);
String month = newMonth.substring(newMonth.length() - 2);
String s = year + "-" + month;
DateTime date = DateUtil.date();
String olddater = date.toString().substring(0, 7);
System.out.println(olddater + s);
}
}
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 获取两个日期字符串之间的所有日期
*/
public class CountDate {
private static transient int gregorianCutoverYear = 1582;
/**
* 闰年中每月天数
*/
private static final int[] DAYS_P_MONTH_LY = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/**
* 非闰年中每月天数
*/
private static final int[] DAYS_P_MONTH_CY = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/**
* 代表数组里的年、月、日
*/
private static final int Y = 0, M = 1, D = 2;
/**
* 将代表日期的字符串分割为代表年月日的整形数组
*
* @param date
* @return
*/
public static Integer[] splitYMD(String date) {
date = date.replace("-", "");
Integer[] ymd = {0, 0, 0};
ymd[Y] = Integer.parseInt(date.substring(0, 4));
ymd[M] = Integer.parseInt(date.substring(4, 6));
ymd[D] = Integer.parseInt(date.substring(6, 8));
return ymd;
}
/**
* 将代表日期的字符串分割为代表年月的整形数组
*
* @param date
* @return
*/
public static Integer[] splitYM(String date) {
date = date.replace("-", "");
Integer[] ym = {0, 0, 0};
ym[Y] = Integer.parseInt(date.substring(0, 4));
ym[M] = Integer.parseInt(date.substring(4, 6));
return ym;
}
/**
* 检查传入的参数代表的年份是否为闰年
*
* @param year
* @return
*/
public static boolean isLeapYear(int year) {
return year >= gregorianCutoverYear ? ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) : (year % 4 == 0);
}
/**
* 日期加1天
*
* @param year
* @param month
* @param day
* @return
*/
private static Integer[] addOneDay(int year, int month, int day) {
if (isLeapYear(year)) {
day++;
if (day > DAYS_P_MONTH_LY[month - 1]) {
month++;
if (month > 12) {
year++;
month = 1;
}
day = 1;
}
} else {
day++;
if (day > DAYS_P_MONTH_CY[month - 1]) {
month++;
if (month > 12) {
year++;
month = 1;
}
day = 1;
}
}
Integer[] ymd = {year, month, day};
return ymd;
}
/**
* 将不足两位的月份或日期补足为两位
*
* @param decimal
* @return
*/
public static String formatMonthDay(int decimal) {
DecimalFormat df = new DecimalFormat("00");
return df.format(decimal);
}
/**
* 将不足四位的年份补足为四位
*
* @param decimal
* @return
*/
public static String formatYear(int decimal) {
DecimalFormat df = new DecimalFormat("0000");
return df.format(decimal);
}
/**
* 计算两个日期之间相隔的天数
*
* @param begin
* @param end
* @return
* @throws ParseException
*/
public static long countDay(String begin, String end) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date beginDate, endDate;
long day = 0;
try {
beginDate = format.parse(begin);
endDate = format.parse(end);
day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
} catch (ParseException e) {
e.printStackTrace();
}
return day;
}
/**
* 计算两个月份 相差多少个月 yyyy-MM
* @param date1
* @param date2
* @return
* @throws ParseException
*/
public static int getMonthSpace(String date1, String date2)
throws ParseException {
int result = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(sdf.parse(date1));
c2.setTime(sdf.parse(date2));
result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH);
return result == 0 ? 1 : Math.abs(result);
}
/**
* 以循环的方式计算日期 格式 YYYY-MM-dd
*
* @param beginDate
* @param endDate
* @return
*/
public static List<String> getEveryday(String beginDate, String endDate) {
long days = countDay(beginDate, endDate);
Integer[] ymd = splitYMD(beginDate);
List<String> everyDays = new ArrayList<String>();
everyDays.add(beginDate);
for (int i = 0; i < days; i++) {
ymd = addOneDay(ymd[Y], ymd[M], ymd[D]);
everyDays.add(formatYear(ymd[Y]) + "-" + formatMonthDay(ymd[M]) + "-" + formatMonthDay(ymd[D]));
}
return everyDays;
}
/**
* 以循环的方式计算日期 格式 YYYYMMdd
*
* @param beginDate
* @param endDate
* @return
*/
public static List<String> getEverydayList(String beginDate, String endDate) {
long days = countDay(beginDate, endDate);
Integer[] ymd = splitYMD(beginDate);
List<String> everyDays = new ArrayList<String>();
everyDays.add(formatYear(ymd[Y]) + formatMonthDay(ymd[M]) + formatMonthDay(ymd[D]));
for (int i = 0; i < days - 1; i++) {
ymd = addOneDay(ymd[Y], ymd[M], ymd[D]);
everyDays.add(formatYear(ymd[Y]) + formatMonthDay(ymd[M]) + formatMonthDay(ymd[D]));
}
return everyDays;
}
/**
* 计算日期内所有月份
*
* @param beginDate
* @param endDate
* @return
*/
public static List<String> getEveryMonth(String beginDate, String endDate) {
Date start = null;
Date end = null;
try {
start = new SimpleDateFormat("yyyy-MM").parse(beginDate);
end = new SimpleDateFormat("yyyy-MM").parse(endDate);
} catch (Exception e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(start);
calendar.add(Calendar.MONTH, 1);
// 获取开始年份和开始月份
int startYear = calendar.get(Calendar.YEAR);
int startMonth = calendar.get(Calendar.MONTH);
// 获取结束年份和结束月份
Calendar cal = Calendar.getInstance();
cal.setTime(end);
cal.add(Calendar.MONTH, 1);
int endYear = cal.get(Calendar.YEAR);
int endMonth = cal.get(Calendar.MONTH);
//
List<String> list = new ArrayList<String>();
for (int i = startYear; i <= endYear; i++) {
String date = "";
if (startYear == endYear) {
for (int j = startMonth; j < endMonth - 1; j++) {
if (j < 9) {
date = i + "0" + (j + 1);
} else {
date = i + "" + (j + 1);
}
list.add(date);
}
} else {
if (i == startYear) {
for (int j = startMonth; j < 11; j++) {
if (j < 9) {
date = i + "0" + (j + 1);
} else {
date = i + "" + (j + 1);
}
list.add(date);
}
} else if (i == endYear) {
for (int j = 0; j < endMonth - 1; j++) {
if (j < 9) {
date = i + "0" + (j + 1);
} else {
date = i + "" + (j + 1);
}
list.add(date);
}
} else {
for (int j = 0; j < 11; j++) {
if (j < 9) {
date = i + "0" + (j + 1);
} else {
date = i + "" + (j + 1);
}
list.add(date);
}
}
}
}
return list;
}
/**
* @param time 支持 2017-01的时间字符串格式
* @return
*/
public static String[] getLast12Months(String time) {
//处理月份输入条件
if (time.length() == 7) {
time = time + "-01 06:00:00";
} else if (time.length() == 110) {
time = time.substring(0, 7) + "-01 06:00:00";
}
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
date = sdf.parse(time);
} catch (Exception e) {
return null;
}
String[] last12Months = new String[12];
Calendar cal = Calendar.getInstance();
//设置输入条件时间
cal.setTime(date);
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1); //要先+1,才能把本月的算进去
for (int i = 0; i < 12; i++) {
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1); //逐次往前推1个月
last12Months[11 - i] = cal.get(Calendar.YEAR) + addZeroForNum(String.valueOf(cal.get(Calendar.MONTH) + 1), 2);
}
return last12Months;
}
public static String addZeroForNum(String str, int strLength) {
int strLen = str.length();
if (strLen < strLength) {
while (strLen < strLength) {
StringBuffer sb = new StringBuffer();
sb.append("0").append(str);// 左补0
// sb.append(str).append("0");//右补0
str = sb.toString();
strLen = str.length();
}
}
return str;
}
/**
* 获取当前时间往前往后n的日期
* eg: date ==null 默认是系统当前时间 否则以date为时间起点
* n > 0 往后
* n = 0 当前时间
* n < 0 往前
*
* @param date
* @param n
* @return
* @author zhangheng5@lenovo.com
*/
public static List<String> getBeforeDate(String date, int n) {
DateTime dateTime = DateUtil.parseDate(date);
List<String> list = new ArrayList<>();
Calendar c = Calendar.getInstance();
Date today = new Date();
if (null != dateTime) {
today = dateTime;
}
c.setTime(today);
if (n > 0) {
for (int i = 0; i <= n; i++) {
c.add(Calendar.DATE, i - 1);
list.add(new SimpleDateFormat("yyyyMMdd").format(c.getTime()));
c.setTime(today);
}
} else if (n < 0) {
for (int i = n; i <= 0; i++) {
c.add(Calendar.DATE, i);
list.add(new SimpleDateFormat("yyyyMMdd").format(c.getTime()));
c.setTime(today);
}
} else {
c.add(Calendar.DATE, 1);
list.add(new SimpleDateFormat("yyyyMMdd").format(c.getTime()));
c.setTime(today);
}
return list;
}
/**
* 拆分年月日
* YEARN年
* MONTH月
* DAYANDMONTH 带日的或者不带日的月分
*
* @return
*/
public static int splitDate(String dateType, String oldDate) {
int i = 0;
if (StringUtils.equals(dateType, "YEAR")) {
i = NumberUtil.parseInt(oldDate.substring(0, 4));
}
if (StringUtils.equals(dateType, "MONTH")) {
String substring = oldDate.substring(oldDate.length() - 4);
i = NumberUtil.parseInt(substring.substring(0, 2));
}
if (StringUtils.equals(dateType, "DAYANDMONTH")) {
i = NumberUtil.parseInt(oldDate.substring(oldDate.length() - 2));
}
return i;
}
/* public static void main(String[] args) {
List<String> list = CountDate.getEveryday("2020-08-29", "2022-09-02");
for (String result : list) {
System.out.println(result);
}
}*/
/**
* 校验时间格式
*
* @param type
* @param timeStamp
* @return
*/
public static boolean checkDateFormat(String type, String timeStamp) {
SimpleDateFormat format;
boolean dateflag = true;
if (StringUtils.equals(type, "MONTH")) {
format = new SimpleDateFormat("yyyy-MM");
} else {
format = new SimpleDateFormat("yyyy-MM-dd");
}
try {
format.setLenient(false);
format.parse(timeStamp.intern());
} catch (Exception e) {
dateflag = false;
}
if (!dateflag || type.equals("MONTH") ? timeStamp.length() > 7 : timeStamp.length() > 10) {
dateflag = false;
}
return dateflag;
}
/**
* 获取传入时间的 起点时间 和终点时间
*
* @param MONTH
* @return
*/
public static Map<String, Object> getDateDifference(String MONTH) {
String newMonth = MONTH.substring(0, 4) + "-" + MONTH.substring(MONTH.length() - 2);
Map<String, Object> resuleMap = new HashMap<>();
//指定时间往前
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Calendar oldCal = Calendar.getInstance();
String endTime, startTime;
if (StringUtils.isBlank(newMonth)) {
if (1 == cal.get(Calendar.DAY_OF_MONTH)) { //第一天展示上月整月数据
endTime = sdf.format(cal.getTime()) + "-01 06:00:00";
oldCal.setTime(date);
oldCal.add(Calendar.MONTH, -1);
startTime = sdf.format(oldCal.getTime()) + "-01 06:00:00";
} else {
//当前月
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
endTime = format.format(cal.getTime()) + " 06:00:00";
oldCal.setTime(date);
startTime = sdf.format(oldCal.getTime()) + "-01 06:00:00";
}
} else {
startTime = newMonth + "-01 06:00:00";
String[] str = newMonth.split("-");
if (Integer.valueOf(str[1]) == 12) {
endTime = (Integer.valueOf(str[0]) + 1) + "-01-01 06:00:00";
} else {
endTime = str[0] + "-" + (Integer.valueOf(str[1]) + 1) + "-01 06:00:00";
}
}
resuleMap.put("startTime", startTime);
resuleMap.put("endTime", endTime);
return resuleMap;
}
/**
* 获取当前天数时间差
*
* @return
*/
public static long getDateTimesDay(String newMonth) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String year = newMonth.substring(0, 4);
String month = newMonth.substring(newMonth.length() - 2);
String cDate = year + "-" + month;
DateTime date = DateUtil.date();
String olddater = date.toString().substring(0, 7);
if (cDate.equals(olddater)) {
Date startDate = format.parse(cDate + "-01 06:00:00");
Date endDate = format.parse(date.toString().substring(0, 10) + " 06:00:00");
long count = DateUtil.between(startDate, endDate, DateUnit.DAY);
return count;
} else {
return 30;
}
}
/**
* @param time 第几天
* @param
* @return java.lang.String
* @description:获取当前日期前 多少 天
* @author: yc
* @date: 2022-08-11 13:30:49
*/
public static List<String> getLastDays(String dateTime, int time) {
String[] strings = dateTime.split("-");
List<String> frontDaysList = new ArrayList<String>();
for (int i = 0; i < time; i++) {
Calendar calendar = Calendar.getInstance();
calendar.set(NumberUtils.toInt(strings[0]),NumberUtils.toInt(strings[1])-1,NumberUtils.toInt(strings[2]));
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - i);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
frontDaysList.add(format.format(today));
}
Collections.reverse(frontDaysList);
return frontDaysList;
}
/**
* 获取指定时间 前 多少 月的日期集合
* @param dateTime
* @param size
* @return
*/
public static List<String> getLastMonths(String dateTime, int size) {
String[] strings = dateTime.split("-");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar c = Calendar.getInstance();
c.set(NumberUtils.toInt(strings[0]), NumberUtils.toInt(strings[1]), 1);
List<String> list = new ArrayList(size);
for (int i = 0; i < size; i++) {
//c.setTime(new Date());
c.add(Calendar.MONTH, -1);
Date m = c.getTime();
list.add(sdf.format(m));
}
Collections.reverse(list);
return list;
}
public static void main(String[] args) {
String newMonth = "202302";
String year = newMonth.substring(0, 4);
String month = newMonth.substring(newMonth.length() - 2);
String s = year + "-" + month;
DateTime date = DateUtil.date();
String olddater = date.toString().substring(0, 7);
System.out.println(olddater + s);
}
}
</pre></div>