package com.xes.app.job.utils.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 日期工具类 默认使用 "yyyy-MM-dd HH:mm:ss" 格式化日期 * */ public class DateUtils { /** * 英文简写(默认)如:2010-12-01 */ public final static String FORMAT_SHORT = "yyyy-MM-dd"; /** * 英文全称 如:2010-12-01 23:15 */ public final static String FORMAT_LONG = "yyyy-MM-dd HH:mm"; /** * 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S */ public final static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S"; /** * 中文简写 如:2010年12月01日 */ public final static String FORMAT_SHORT_CN = "yyyy年MM月dd"; /** * 中文全称 如:2010年12月01日 23时15分06秒 */ public final static String FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒"; /** * 精确到毫秒的完整中文时间 */ public final static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒"; /** * 获得默认的 date pattern */ public static String getDatePattern() { return FORMAT_LONG; } /** * 根据预设格式返回当前日期 * * @return */ public static String getNow() { return format(new Date()); } /** * 根据用户格式返回当前日期 * * @param format * @return */ public static String getNow(String format) { return format(new Date(), format); } /** * 使用预设格式格式化日期 * * @param date * @return */ public static String format(Date date) { return format(date, getDatePattern()); } /** * 使用用户格式格式化日期 * * @param date * 日期 * @param pattern * 日期格式 * @return */ public static String format(Date date, String pattern) { String returnValue = ""; if (date != null) { SimpleDateFormat df = new SimpleDateFormat(pattern); returnValue = df.format(date); } return (returnValue); } /** * 使用预设格式提取字符串日期 * * @param strDate * 日期字符串 * @return */ public static Date parse(String strDate) { return parse(strDate, getDatePattern()); } /** * 使用用户格式提取字符串日期 * * @param strDate * 日期字符串 * @param pattern * 日期格式 * @return */ public static Date parse(String strDate, String pattern) { SimpleDateFormat df = new SimpleDateFormat(pattern); try { return df.parse(strDate); } catch (ParseException e) { e.printStackTrace(); return null; } } /** * 在日期上增加数个整月 * * @param date * 日期 * @param n * 要增加的月数 * @return */ public static Date addMonth(Date date, int n) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, n); return cal.getTime(); } /** * 在日期上增加天数 * * @param date * 日期 * @param n * 要增加的天数 * @return */ public static Date addDay(Date date, int n) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DATE, n); return cal.getTime(); } /** * 获取时间戳 */ public static String getTimeString() { SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL); Calendar calendar = Calendar.getInstance(); return df.format(calendar.getTime()); } /** * 获取日期年份 * * @param date * 日期 * @return */ public static String getYear(Date date) { return format(date).substring(0, 4); } /** * 按默认格式的字符串距离今天的天数 * * @param date * 日期字符串 * @return */ public static int countDays(String date) { long t = Calendar.getInstance().getTime().getTime(); Calendar c = Calendar.getInstance(); c.setTime(parse(date)); long t1 = c.getTime().getTime(); return (int) (t / 1000 - t1 / 1000) / 3600 / 24; } /** * 按用户格式字符串距离今天的天数 * * @param date * 日期字符串 * @param format * 日期格式 * @return */ public static int countDays(String date, String format) { long t = Calendar.getInstance().getTime().getTime(); Calendar c = Calendar.getInstance(); c.setTime(parse(date, format)); long t1 = c.getTime().getTime(); return (int) (t / 1000 - t1 / 1000) / 3600 / 24; }
/**
* 获取当前时间,将当前时间 减去固定的天数,返回Date
* @param day 被减去的天数
* @return resultDate
* @throws 隔壁老王
*/
public static Date currentDatebetweenDate(int day) throws ParseException{
SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date beginDate = new Date();
Calendar date = Calendar.getInstance();
date.setTime(beginDate);
date.set(Calendar.DATE, date.get(Calendar.DATE) - day);
Date resultDate = dft.parse(dft.format(date.getTime()));
return resultDate;
}
/** * 根据用户生日计算年龄 */ public static int getAgeByBirthday(Date birthday) { Calendar cal = Calendar.getInstance(); if (cal.before(birthday)) { throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!"); } int yearNow = cal.get(Calendar.YEAR); int monthNow = cal.get(Calendar.MONTH) + 1; int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); cal.setTime(birthday); int yearBirth = cal.get(Calendar.YEAR); int monthBirth = cal.get(Calendar.MONTH) + 1; int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); int age = yearNow - yearBirth; if (monthNow <= monthBirth) { if (monthNow == monthBirth) { // monthNow==monthBirth if (dayOfMonthNow < dayOfMonthBirth) { age--; } } else { // monthNow>monthBirth age--; } } return age; }
/**
* 通过身份证号码获取出生日期、性别、年龄
* @param certificateNo
* @return 返回的出生日期格式:1990-01-01 性别格式:F-女,M-男
*/
public static Map<String, String> getBirAgeSex(String certificateNo) {
String birthday = "";
String age = "";
String sexCode = "";
int year = Calendar.getInstance().get(Calendar.YEAR);
char[] number = certificateNo.toCharArray();
boolean flag = true;
if (number.length == 15) {
for (int x = 0; x < number.length; x++) {
if (!flag) return new HashMap<String, String>();
flag = Character.isDigit(number[x]);
}
* 通过身份证号码获取出生日期、性别、年龄
* @param certificateNo
* @return 返回的出生日期格式:1990-01-01 性别格式:F-女,M-男
*/
public static Map<String, String> getBirAgeSex(String certificateNo) {
String birthday = "";
String age = "";
String sexCode = "";
int year = Calendar.getInstance().get(Calendar.YEAR);
char[] number = certificateNo.toCharArray();
boolean flag = true;
if (number.length == 15) {
for (int x = 0; x < number.length; x++) {
if (!flag) return new HashMap<String, String>();
flag = Character.isDigit(number[x]);
}
} else if (number.length == 18) {
for (int x = 0; x < number.length - 1; x++) {
if (!flag) return new HashMap<String, String>();
flag = Character.isDigit(number[x]);
}
}
if (flag && certificateNo.length() == 15) {
birthday = "19" + certificateNo.substring(6, 8) + "-"+ certificateNo.substring(8, 10) + "-"
+ certificateNo.substring(10, 12);
sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 3, certificateNo.length())) % 2 == 0 ? "F" : "M";
age = (year - Integer.parseInt("19" + certificateNo.substring(6, 8))) + "";
for (int x = 0; x < number.length - 1; x++) {
if (!flag) return new HashMap<String, String>();
flag = Character.isDigit(number[x]);
}
}
if (flag && certificateNo.length() == 15) {
birthday = "19" + certificateNo.substring(6, 8) + "-"+ certificateNo.substring(8, 10) + "-"
+ certificateNo.substring(10, 12);
sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 3, certificateNo.length())) % 2 == 0 ? "F" : "M";
age = (year - Integer.parseInt("19" + certificateNo.substring(6, 8))) + "";
} else if (flag && certificateNo.length() == 18) {
birthday = certificateNo.substring(6, 10) + "-" + certificateNo.substring(10, 12) + "-"
+ certificateNo.substring(12, 14);
sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 4, certificateNo.length() - 1)) % 2 == 0 ? "F" : "M";
age = (year - Integer.parseInt(certificateNo.substring(6, 10))) + "";
birthday = certificateNo.substring(6, 10) + "-" + certificateNo.substring(10, 12) + "-"
+ certificateNo.substring(12, 14);
sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 4, certificateNo.length() - 1)) % 2 == 0 ? "F" : "M";
age = (year - Integer.parseInt(certificateNo.substring(6, 10))) + "";
}
Map<String, String> map = new HashMap<String, String>();
map.put("birthday", birthday);
map.put("age", age);
map.put("sexCode", sexCode);
return map;
}
map.put("birthday", birthday);
map.put("age", age);
map.put("sexCode", sexCode);
return map;
}
}