package com.jhkj.application.common.utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
public static final String DATE_FORMAT_STR = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMAT_PAR = "yyyy-MM-dd";
public static final SimpleDateFormat formatLongDate = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
public static final SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd");
public static final SimpleDateFormat formatDate = new SimpleDateFormat(
"yyyyMMddHHmmss");
public static final SimpleDateFormat formatDate1 = new SimpleDateFormat(
"yyyyMMdd");
/**
* 将短时间格式字符串yyyyMMddHHmmss转换为时间
*
* @param strDate
* @return
*/
public static Date strToDate(String strDate) {
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatDate.parse(strDate, pos);
return strtodate;
}
/**
* 将短时间格式字符串yyyy-MM-dd转换为时间
*
* @param strDate
* @return
*/
public static Date strToDate2(String strDate) {
ParsePosition pos = new ParsePosition(0);
Date strtodate = format.parse(strDate, pos);
return strtodate;
}
/**
*
* @param date
* @return
* @throws ParseException
*/
public static Date parseStr2Date(String date) throws ParseException {
synchronized (formatLongDate) {
return formatLongDate.parse(date);
}
}
/**
* 将指定时间格式字符串转换为时间
*
* @param strDate
* @return
*/
public static Date strToDate(String strDate, String pattern) {
ParsePosition pos = new ParsePosition(0);
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
Date strtodate = dateFormat.parse(strDate, pos);
return strtodate;
}
/**
* 显示年月日 格式yyyyMMddHHmmss
*/
public static String getDateFromString(Date date) {
return formatDate.format(date);
}
// 得到一个时间延后或前移几天的时间
public static String getCountedDay(String strDate, Integer delay) {
try {
String mdate = "";
Date d = strToDate2(strDate);
long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
d.setTime(myTime * 1000);
mdate = format.format(d);
return mdate;
} catch (Exception e) {
return "";
}
}
/**
* 判断两个日期之间差了多少天,不足一天,则按一天计算,即20.01天也算21天
*/
public static int dateDiff(Date date1, Date date2) {
if (date1 == null || date2 == null)
return 0;
long baseNum = 3600 * 1000 * 24;
long absNum = Math.abs(date1.getTime() - date2.getTime());
long mod = absNum % baseNum;
int num = (int) (absNum / baseNum);
if (mod > 0)
num++;
return num;
}
/**
* 判断两个日期之间差了多少天,不足一天,则按一天计算,即20.01天也算21天
*/
public static int dateDiff1(Date date1, Date date2) {
if (date1 == null || date2 == null)
return 0;
long baseNum = 3600 * 1000 * 24;
long absNum = date1.getTime() - date2.getTime();
long mod = absNum % baseNum;
int num = (int) (absNum / baseNum);
if (mod > 0)
num++;
return num;
}
/**
* 得到一个日期接下来一年的天数
*/
public static int getYearDay(Date date) {
if (date == null)
return 0;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR,1);
// calendar.add(Calendar.DATE,-1);
Date endDate = calendar.getTime();
long baseNum = 3600 * 1000 * 24;
long absNum = endDate.getTime() - date.getTime();
long mod = absNum % baseNum;
int num = (int) (absNum / baseNum);
if (mod > 0)
num++;
return num;
}
/**
* 得到一个时间延后或前移几月的时间
*
* @param strDate
* @param delay
* @return
*/
public static String getCountedMonth(String strDate, Integer delay) {
try {
Date dd = strToDate(strDate);
Calendar currentDate = Calendar.getInstance();
currentDate.setTime(dd);
currentDate.add(Calendar.MONDAY, delay);
return formatDate.format(currentDate.getTime());
} catch (Exception e) {
return "";
}
}
/**
* 得到一个时间延后或前移几月的时间
*
* @param strDate
* @param delay
* @return
*/
public static String getCountedMonthAll(String strDate, Integer delay) {
try {
Date dd = strToDate(strDate);
Calendar currentDate = Calendar.getInstance();
currentDate.setTime(dd);
currentDate.add(Calendar.MONDAY, delay);
return formatDate.format(currentDate.getTime());
} catch (Exception e) {
return "";
}
}
/**
* 日期转毫秒
*
* @param expireDate
* @return
*/
public static Long getSecondsFromDate(String expireDate) {
if (expireDate == null || expireDate.trim().equals(""))
return 0l;
Date date = null;
try {
date = formatDate.parse(expireDate);
return date.getTime();
} catch (ParseException e) {
e.printStackTrace();
return 0l;
}
}
public static String formatDateStr(Date date) {
return formatDate.format(date);
}
public static String formatDateStr1(Date date) {
return formatDate1.format(date);
}
public static String formatDateStr2(Date date) {
return format.format(date);
}
public static String formatDateStr3(Date date) {
return formatLongDate.format(date);
}
public static Date parseStr2Date1(String date) throws ParseException {
return formatDate.parse(date);
}
/**
* 根据身份编号获取生日Date
*
* @param idCard 身份编号
* @return 生日(yyyyMMdd)
*/
public static Date getBirthByIdCard(String idCard) {
Date date = null;
try {
DateFormat format = new SimpleDateFormat("yyyyMMdd");
date = format.parse(idCard.substring(6, 14));
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 获取当前时间字符串
*/
public static String getDateStr(Date date, String timePattern) {
String dateStr = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(timePattern);
dateStr = sdf.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return dateStr;
}
/**
* 获取当前时间下一个整点时间
*/
public static Date getNextNSecond(Date date, Integer n) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.SECOND, n);
return calendar.getTime();
}
/**
* 获取当前时间下一个整点时间
*/
public static Date getNextHour(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 1);
return calendar.getTime();
}
/**
* 获取当前时间后N天
*/
public static Date getNextNDay(Date date, Integer n) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, n);
return calendar.getTime();
}
/**
* 获取当前时间后N年
*/
public static Date getNextNYear(Date date, Integer n) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, n);
return calendar.getTime();
}
/**
* 获取当前时间小时数(24小时制)
*/
public static Integer getDateHour(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OF_DAY);
}
/**
* 根据生日计算年龄
*
* @param birthday
* @return
*/
public static int getAgeByBirth(Date birthday) {
int age = 0;
try {
Calendar cal = Calendar.getInstance();
if (cal.before(birthday)) {
return 0;
}
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(birthday);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth) age--;
} else {
age--;
}
}
return age;
} catch (Exception e) {//兼容性更强,异常后返回数据
return 0;
}
}
/**
* 根据身份编号获取年龄
*
* @param idCard 身份编号
* @return 生日(yyyyMMdd)
*/
public static int getAgeByIdCard(String idCard) {
Date date = null;
try {
DateFormat format = new SimpleDateFormat("yyyyMMdd");
date = format.parse(idCard.substring(6, 14));
return getAgeByBirth(date);
} catch (ParseException e) {
e.printStackTrace();
return -1;
}
}
/**
* * 将时间戳转换为时间
*
* @param stamp
* @param rex
* @return
*/
public static String stampToDate(String stamp, String rex) {
String res;
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(rex);
long lt = new Long(stamp);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
}
Java operation time tool
Guess you like
Origin blog.csdn.net/qiteng_sijia/article/details/103190147
Recommended
Ranking