com.founder.core.utils.DateUtils Maven / Gradle / Ivy
package com.founder.core.utils;
import org.apache.commons.lang3.StringUtils;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
public static final String FORMAT_YYYY_MM = "yyyy-MM-dd";
public static final String FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT_YYYYMMDDHHMMSSSSS = "yyyyMMddhhmmssSSS";
public static final String FORMAT_YYYYMMDDHHMMSS = "yyyyMMddhhmmss";
public static final String FORMAT_YYYY_MM_DD_HH_MM_SSSSS = "yyyy-MM-dd HH:mm:ss.SSS";
public static String getCurrentDate() {
String formatPattern_Short = "yyyyMMddhhmmss";
SimpleDateFormat format = new SimpleDateFormat(formatPattern_Short);
return format.format(new Date());
}
public static String getSeqString() {
SimpleDateFormat fm = new SimpleDateFormat("yyyyMMddHHmmss"); // "yyyyMMdd G
return fm.format(new Date());
}
public static Timestamp getCurrentTimestamp() {
return new Timestamp(System.currentTimeMillis());
}
/**
* 获取当前时间,格式为 yyyyMMddHHmmss
*
* @return
*/
public static String getCurrentTimeStr(String format) {
format = StringUtils.isEmpty(format) ? FORMAT_YYYY_MM_DD_HH_MM_SS : format;
Date now = new Date();
return date2Str(now, format);
}
public static String date2Str(Date date) {
return date2Str(date, FORMAT_YYYY_MM_DD_HH_MM_SS);
}
/**
* 时间格式化FORMAT_YYYY_MM_DD_HH_MM_SS格式
*
* @param date
* @return
*/
public static Date dateFormate(Date date) {
if (date != null) {
return str2datetime(date2Str(date));
} else {
return str2datetime(date2Str(new Date()));
}
}
/**
* 时间转换成 Date 类型
*
* @param date
* @param format
* @return
*/
public static String date2Str(Date date, String format) {
if ((format == null) || format.equals("")) {
format = FORMAT_YYYY_MM_DD_HH_MM_SS;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
if (date != null) {
return sdf.format(date);
}
return "";
}
public static Date str2date(String str) {
DateFormat format = new SimpleDateFormat(FORMAT_YYYY_MM);
Date date = null;
try {
date = format.parse(str);
} catch (Exception e) {
date = null;
}
return date;
}
public static Date str2datetime(String str) {
DateFormat format = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS);
Date date = null;
try {
date = format.parse(str);
} catch (Exception e) {
date = null;
}
return date;
}
public static Date strDate(String str) {
DateFormat format = new SimpleDateFormat(FORMAT_YYYY_MM);
Date date = null;
try {
date = format.parse(str);
} catch (Exception e) {
date = null;
}
return date;
}
public static Date addDay(Date date, int num) {
Calendar sdata = Calendar.getInstance();
sdata.setTime(date);
sdata.add(Calendar.DATE, num);
return sdata.getTime();
}
/**
* 获取批量付款预约时间
*
* @return
*/
public static String getRevTime() {
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 1);
String dateString = new SimpleDateFormat(DateUtils.FORMAT_YYYYMMDDHHMMSS).format(cal.getTime());
System.out.println(dateString);
return dateString;
}
/**
* 时间比较
*
* @param date1
* @param date2
* @return DATE1>DATE2返回1,DATE1 dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
/**
* 把给定的时间减掉给定的分钟数
*
* @param date
* @param minute
* @return
*/
public static Date minusDateByMinute(Date date, int minute) {
Date newDate = new Date(date.getTime() - (minute * 60 * 1000));
return newDate;
}
/**
* 根据身份证计算出生日期
* 只支持18位身份证号
* @param id_no
* @return
*/
public static Date getBirthDay(String id_no){
Date birthDay = null;
if (id_no.length() == 18) {
try {
String str = id_no.substring(6, 14);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, Integer.parseInt(str.substring(0,4)));
calendar.set(Calendar.MONTH, Integer.parseInt(str.substring(4,6)) - 1);
calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(str.substring(6,8)));
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND,0);
birthDay = calendar.getTime();
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
return birthDay;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy