public class DateUtils {
public static Logger log = Logger.getLogger(DateUtils.class);
public static int YEAR = 1;
public static int MONTH = 2;
public static int DATE = 3;
public static final String defaultDateFormat = "yyyy-MM-dd";
public static final String defaultTimeFormat = "yyyy-MM-dd HH:mm:ss";
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat stf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public DateUtils() {
}
public static int getCurrentMinute() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
return calendar.get(12);
}
public static int getCurrentHour() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
return calendar.get(11);
}
public static int getMondayPlus() {
Calendar cd = Calendar.getInstance();
int dayOfWeek = cd.get(7);
return dayOfWeek == 1 ? -6 : 2 - dayOfWeek;
}
public static String getCurrentMonday() {
int mondayPlus = getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
public static String getCurrentSunday() {
int mondayPlus = getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus + 6);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
public static String getCurrentMonth() {
return (new SimpleDateFormat("MM")).format(new Date());
}
public static String getCurrentMonthFirstDate() {
Calendar calendar = Calendar.getInstance();
calendar.set(5, calendar.getActualMinimum(5));
return sdf.format(calendar.getTime());
}
public static String getCurrentMonthFirstDate(String date) throws ParseException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(sdf.parse(date));
calendar.set(5, calendar.getActualMinimum(5));
return sdf.format(calendar.getTime());
}
public static String getCurrentMonthLastDate() {
Calendar calendar = Calendar.getInstance();
calendar.set(5, calendar.getActualMaximum(5));
return sdf.format(calendar.getTime());
}
public static String getCurrentMonthLastDate(String date) throws ParseException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(sdf.parse(date));
calendar.set(5, calendar.getActualMaximum(5));
return sdf.format(calendar.getTime());
}
public static String getCurrentMonthLastWeekendDay(String weekend) {
String lastWeekendDay = "";
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(5, 1);
calendar.add(2, 1);
calendar.add(5, -1);
int endDay = calendar.get(7) - 1;
if (weekend != null && !weekend.equals("")) {
if (weekend.equals("6")) {
calendar.add(5, -endDay - 1);
} else {
calendar.add(5, -endDay);
}
} else {
calendar.add(5, -endDay);
}
lastWeekendDay = String.valueOf(calendar.get(5)).trim();
return lastWeekendDay;
}
public static String getCurrentYear() {
return (new SimpleDateFormat("yyyy")).format(new Date());
}
public static String getCurrentYearFirstDate() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(1);
return year + "-01-01";
}
public static String getCurrentYearLastDate() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(1);
return year + "-12-31";
}
public static String getLastMonthToday() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(2, calendar.get(2) - 1);
return sdf.format(calendar.getTime());
}
public static String getLastMonthEndday() {
String str = "";
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(2, -1);
calendar.set(5, 1);
calendar.roll(5, -1);
str = sdf.format(calendar.getTime());
return str;
}
public static String getLastTwoMonthToday() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(2, calendar.get(2) - 2);
return sdf.format(calendar.getTime());
}
public static String getLastTwoMonthFirstDay() {
String str = "";
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(5, 1);
calendar.add(2, -2);
str = sdf.format(calendar.getTime());
return str;
}
public static String getNextMonthToday() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(2, calendar.get(2) + 1);
return sdf.format(calendar.getTime());
}
public static Date getNextYearToday() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(1, calendar.get(1) + 1);
return calendar.getTime();
}
public static String getLastFewDaysAgo(int days) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int day = calendar.get(6);
calendar.set(6, day - days);
return sdf.format(calendar.getTime());
}
public static String getOffsetData(int type, int value) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
return createOffsetData(calendar, type, value, (String)null);
}
public static String getOffsetData(String fulldate, int type, int value) {
Date date = null;
try {
date = (new SimpleDateFormat(parseDateFormat(fulldate))).parse(fulldate);
} catch (Exception var5) {
log.error("转换时间发生错误!", var5);
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return createOffsetData(calendar, type, value, (String)null);
}
public static String getOffsetData(String fulldate, int type, int value, String reDateFormat) {
Date date = null;
try {
date = (new SimpleDateFormat(parseDateFormat(fulldate))).parse(fulldate);
} catch (Exception var6) {
log.error("转换时间发生错误!", var6);
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return createOffsetData(calendar, type, value, reDateFormat);
}
private static String createOffsetData(Calendar cal, int type, int value, String reDateformat) {
if (type == YEAR) {
cal.add(1, value);
} else if (type == MONTH) {
cal.add(2, value);
} else if (type == DATE) {
cal.add(5, value);
}
return reDateformat != null && !"".equals(reDateformat) ? (new SimpleDateFormat(reDateformat)).format(cal.getTime()) : sdf.format(cal.getTime());
}
public static String getCurrentDateStr() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
return sdf.format(calendar.getTime());
}
public static String getCurrentDateStr(String format) {
if (format == null || format.equals("")) {
format = "yyyy-MM-dd";
}
return (new SimpleDateFormat(format)).format(new Date());
}
public static Date getCurrentDate() {
String date = sdf.format(new Date());
Date date2 = null;
try {
date2 = sdf.parse(date);
} catch (ParseException var3) {
log.error("转换日期时发生错误!", var3);
}
return date2;
}
public static Date getCurrentDate(String format) {
if (format != null && !format.equals("")) {
SimpleDateFormat temp = new SimpleDateFormat(format);
String date2 = temp.format(new Date());
Date date3 = null;
try {
date3 = temp.parse(date2);
} catch (ParseException var5) {
log.error("转换日期时发生错误!", var5);
}
return date3;
} else {
return getCurrentDate();
}
}
public static Date getCurrentTime() {
SimpleDateFormat temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date2 = temp.format(new Date());
Date date3 = null;
try {
date3 = temp.parse(date2);
} catch (ParseException var4) {
log.error("转换时间时发生错误!", var4);
}
return date3;
}
public static String getCurrentTimeStr() {
Date d = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(d);
}
public static Timestamp getCurrentTimestamp() {
long currentTime = System.currentTimeMillis();
Timestamp currentTimestamp = new Timestamp(currentTime);
return currentTimestamp;
}
public static String getCurrentTimestampString() {
long currentTime = System.currentTimeMillis();
String currentTimestampString = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")).format(new Date(currentTime));
return currentTimestampString;
}
public static Date createCurrentDate() {
if (SysConfigUtils.getProperty("oracle8i", "false").equals("true")) {
return transString2Date(getCurrentDateStr());
} else {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
return calendar.getTime();
}
}
public static String transDate2String(Date date) {
return (new SimpleDateFormat("yyyy-MM-dd")).format(date);
}
public static String transDate2String(Date date, String format) {
if (date == null) {
return null;
} else {
return format != null && !"".equals(format) ? (new SimpleDateFormat(format)).format(date) : transDate2String(date);
}
}
public static Timestamp transDate2Timestamp(Date date) {
if (date == null) {
return null;
} else {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = df.format(date);
Timestamp ts = Timestamp.valueOf(time);
return ts;
}
}
public static Date transString2Date(String dateStr) {
if (dateStr != null && !"".equals(dateStr.trim())) {
Date date = null;
String format = parseDateFormat(dateStr);
try {
date = (new SimpleDateFormat(format)).parse(dateStr);
} catch (ParseException var4) {
log.error("String转换成日期类型的时发生错误!", var4);
}
return date;
} else {
return null;
}
}
public static Date transString2Date(String dateStr, String format) {
if (dateStr != null && !"".equals(dateStr.trim())) {
if (format != null && !"".equals(format.trim())) {
Date date = null;
try {
date = (new SimpleDateFormat(format)).parse(dateStr);
} catch (ParseException var4) {
log.error("String转换成日期类型的时发生错误!", var4);
}
return date;
} else {
return transString2Date(dateStr);
}
} else {
return null;
}
}
public static Timestamp transString2Timestamp(String timeString) {
String format = parseDateFormat(timeString);
return transString2Timestamp(timeString, format);
}
public static Timestamp transString2Timestamp(String timeString, String dateFormat) {
SimpleDateFormat format1 = new SimpleDateFormat(dateFormat);
Date d1 = new Date();
try {
d1 = format1.parse(timeString);
} catch (ParseException var5) {
log.error("字符串转日期错误。字符串为:" + timeString, var5);
}
return new Timestamp(d1.getTime());
}
public static String transTimestamp2StringWithFormat(Date date) {
return transTimestamp2String(date, "yyyy-MM-dd HH:mm:ss");
}
public static String transTimestamp2StringNoFormat(Date date) {
return transTimestamp2String(date, "yyyyMMddHHmmss");
}
public static String transTimestamp2String(Timestamp time) {
return time == null ? null : (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")).format(time);
}
private static String transTimestamp2String(Date date, String format) {
return (new SimpleDateFormat(format)).format(date);
}
public static String transTimeStamp2Date(String timestampString, String format) {
if (format == null || "".equals(format)) {
format = "yyyy-MM-dd HH:mm:ss";
}
Long timestamp = Long.parseLong(timestampString) * 1000L;
String date = (new SimpleDateFormat(format)).format(new Date(timestamp.longValue()));
return date;
}
public static String transLongToDateString(long time) {
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
String datestr = sdf.format(date);
return datestr;
}
public static java.sql.Date transUtilDate2SqlDate(Date date, String format) {
return java.sql.Date.valueOf((new SimpleDateFormat(format)).format(date));
}
public static Date transDateFormat(Date totransdate, String format) {
if (totransdate == null) {
return null;
} else {
if (format == null || format.equals("")) {
format = "yyyy-MM-dd";
}
String date = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(totransdate);
Date transdate = null;
try {
transdate = (new SimpleDateFormat(format)).parse(date);
} catch (ParseException var5) {
log.error("日期格式化时发生错误!", var5);
}
return transdate;
}
}
public static String transDateFormat(String totransdate, String format) {
if (totransdate == null) {
return null;
} else {
if (format == null || format.equals("")) {
format = "yyyy-MM-dd";
}
Date date = null;
try {
date = stf.parse(totransdate);
return (new SimpleDateFormat(format)).format(date);
} catch (ParseException var4) {
log.error("日期格式转换时发生错误!", var4);
return null;
}
}
}
public static String transDateFormat(String totransdate, String srcformat, String format) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
try {
SimpleDateFormat tmpformat = new SimpleDateFormat(srcformat);
Date date = tmpformat.parse(totransdate);
String resultString = dateFormat.format(date);
String resultString_ = transDateFormat_(resultString, format, srcformat);
return !totransdate.equalsIgnoreCase(resultString_) ? totransdate + "(日期格式转换失败,使用未转换前日期串)" : resultString;
} catch (Exception var8) {
log.error("转换Date格式时发生错误!", var8);
return totransdate + "(日期格式转换失败,使用未转换前日期串)";
}
}
private static String transDateFormat_(String totransdate, String srcformat, String format) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
try {
SimpleDateFormat tmpformat = new SimpleDateFormat(srcformat);
Date date = tmpformat.parse(totransdate);
String resultString = dateFormat.format(date);
return resultString;
} catch (Exception var7) {
log.error("转换Date格式时发生错误!", var7);
return null;
}
}
public static int countDays(String fromDate, String toDate) {
int days = 0;
try {
String fromDateFormat = parseDateFormat(fromDate);
String toDateFormat = parseDateFormat(toDate);
long l = transString2Date(toDate, fromDateFormat).getTime() - transString2Date(fromDate, toDateFormat).getTime();
days = (int)(l / 60L / 60L / 1000L / 24L);
} catch (Exception var7) {
log.error("计算两个日期相差天数出错", var7);
}
return days;
}
public static int countHours(String fromDate, String toDate) {
int days = 0;
try {
String fromDateFormat = parseDateFormat(fromDate);
String toDateFormat = parseDateFormat(toDate);
long l = transString2Date(toDate, fromDateFormat).getTime() - transString2Date(fromDate, toDateFormat).getTime();
days = (int)(l / 60L / 60L / 1000L);
} catch (Exception var7) {
log.error("计算两个日期相差小时数出错", var7);
}
return days;
}
public static int countMinute(String fromDate, String toDate) {
int minute = 0;
try {
String fromDateFormat = parseDateFormat(fromDate);
String toDateFormat = parseDateFormat(toDate);
long l = (new SimpleDateFormat(fromDateFormat)).parse(toDate).getTime() - (new SimpleDateFormat(toDateFormat)).parse(fromDate).getTime();
minute = (int)(l / 60L / 1000L);
} catch (Exception var7) {
log.error("计算两个日期相差分钟出错", var7);
}
return minute;
}
public static long compare2Seconds(String now, String last) {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date startDate = null;
Date endDate = null;
try {
startDate = inputFormat.parse(now);
endDate = inputFormat.parse(last);
} catch (ParseException var6) {
return 0L;
}
return (startDate.getTime() - endDate.getTime()) / 1000L;
}
public static void main(String[] args) {
long compare2Min = compare2Seconds("2019-03-12 10:25:37", "2019-03-12 10:18:43");
System.out.println(compare2Min);
int compare2Min1 = countSecond("2019-03-12 10:25:37", "2019-03-12 10:18:43");
System.out.println(compare2Min1);
}
public static int countSecond(String fromDate, String toDate) {
int minute = 0;
try {
String fromDateFormat = parseDateFormat(fromDate);
String toDateFormat = parseDateFormat(toDate);
long l = (new SimpleDateFormat(fromDateFormat)).parse(toDate).getTime() - (new SimpleDateFormat(toDateFormat)).parse(fromDate).getTime();
minute = (int)(l / 1000L);
} catch (Exception var7) {
log.error("计算两个日期相差秒数出错", var7);
}
return minute;
}
public static boolean compareDate(String beforeDate, String afterDate) {
try {
String beforeDateFormat = parseDateFormat(beforeDate);
String afterDateFormat = parseDateFormat(afterDate);
return transString2Date(beforeDate, beforeDateFormat).before(transString2Date(afterDate, afterDateFormat));
} catch (Exception var4) {
return false;
}
}
public static boolean compareDate(Date beforeDate, Date afterDate) {
try {
return beforeDate.before(afterDate);
} catch (Exception var3) {
return false;
}
}
public static boolean isGreaterThanToay(String date) {
return getCurrentDateStr().compareTo(date) > 0;
}
public static void executeTime(long startTime) {
long endTime = System.currentTimeMillis();
long exeTime = endTime - startTime;
String starttime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")).format(new Date(startTime));
String endtime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")).format(new Date(endTime));
long minutes = exeTime / 60000L;
long minutes_ = exeTime % 60000L;
long seconds = minutes_ / 1000L;
long millisecond = exeTime - minutes * 60L * 1000L - seconds * 1000L;
log.error("---开始时间" + starttime + "\n结束时间" + endtime + "共执行:" + exeTime + "毫秒,合计" + minutes + "分钟" + seconds + "秒" + millisecond + "毫秒。");
}
public static void executeTime(long startTime, String random) {
long endTime = System.currentTimeMillis();
long exeTime = endTime - startTime;
String starttime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")).format(new Date(startTime));
String endtime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")).format(new Date(endTime));
long minutes = exeTime / 60000L;
long minutes_ = exeTime % 60000L;
long seconds = minutes_ / 1000L;
long millisecond = exeTime - minutes * 60L * 1000L - seconds * 1000L;
log.error(random + "执行文件解析---开始时间" + starttime + " 结束时间" + endtime + "共执行:" + exeTime + "毫秒,合计" + minutes + "分钟" + seconds + "秒" + millisecond + "毫秒。");
}
private static String parseDateFormat(String timeString) {
if (timeString.indexOf("-") != -1) {
if (timeString.length() == 10) {
return "yyyy-MM-dd";
}
if (timeString.length() == 19) {
return "yyyy-MM-dd HH:mm:ss";
}
if (timeString.length() == 16) {
return "yyyy-MM-dd HH:mm";
}
if (timeString.length() == 13) {
return "yyyy-MM-dd HH";
}
}
if (timeString.length() == 8) {
return "yyyyMMdd";
} else {
return timeString.length() == 12 ? "yyyyMMddHHmm" : "yyyyMMddHHmmss";
}
}
}
DateUtils工具类
最新推荐文章于 2024-07-26 12:28:02 发布