com.founder.utils.DateUtils Maven / Gradle / Ivy
The newest version!
package com.founder.util;
import org.apache.commons.lang3.StringUtils;
import java.lang.management.ManagementFactory;
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 str3datetime(String str) {
DateFormat format = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SSSSS);
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;
}
/**
* 得到当前时间延后或前移几天的时间,d为时间,delay为前移或后延的天数
*/
public static Date getDateAgo(int ago) {
return getDateAgo(new Date(),ago);
}
/**
* 得到一个时间延后或前移几天的时间,d为时间,delay为前移或后延的天数
*/
public static Date getDateAgo(Date d, int delay) {
try {
long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
d.setTime(myTime * 1000);
return d;
} catch (Exception e) {
return null;
}
}
public static final String parseDateToStr(final String format, final Date date)
{
return new SimpleDateFormat(format).format(date);
}
/**
* 获取服务器启动时间
*/
public static Date getServerStartDate()
{
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
return new Date(time);
}
/**
* 计算两个时间差
*/
public static String getDatePoor(Date endDate, Date nowDate)
{
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟";
}
public static void main(String[]args){
Date d=getDateAgo(1);
System.out.println(d);
}
}