首页 > 代码库 > DateUtils工具类
DateUtils工具类
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.zuche.framework.utils;
import com.zuche.framework.utils.StringUtils;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DateUtils {
public static final String NORMAL_FORMAT = "yyyy-MM-dd";
public static final String LONG_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final int FOUR = 4;
private static final int FIVE = 5;
private static final int SIX = 6;
private static final int SEVEN = 7;
private static final int EIGHT = 8;
private static final int NINE = 9;
private static final int TEN = 10;
private static final int TWELVE = 12;
private static final int THIRTEEN = 13;
private static final int FOURTEEN = 14;
private static final int TWENTY_FOUR = 24;
private static final int SIXTY = 60;
private static final int ONE_THOUSAND = 1000;
private static final Logger LOGGER = LoggerFactory.getLogger(DateUtils.class);
public DateUtils() {
}
/** @deprecated */
@Deprecated
public static String getCurrentDateToCN(String dateTime) {
String year = dateTime.substring(0, 4);
String day = "";
String month;
if(dateTime.substring(5, 6).equals("0")) {
month = dateTime.substring(6, 7);
} else {
month = dateTime.substring(5, 7);
}
if(dateTime.substring(8, 9).equals("0")) {
day = dateTime.substring(9, 10);
} else {
day = dateTime.substring(8, 10);
}
return year + "年" + month + "月" + day + "日";
}
public static String convertDateStrToChineseDateStr(String dateTime, boolean zeroFlag) {
try {
if(dateTime.length() < 10) {
throw new IllegalArgumentException("日期字符串格式错误!");
} else {
String e = dateTime.substring(0, 4);
String month = dateTime.substring(5, 7);
String day = dateTime.substring(8, 10);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
String result = sdf.format(sdf.parse(e + "年" + month + "月" + day + "日"));
if(!zeroFlag) {
if(result.charAt(8) == 48) {
result = StringUtils.removeCharAt(result, Integer.valueOf(8));
}
if(result.charAt(5) == 48) {
result = StringUtils.removeCharAt(result, Integer.valueOf(5));
}
}
return result;
}
} catch (Exception var7) {
return null;
}
}
public static String convertChineseDateStrToDateStr(String dateTime, Character spliter) {
try {
if(dateTime.length() < 10) {
throw new IllegalArgumentException("中文日期字符串参数错误!");
} else {
SimpleDateFormat e = new SimpleDateFormat("yyyy年MM月dd日");
SimpleDateFormat resultFormat = new SimpleDateFormat("yyyy" + spliter.toString() + "MM" + spliter.toString() + "dd");
return resultFormat.format(e.parse(dateTime));
}
} catch (Exception var4) {
return null;
}
}
public static String convertChineseDateStrToDateStr(String dateTime) {
return convertChineseDateStrToDateStr(dateTime, Character.valueOf(‘-‘));
}
/** @deprecated */
@Deprecated
public static String getCurrentDate2Format() {
Calendar c = Calendar.getInstance();
return "" + c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5);
}
public static String getCurrentDateStr() {
Calendar c = Calendar.getInstance();
return c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5);
}
public static String getNMonthAfterCurrentDay(int n) {
Calendar c = Calendar.getInstance();
c.add(2, n);
return "" + c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5);
}
public static String getNDayBeforeCurrentDate(int n) {
Calendar c = Calendar.getInstance();
c.add(5, -n);
return "" + c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5);
}
public static String getNDayAfterCurrentDate(int n) {
Calendar c = Calendar.getInstance();
c.add(5, n);
return "" + c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5);
}
/** @deprecated */
@Deprecated
public static Calendar switchStringToCalendar(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
public static Calendar convertDateObjectToCalendar(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
public static int countDay(String startTime, String endTime) {
boolean day = false;
Calendar startTimec = Calendar.getInstance();
startTimec.setTime(dateString2Date(startTime));
Calendar endTimec = Calendar.getInstance();
endTimec.setTime(dateString2Date(endTime));
double countMillis = (double)((endTimec.getTimeInMillis() - startTimec.getTimeInMillis()) / 86400000L);
int day1 = (int)Math.ceil(countMillis);
return day1;
}
public static Date dateString2Date(String dateStr) {
return dateString2Date(dateStr, "yyyy-MM-dd");
}
public static Date dateString2Date(String dateStr, String partner) {
try {
SimpleDateFormat e = new SimpleDateFormat(partner);
ParsePosition pos = new ParsePosition(0);
Date current = e.parse(dateStr, pos);
return current;
} catch (NullPointerException var5) {
return null;
}
}
public static Calendar convertDateStrToCalendar(String dateStr) {
return convertDateStrToCalendar(dateStr, "yyyy-MM-dd");
}
public static Calendar convertDateStrToCalendar(String dateStr, String partner) {
try {
SimpleDateFormat e = new SimpleDateFormat(partner);
Date current = e.parse(dateStr);
Calendar c = Calendar.getInstance();
c.setTime(current);
return c;
} catch (Exception var5) {
return null;
}
}
public static int getYearOfDate(Date pDate) {
Calendar c = Calendar.getInstance();
c.setTime(pDate);
return c.get(1);
}
public static int getYearOfDate(String pDate) {
return getYearOfDate((Date)dateString2Date(pDate));
}
public static int getMonthOfDate(Date pDate) {
Calendar c = Calendar.getInstance();
c.setTime(pDate);
return c.get(2) + 1;
}
public static int getMonthOfDate(String date) {
return getMonthOfDate((Date)dateString2Date(date));
}
public static int getDayOfDate(Date pDate) {
Calendar c = Calendar.getInstance();
c.setTime(pDate);
return c.get(5);
}
public static int getHourOfDate(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(11);
}
public static int getMinuteOfDate(Date pDate) {
Calendar c = Calendar.getInstance();
c.setTime(pDate);
return c.get(12);
}
public static int getSecondOfDate(Date pDate) {
Calendar c = Calendar.getInstance();
c.setTime(pDate);
return c.get(13);
}
public static long getMillisOfDate(Date pDate) {
Calendar c = Calendar.getInstance();
c.setTime(pDate);
return c.getTimeInMillis();
}
public static String toStrDateFromUtilDateByFormat(Date pUtilDate, String pFormat) throws ParseException {
String result = "";
if(pUtilDate != null) {
SimpleDateFormat sdf = new SimpleDateFormat(pFormat);
result = sdf.format(pUtilDate);
}
return result;
}
public String addDate(Date startDate, int count, int field, String format) throws ParseException {
int year = getYearOfDate((Date)startDate);
int month = getMonthOfDate((Date)startDate) - 1;
int day = getDayOfDate(startDate);
int hour = getHourOfDate(startDate);
int minute = getMinuteOfDate(startDate);
int second = getSecondOfDate(startDate);
GregorianCalendar calendar = new GregorianCalendar(year, month, day, hour, minute, second);
calendar.add(field, count);
return toStrDateFromUtilDateByFormat(calendar.getTime(), format);
}
public static boolean checkWeekendDay(String date) {
Calendar c = Calendar.getInstance();
c.setTime(dateString2Date(date));
int num = c.get(7);
return num == 6 || num == 7 || num == 1;
}
public static int[][] getMonthsByTime(String startTime, String endTime) {
ArrayList months = new ArrayList();
boolean m = false;
String[] start = startTime.split("-");
String[] end = endTime.split("-");
int year1 = StringUtils.parseInt(start[0]).intValue();
int year2 = StringUtils.parseInt(end[0]).intValue();
int month1 = StringUtils.parseInt(start[1]).intValue();
int month2 = StringUtils.parseInt(end[1]).intValue();
int count = year2 - year1;
int mon = 12;
if(count == 0) {
mon = month2;
}
while(month1 <= mon) {
months.add(year1 + "-" + month1++);
}
int j;
if(count > 1) {
for(int month = 1; month < count; ++month) {
++year1;
for(j = 1; j < 13; ++j) {
months.add(year1 + "-" + j);
}
}
}
if(count > 0) {
++year1;
month1 = 1;
while(month1 <= month2) {
months.add(year1 + "-" + month1++);
}
}
int var15 = months.size();
int[][] var16 = new int[var15][2];
for(j = 0; j < months.size(); ++j) {
String[] date = ((String)months.get(j)).split("-");
var16[j][0] = StringUtils.parseInt(date[0]).intValue();
var16[j][1] = StringUtils.parseInt(date[1]).intValue();
}
return var16;
}
public static int getDateDiff(DateUtils.DateType dateType, String pattern, boolean isRoundMode, String startDate, String... endDate) throws ParseException {
SimpleDateFormat fmt = new SimpleDateFormat(pattern);
Date startDate1 = fmt.parse(startDate);
Calendar starCal = Calendar.getInstance();
starCal.setTime(startDate1);
int sYear = starCal.get(1);
int sMonth = starCal.get(2);
int sDay = starCal.get(5);
Calendar endCal = Calendar.getInstance();
Date endDate1 = null;
int eMonth;
int eDay;
if(endDate.length != 0) {
if(endDate.length > 1) {
throw new RuntimeException("endDate参数只能是0个或者1个");
}
String[] eYear = endDate;
eMonth = endDate.length;
for(eDay = 0; eDay < eMonth; ++eDay) {
String date = eYear[eDay];
endDate1 = fmt.parse(date);
}
} else {
endDate1 = fmt.parse(getCurrentDateStr());
}
endCal.setTime(endDate1);
int var20 = endCal.get(1);
eMonth = endCal.get(2);
eDay = endCal.get(5);
switch(DateUtils.SyntheticClass_1.$SwitchMap$com$zuche$framework$utils$DateUtils$DateType[dateType.ordinal()]) {
case 1:
int year = var20 - sYear;
return year;
case 2:
int month = (var20 - sYear) * 12 + (eMonth - sMonth);
if(isRoundMode) {
month += eDay > sDay?1:0;
} else {
month += eDay >= sDay?0:-1;
}
return month;
case 3:
int day = (int)((endDate1.getTime() - startDate1.getTime()) / 86400000L);
return day;
default:
return 0;
}
}
public static Date dateAdd(Date date, String type, int value) {
Calendar c = Calendar.getInstance();
c.setTime(date);
if(!type.toLowerCase().equals("y") && !type.toLowerCase().equals("year")) {
if(!type.equals("M") && !type.toLowerCase().equals("month")) {
if(!type.toLowerCase().equals("d") && !type.toLowerCase().equals("date")) {
if(!type.toLowerCase().equals("h") && !type.toLowerCase().equals("hour")) {
if(!type.equals("m") && !type.toLowerCase().equals("minute")) {
if(type.toLowerCase().equals("s") || type.toLowerCase().equals("second")) {
c.add(13, value);
}
} else {
c.add(12, value);
}
} else {
c.add(10, value);
}
} else {
c.add(5, value);
}
} else {
c.add(2, value);
}
} else {
c.add(1, value);
}
return c.getTime();
}
public static int getWeekOfDate(String date) {
ParsePosition pos = new ParsePosition(0);
Date parseDate = (new SimpleDateFormat("yyyy-MM-dd")).parse(date, pos);
return parseDate.getDay();
}
public static String dateAddByString(String date, int amount) {
Date d = StringUtils.parseDate(date, "yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(5, amount);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(c.getTime());
}
public static String dateMonthAddByString(String date, int amount) {
Date d = StringUtils.parseDate(date, "yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(2, amount);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(c.getTime());
}
public static String formatDate(Date date) {
return formatDate((Date)date, "yyyy-MM-dd HH:mm:ss");
}
public static String formatDate(Date date, String format) {
return date == null?"":(new SimpleDateFormat(format)).format(date);
}
/** @deprecated */
@Deprecated
public static String formatDate(String date, String format) {
Date d = StringUtils.parseDate(date, format);
return d == null?"":(new SimpleDateFormat(format)).format(d);
}
public static String formatDateStr(String date, String format) {
Date d = StringUtils.parseDate(date, format);
return d == null?"":(new SimpleDateFormat(format)).format(d);
}
public static String getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(1, year);
cal.set(2, month);
cal.set(5, cal.getActualMaximum(5));
return (new SimpleDateFormat("yyyy-MM-dd ")).format(cal.getTime());
}
public static String getLastDayOfMonth(String dateSt) {
int year = getYearOfDate((String)dateSt);
int month = getMonthOfDate((String)dateSt) - 1;
return getLastDayOfMonth(year, month);
}
public static long dateDiffToMinuteD(String startTime, String toTime) {
long diff = 0L;
if(StringUtils.checkEmpty(startTime) != null && StringUtils.checkEmpty(toTime) != null) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
ParsePosition pos = new ParsePosition(0);
Date startTimeD = format.parse(startTime, pos);
pos.setIndex(0);
Date toTimeD = format.parse(toTime, pos);
diff = (startTimeD.getTime() - toTimeD.getTime()) / 1000L / 60L;
}
return diff;
}
public static String getNextDate(String thisDate, int days) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
try {
Date e = df.parse(thisDate);
cal.setTime(e);
} catch (ParseException var5) {
LOGGER.error(var5.getMessage(), var5);
throw new IllegalArgumentException("参数格式异常thisDate:" + thisDate);
}
cal.add(5, days);
return df.format(cal.getTime());
}
public static boolean isValidDate(String sDate) {
String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
if(sDate != null) {
Pattern pattern = Pattern.compile(datePattern1);
Matcher match = pattern.matcher(sDate);
if(match.matches()) {
pattern = Pattern.compile(datePattern2);
match = pattern.matcher(sDate);
return match.matches();
} else {
return false;
}
} else {
return false;
}
}
public static String formatDate(String time) {
String ctime = null;
StringBuilder temp = new StringBuilder();
for(int timeFormat = 0; timeFormat < time.length(); ++timeFormat) {
if(time.charAt(timeFormat) >= 48 && time.charAt(timeFormat) <= 57) {
temp.append(time.charAt(timeFormat));
}
}
if(!StringUtils.isEmpty(temp.toString())) {
StringBuilder var4 = new StringBuilder();
if(temp.length() == 14) {
var4.append(temp.substring(0, 4));
var4.append("-");
var4.append(temp.substring(4, 6));
var4.append("-");
var4.append(temp.substring(6, 8));
var4.append(" ");
var4.append(temp.substring(8, 10));
var4.append(":");
var4.append(temp.substring(10, 12));
var4.append(":");
var4.append(temp.substring(12, 14));
} else if(temp.length() == 8) {
var4.append(temp.substring(0, 4));
var4.append("-");
var4.append(temp.substring(4, 6));
var4.append("-");
var4.append(temp.substring(6, 8));
var4.append(" 00:00:00");
}
ctime = var4.toString();
}
return ctime;
}
public static enum DateType {
year,
month,
day;
private DateType() {
}
}
}
DateUtils工具类