赞
踩
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
public static String YYYY = "yyyy";
public static String YYYY_MM = "yyyy-MM";
public static String YYYY_MM_DD = "yyyy-MM-dd";
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private static String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
/**
* 将字符串形式的日期值转化为Date,格式:yyyy-MM-dd HH:mm:ss
* @param dateStr 日期字符串
* @return Date
*/
public static Date parseDateTime(String dateStr) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d = sdf.parse(dateStr);
return d;
} catch (ParseException e) {
throw new DateParseException(e.getMessage(), e.getCause());
}
}
/**
* 根据指定的格式将日期转化为Date
* @param dateStr 日期字符串
* @param format 格式字符串
* @return Date
*/
public static Date parseDateTime(String dateStr, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date d = sdf.parse(dateStr);
return d;
} catch (ParseException e) {
throw new DateParseException(e.getMessage(), e.getCause());
}
}
public static Date parseDateTime(Long time, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
String str = sdf.format(time);
Date d = sdf.parse(str);
return d;
} catch (ParseException e) {
throw new DateParseException(e.getMessage(), e.getCause());
}
}
/**
* 将日期型按照指定的格式转换为字符串
* @param date 需要转换的日期
* @param format 指定的转换格式,如果为null,则默认为<code>yyyy-MM-dd HH:mm:ss</code>
* @return 日期的字符串格式
*/
public static String formatDate(Date date, String format) {
String _format = format;
if(_format == null || "".equals(_format)) {
_format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(_format);
return sdf.format(date);
}
/**
* 获取当前日期
* @return Date
*/
public static Date getNow(){
return new Date(System.currentTimeMillis());
}
/**
* 获取一个日期和当前日期相差的天数
*
* @param theDate
* @return int
*/
public static int daysBetween(Date preDate, Date theDate) throws ParseException {
long nowTime = theDate.getTime();
long theTime = preDate.getTime();
long between_days= (nowTime-theTime) /(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 获取一个日期和当前日期相差的小时(向上取整)
*
* @param theDate
* @return int
*/
public static int hoursBetween(Date preDate, Date theDate) throws ParseException {
long nowTime = theDate.getTime();
long theTime = preDate.getTime();
long between_hour= 0;
if((nowTime-theTime)%(1000*3600)>0){
between_hour= (nowTime-theTime) /(1000*3600)+1;
}else {
between_hour= (nowTime-theTime) /(1000*3600);
}
return Integer.parseInt(String.valueOf(between_hour));
}
/****
* 传入具体日期 ,返回具体日期减少一天
* @param date 日期(2017-04-13)
* @return 2017-04-12
* @throws ParseException
*/
public static String subDay(String date) {
SimpleDateFormat sdf = new SimpleDateFormat(date);
Date dt = new Date();//获取当前时间
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.DAY_OF_MONTH, -1);
Date dt1 = rightNow.getTime();
String reStr = sdf.format(dt1);
return reStr;
}
/**
* 获取当前Date型日期
*
* @return Date() 当前日期
*/
public static Date getNowDate() {
return new Date();
}
/**
* 获取当前日期, 默认格式为yyyy-MM-dd
*
* @return String
*/
public static String getDate() {
return dateTimeNow(YYYY_MM_DD);
}
public static final String getTime() {
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
}
public static final String dateTimeNow() {
return dateTimeNow(YYYYMMDDHHMMSS);
}
public static final String dateTimeNow(final String format) {
return parseDateToStr(format, new Date());
}
public static final String dateTime(final Date date) {
return parseDateToStr(YYYY_MM_DD, date);
}
public static final String parseDateToStr(final String format, final Date date) {
return new SimpleDateFormat(format).format(date);
}
public static final Date dateTime(final String format, final String ts) {
try {
return new SimpleDateFormat(format).parse(ts);
}
catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* 日期路径 即年/月/日 如2018/08/08
*/
public static final String datePath() {
Date now = new Date();
return DateFormatUtils.format(now, "yyyy/MM/dd");
}
/**
* 日期路径 即年/月/日 如20180808
*/
public static final String dateTime() {
Date now = new Date();
return DateFormatUtils.format(now, "yyyyMMdd");
}
/**
* 日期型字符串转化为日期 格式
*/
public static Date parseDate(Object str) {
if (str == null) {
return null;
}
try {
return parseDate(str.toString(), parsePatterns);
}
catch (ParseException e) {
return null;
}
}
/**
* 获取服务器启动时间
*/
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 + "分钟";
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。