123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754 |
- package com.behosoft.util;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- public class DateUtils extends org.apache.commons.lang.time.DateUtils {
- /** 数据库存储的时间格式串,如yyyymmdd 或yyyymmddHHMiSS */
- public static final int DB_STORE_DATE = 1;
- /** 用连字符-分隔的时间时间格式串,如yyyy-mm-dd 或yyyy-mm-dd HH:Mi:SS */
- public static final int HYPHEN_DISPLAY_DATE = 2;
- /** 用连字符.分隔的时间时间格式串,如yyyy.mm.dd 或yyyy.mm.dd HH:Mi:SS */
- public static final int DOT_DISPLAY_DATE = 3;
- /** 用中文字符分隔的时间格式串,如yyyy年mm月dd 或yyyy年mm月dd HH:Mi:SS */
- public static final int CN_DISPLAY_DATE = 4;
- /***** 数据库存储时间格式串,如yyyymmddHHMissSS **/
- public static final int DB_LONG_DATE = 5;
-
- public static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
- public static Date setDateTime(Date date, int hour, int minute, int second) {
- Calendar c = Calendar.getInstance();
- c.setTime(date);
- c.set(Calendar.HOUR_OF_DAY, hour);
- c.set(Calendar.MINUTE, minute);
- c.set(Calendar.SECOND, second);
- return c.getTime();
- }
-
- public static Date getNextDay() {
- Calendar calendar = Calendar.getInstance();
- calendar.add(Calendar.DAY_OF_MONTH, 1);
- return calendar.getTime();
- }
-
- public static String getDateTimeStr(Date date) {
- return DATETIME_FORMAT.format(date);
- }
- /**
- * 返回当前时间
- *
- * @return
- */
- public static Date getCurrentDate() {
- return new Date(System.currentTimeMillis());
- }
- /**
- * 得到精确到秒的格式化当前时间串
- *
- * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
- * @return 当前时间格式化时间串
- */
- public static String getCurrTimeStr(int formatType) {
- return getTimeStr(new Date(), formatType);
- }
- public static String getCurrTimeStr() {
- return getCurrTimeStr(HYPHEN_DISPLAY_DATE);
- }
- public static String getTimeStr(Date date) {
- return getTimeStr(date, HYPHEN_DISPLAY_DATE);
- }
- /**
- * 得到精确到秒的格式化时间串
- *
- * @param date 指定时间
- * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
- * @return 指定时间的格式化时间串
- */
- public static String getTimeStr(Date date, int formatType) {
- if (formatType < DB_STORE_DATE || formatType > DB_LONG_DATE) {
- throw new IllegalArgumentException("时间格式化类型不是合法的值。");
- }
- else {
- String formatStr = null;
- switch (formatType) {
- case DB_STORE_DATE:
- formatStr = "yyyyMMddHHmmss";
- break;
- case HYPHEN_DISPLAY_DATE:
- formatStr = "yyyy'-'MM'-'dd HH:mm:ss";
- break;
- case DOT_DISPLAY_DATE:
- formatStr = "yyyy.MM.dd HH:mm:ss";
- break;
- case CN_DISPLAY_DATE:
- formatStr = "yyyy'年'MM'月'dd'日' HH:mm:ss";
- break;
- case DB_LONG_DATE:
- formatStr = "yyyyMMddHHmmssSS";
- }
- SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
- return sdf.format(date);
- }
- }
- /**
- * 得到精确到天的当前格式化日期串
- *
- * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
- * @return
- */
- public static String getCurrDateStr(int formatType) {
- return getDateStr(new Date(), formatType);
- }
- public static String getCurrDateStr() {
- return getCurrDateStr(HYPHEN_DISPLAY_DATE);
- }
- /**
- * 得到精确到天的指定时间格式化日期串
- *
- * @param date 指定时间
- * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
- * @return 指定时间格式化日期串
- */
- public static String getDateStr(Date date, int formatType) {
- if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
- throw new IllegalArgumentException("时间格式化类型不是合法的值。");
- }
- else {
- String formatStr = null;
- switch (formatType) {
- case DB_STORE_DATE:
- formatStr = "yyyyMMdd";
- break;
- case HYPHEN_DISPLAY_DATE:
- formatStr = "yyyy-MM-dd";
- break;
- case DOT_DISPLAY_DATE:
- formatStr = "yyyy.MM.dd";
- break;
- case CN_DISPLAY_DATE:
- formatStr = "yyyy'年'MM'月'dd'日'";
- break;
- }
- SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
- return sdf.format(date);
- }
- }
- /**
- * 得到精确到月的当前时间格式化年月串
- *
- * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
- * @return 精确到月当前时间格式化年月串
- */
- public static String getYearMonthStr(int formatType) {
- return getYearMonthStr(new Date(), formatType);
- }
- /**
- * 得到精确到月的指定时间格式化年月串
- *
- * @param date 指定的时间
- * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
- * @return 精确到月当前时间格式化年月串
- */
- public static String getYearMonthStr(Date date, int formatType) {
- if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
- throw new IllegalArgumentException("时间格式化类型不是合法的值。");
- }
- else {
- String formatStr = null;
- switch (formatType) {
- case DB_STORE_DATE:
- formatStr = "yyyyMM";
- break;
- case HYPHEN_DISPLAY_DATE:
- formatStr = "yyyy-MM";
- break;
- case DOT_DISPLAY_DATE:
- formatStr = "yyyy.MM";
- break;
- case CN_DISPLAY_DATE:
- formatStr = "yyyy'年'MM'月'";
- break;
- }
- SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
- return sdf.format(date);
- }
- }
- /**
- * 将数据库存储的日期格式串转换为各种显示的格式
- *
- * @param dateStr 最小6位,最大14位的数据库存储格式时间串如:20041212
- * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
- * @return 格式化的时间串
- */
- public static String toDisplayStr(String dateStr, int formatType) {
- if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
- throw new IllegalArgumentException("时间格式化类型不是合法的值。");
- }
- if (dateStr == null || dateStr.length() < 6 || dateStr.length() > 14 || formatType == DB_STORE_DATE) {
- return StringUtils.toVisualString(dateStr);
- }
- else {
- char[] charArr = null;
- switch (formatType) {
- case HYPHEN_DISPLAY_DATE:
- charArr = new char[] { '-', '-', ' ', ':', ':' };
- break;
- case DOT_DISPLAY_DATE:
- charArr = new char[] { '.', '.', ' ', ':', ':' };
- break;
- case CN_DISPLAY_DATE:
- charArr = new char[] { '年', '月', ' ', ':', ':' };
- break;
- default:
- charArr = new char[] { '-', '-', ' ', ':', ':' };
- }
- try {
- SimpleDateFormat sdf_1 = null;
- SimpleDateFormat sdf_2 = null;
- switch (dateStr.length()) {
- case 6:
- sdf_1 = new SimpleDateFormat("yyyyMM");
- sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM");
- break;
- case 8:
- sdf_1 = new SimpleDateFormat("yyyyMMdd");
- sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM'" + charArr[1] + "'dd");
- break;
- case 10:
- sdf_1 = new SimpleDateFormat("yyyyMMddHH");
- sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM'" + charArr[1] + "'dd'" + "+charArr[2]" + "'HH");
- break;
- case 12:
- sdf_1 = new SimpleDateFormat("yyyyMMddHHmm");
- sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM'" + charArr[1] + "'dd'" + charArr[2] + "'HH'" + charArr[3] + "'mm");
- break;
- case 14:
- sdf_1 = new SimpleDateFormat("yyyyMMddHHmmss");
- sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM'" + charArr[1] + "'dd'" + charArr[2] + "'HH'" + charArr[3] + "'mm'" + charArr[4] + "'ss");
- break;
- default:
- return dateStr;
- }
- return sdf_2.format(sdf_1.parse(dateStr));
- }
- catch (ParseException ex) {
- return dateStr;
- }
- }
- }
- /**
- * 将显示格式的时间字符串转换为数据库存储的类型
- *
- * @param dateStr 最小4位,最大19位。显示的时间格式时间串如:2004-12-12
- * @return 数据库存储的时间字符串
- */
- public static String toStoreStr(String dateStr) {
- if (dateStr == null || dateStr.trim().equals("")) {
- return "";
- }
- StringBuilder strBuf = new StringBuilder();
- for (int i = 0; i < dateStr.length(); i++) {
- if (dateStr.charAt(i) >= '0' && dateStr.charAt(i) <= '9') {
- strBuf.append(dateStr.charAt(i));
- }
- }
- return strBuf.toString();
- }
- /**
- * <b>功能描述:</b> 把时间转换成14位的字符串,不足位数补充 0 或者 9
- *
- * @param dateStr String
- * @param key int
- * @return String
- */
- public static String toStoreStr14(String dateStr, boolean bMax) {
- if (dateStr == null || dateStr.trim().equals("")) {
- return "";
- }
- StringBuilder strBuf = new StringBuilder(toStoreStr(dateStr));
- String retStr = strBuf.toString();
- if (bMax) {
- retStr = StringUtils.pad(retStr, 14, '9', true);
- }
- else {
- retStr = StringUtils.pad(retStr, 14, '0', true);
- }
- return retStr;
- }
- /**
- * 将生日存储的时间格式转化为年龄(周岁,小数点后不计)
- *
- * @param birthdayStr 生日字段 "yyyymmdd"
- * @return 年龄
- */
- public static String birthdayToAge(String birthdayStr) {
- if (birthdayStr == null || birthdayStr.length() < 6) {
- return "";
- }
- else {
- int birthYear = Integer.parseInt(birthdayStr.substring(0, 4));
- int birthMonth = Integer.parseInt(birthdayStr.substring(4, 6));
- Calendar cal = new GregorianCalendar();
- int currYear = cal.get(Calendar.YEAR);
- int currMonth = cal.get(Calendar.MONTH);
- int age = currYear - birthYear;
- age -= (currMonth < birthMonth) ? 1 : 0;
- return "" + age;
- }
- }
- /**
- * 功能描述: 将年龄转换为生日
- *
- * @param age int
- * @return String
- */
- public static String ageToBirthday(int age) {
- String currDateStr = DateUtils.getYearMonthStr(DB_STORE_DATE);
- int currDateInt = Integer.parseInt(currDateStr);
- int ageDateInt = currDateInt - age * 100;
- return String.valueOf(ageDateInt);
- }
- /**
- * 功能描述: 增加一年
- *
- * @param age int
- * @return String
- */
- public static String nextAgeDate(String birDate) {
- int currDateInt = Integer.parseInt(birDate);
- int ageDateInt = currDateInt + 100;
- return String.valueOf(ageDateInt);
- }
- /**
- * @param dateTimeStr String 格式化的时间串
- * @param formatType 数据格式类型 {@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
- * @param detal int 增加或减少的时间
- * @param field int 参见Calendar中关于时间字段属性的定义
- * @return String 返回的
- */
- public static String add(String dateTimeStr, int formatType, int detal, int field) {
- if (dateTimeStr == null || dateTimeStr.length() < 6) {
- return dateTimeStr;
- }
- else {
- try {
- String formatStr = null;
- switch (formatType) {
- case DB_STORE_DATE:
- formatStr = "yyyyMMddHHmmss";
- break;
- case HYPHEN_DISPLAY_DATE:
- formatStr = "yyyy-MM-dd HH:mm:ss";
- break;
- case DOT_DISPLAY_DATE:
- formatStr = "yyyy.MM.dd HH:mm:ss";
- break;
- case CN_DISPLAY_DATE:
- formatStr = "yyyy'年'MM'月' HH:mm:ss";
- break;
- default:
- formatStr = "yyyyMMddHHmmss";
- break;
- }
- formatStr = formatStr.substring(0, dateTimeStr.length());
- SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
- Date d = sdf.parse(dateTimeStr);
- GregorianCalendar g = new GregorianCalendar();
- g.setTime(d);
- g.add(field, detal);
- d = g.getTime();
- return sdf.format(d);
- }
- catch (ParseException ex) {
- ex.printStackTrace();
- return dateTimeStr;
- }
- }
- }
- // /**
- // * @param date Date 时间
- // * @param detal int 增加的时间
- // * @param field int 参见Calendar中关于时间字段属性的定义
- // * @return Date
- // */
- // public static Date add(Date date, int detal, int field)
- // {
- // Calendar g = new GregorianCalendar();
- // g.setTime(date);
- // g.add(field, detal);
- // return g.getTime();
- // }
- /**
- * 日期、时间格式化
- *
- * @param date Date 将要被格式化的日期对象
- * @param outFmt String 返回样式,参照类说明,如:yyyy年MM月dd日
- * @return String 格式化后的日期、时间字符串,data为null时返回null,outFmt非法时返回yyyyMMdd格式
- */
- public static String getDateFormat(Date date, String outFmt) {
- if (null == date) {
- return null;
- }
- if (null == outFmt || "".equals(outFmt.trim())) { // outFmt非法
- outFmt = "yyyyMMdd";
- }
- String retu = null;
- SimpleDateFormat dateFormat = null;
- try {
- dateFormat = new SimpleDateFormat(outFmt);
- }
- catch (IllegalArgumentException iaex) { // outFmt非法
- dateFormat = new SimpleDateFormat("yyyyMMdd");
- }
- retu = dateFormat.format(date);
- dateFormat = null;
- return retu;
- }
- /**
- * 把日期时间对象格式化为yyyyMMdd样式
- *
- * @param date Date 将要被格式化的日期对象
- * @return String 格式化后的日期、时间字符串,如:20041001
- */
- public static String getDateFormat(Date date) {
- return getDateFormat(date, "yyyyMMdd");
- }
- /**
- * 把系统当前日期时间格式化为指定的样式
- *
- * @param outFmt String 返回样式,参照类说明,如:yyyy年MM月dd日
- * @return String 格式化后的日期、时间字符串,如:2004年10月01日
- */
- public static String getDateFormat(String outFmt) {
- return getDateFormat(new Date(), outFmt);
- }
- /**
- * 把系统当前日期时间格式化为默认样式yyyyMMdd
- *
- * @return String 格式化后的日期、时间字符串,如:20041001
- */
- public static String getDateFormat() {
- return getDateFormat(new Date(), "yyyyMMdd");
- }
- /**
- * 日期、时间格式化
- *
- * @param millis long the number of milliseconds(毫秒) since January 1, 1970, 00:00:00 GMT.
- * @param outFmt String 返回样式,参照类说明,如:yyyy年MM月dd日
- * @return String 格式化后的日期、时间字符串
- */
- public static String getDateFormat(long millis, String outFmt) {
- Date d = new Date(millis);
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(d);
- String retu = getDateFormat(calendar.getTime(), outFmt);
- calendar = null;
- return retu;
- }
- /**
- * 日期、时间格式化
- *
- * @param datestr String 存在一定格式的日期、时间字符串,如:20041001、200410011503
- * @param inFmt String 对datestr参数格式说明,参照类说明,如:yyyyMMdd、yyyyMMddHHmm
- * @param outFmt String 返回样式,参照类说明,如:yyyy年MM月dd日
- * @throws ParseException 当datestr不能格式化为inFmt格式时抛出此异常
- * @return String 格式化后的日期、时间字符串,如:2004年10月01日、2004年10月01日 <BR>
- * 输出样式outFmt非法时,使用yyyyMMdd格式输出
- */
- public static String getDateFormat(String datestr, String inFmt, String outFmt) throws ParseException {
- if (null == datestr || "".equals(datestr.trim())) {
- return datestr;
- }
- if (null == inFmt || "".equals(inFmt.trim())) {
- return datestr;
- }
- if (null == outFmt || "".equals(outFmt.trim())) { // 输出样式非法
- outFmt = "yyyyMMdd";
- }
- java.util.Date inDate = getDate(datestr, inFmt);
- if (null == inDate) { // 根据inFmt分析datestr时抛出异常
- return datestr;
- }
- String retu = getDateFormat(inDate, outFmt);
- inDate = null;
- return retu;
- }
- /**
- * 把日期时间字符串,按inFmt样式转化为日期对象,然后格式化为默认样式yyyyMMdd
- *
- * @param datestr String 存在一定格式的日期、时间字符串,如:20041001、200410011503
- * @param inFmt String 对datestr参数格式说明,参照类说明,如:yyyyMMdd、yyyyMMddHHmm
- * @throws ParseException 当datestr不能格式化为inFmt格式时抛出此异常
- * @return String 格式化后的日期、时间字符串,如:20041001、20041001
- */
- public static String getDateFormat(String datestr, String inFmt) throws ParseException {
- return getDateFormat(datestr, inFmt, "yyyyMMdd");
- }
- /**
- * 根据inFmt的样式,日期时间字符串转化为日期时间对象
- *
- * @param datestr String 日期时间字符串,如:20041001、2004年10月01日 15:03
- * @param inFmt String 对datestr参数格式说明,参照类说明,如yyyyMMdd、yyyy年MM月dd日 HH:mm
- * @throws ParseException 当datestr不能格式化为inFmt格式时抛出此异常
- * @return Date 日期时间对象,格式inFmt非法时,使用yyyyMMdd格式
- */
- public static Date getDate(String datestr, String inFmt) throws ParseException {
- if (null == datestr || "".equals(datestr.trim())) {
- return null;
- }
- if (null == inFmt || "".equals(inFmt.trim())) { // inFmt非法
- inFmt = "yyyyMMdd";
- }
- java.util.Date inDate = null;
- // 依据inFmt格式把日期字符串转化为日期对象
- SimpleDateFormat inDateFormat = new SimpleDateFormat(inFmt);
- inDateFormat.setLenient(true);
- inDate = inDateFormat.parse(datestr);
- inDateFormat = null;
- return inDate;
- }
- /**
- * 对日期时间对象进行调整,实现如昨天是几号,去年的今天星期几等. <BR>
- * 例子:
- *
- * <pre>
- * <blockquote>
- * 计算去年今天星期几
- * Date date = DateUtils.addDate(new Date(),Calendar.YEAR,-1);
- * System.out.println(DateUtils.getDateFormat(date,"E"));
- * 打印60天后是什么日期,并显示为 yyyy-MM-dd 星期
- * Date date = DateUtils.addDate(new Date(),Calendar.DATE,60);
- * System.out.println(DateUtils.getDateFormat(date,"yyyy-MM-dd E"));
- * </blockquote>
- * </pre>
- *
- * @param date Date 需要调整的日期时间对象
- * @param CALENDARFIELD int 对日期时间对象以什么单位进行调整:
- *
- * <pre>
- * <blockquote>
- * 年 Calendar.YEAR
- * 月 Calendar.MONTH
- * 日 Calendar.DATE
- * 时 Calendar.HOUR
- * 分 Calendar.MINUTE
- * 秒 Calendar.SECOND
- * </blockquote>
- * </pre>
- * @param amount int 调整数量,>0表向后调整(明天,明年),<0表向前调整(昨天,去年)
- * @return Date 调整后的日期时间对象
- */
- public static Date addDate(Date date, int CALENDARFIELD, int amount) {
- if (null == date) {
- return date;
- }
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.add(CALENDARFIELD, amount);
- return calendar.getTime();
- }
- /**
- * 对日期时间对象进行调整.
- *
- * @param datestr String 需要调整的日期时间字符串,它的格式为yyyyMMdd
- * @param CALENDARFIELD int 对日期时间对象以什么单位进行调整
- * @param amount int 调整数量
- * @throws ParseException 当datestr不能格式化为yyyyMMdd格式时抛出此异常
- * @return Date 调整后的日期时间对象
- * @see #addDate(java.util.Date, int, int)
- */
- public static Date addDate(String datestr, int CALENDARFIELD, int amount) throws ParseException {
- return addDate(getDate(datestr, "yyyyMMdd"), CALENDARFIELD, amount);
- }
- /**
- * 根据出生日期,计算出在某一个日期的年龄
- *
- * @param birthday Date 出生日期时间对象
- * @param date2 Date 计算日期对象
- * @return int 返回date2那一天出生日期为birthday的年龄,如果birthday大于date2则返回-1
- */
- public static int getAge(Date birthday, Date date2) {
- if (null == birthday || null == date2) {
- return -1;
- }
- if (birthday.after(date2)) { // birthday大于date2
- return -1;
- }
- int ibdYear = StringUtils.getInt(getDateFormat(birthday, "yyyy"), -1);
- int idate2Year = StringUtils.getInt(getDateFormat(date2, "yyyy"), -1);
- if (ibdYear < 0 || idate2Year < 0) {
- return -1;
- }
- if (ibdYear > idate2Year) {
- return -1;
- }
- return idate2Year - ibdYear + 1;
- }
- /**
- * 根据出生日期,计算出当前的年龄
- *
- * @param birthday Date 出生日期时间对象
- * @return int 返回出生日期为birthday的年龄,如果birthday大于当前系统日期则返回-1
- */
- public static int getAge(Date birthday) {
- return getAge(birthday, new Date());
- }
- /**
- * 根据出生日期,计算出当前的年龄
- *
- * @param birthdaystr String 出生日期时间字符串,其格式一定为yyyyMMdd
- * @throws ParseException 当datestr不能格式化为yyyyMMdd格式时抛出此异常
- * @return int 返回出生日期为birthday的年龄,如果birthday大于当前系统日期则返回-1
- */
- public static int getAge(String birthdaystr) throws ParseException {
- return getAge(getDate(birthdaystr, "yyyyMMdd"));
- }
- public static Date getCurDate() {
- return Calendar.getInstance().getTime();
- }
- public static String getCurDay(Date date) {
- String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
- if (w < 0)
- w = 0;
- return weekDays[w];
- }
-
- /**
- * 格式化
- *
- * @param str 字符
- * @param format 格式化
- *
- * @return Date
- * @throws ParseException 异常
- */
- public static Date parse(final String str) throws ParseException
- {
- final Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str);
- return date;
- }
-
- /**
- *
- * <p>
- * 计算指定日期到今天的天数
- * </p>
- * @param sql 参数说明
- * @param args 参数说明
- * @return 返回值说明
- */
- public static Long betweenDay(String beginDate) throws ParseException{
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- Date cDate = df.parse(beginDate);
- Date dDate = new Date();
- return (dDate.getTime()-cDate.getTime())/(24*60*60*1000);
- }
-
- public static String dateToStr(String dateFormat, Date date){
- //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
- return sdf.format(date);
- }
-
- //获取几天前日期
- public static String getDateBefore(Date d,int day){
- Calendar now =Calendar.getInstance();
- now.setTime(d);
- now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
- return sdf.format(now.getTime());
- }
-
- public static Long subDay(Date day1,Date day2){
- return (day1.getTime()-day2.getTime())/(24*60*60*1000);
- }
-
- public static double subDouble(Object a,Object b)
- {
- return asDouble(a, 0d) - asDouble(b, 0d);
- }
-
- public static double asDouble(Object obj, double defaultValue) {
- double value = defaultValue;
- if (obj instanceof String) {
- String str = (String) obj;
- if (StringUtils.isNotBlank(str)) {
- try {
- value = Double.parseDouble(str);
- } catch (NumberFormatException e) {
- }
- }
- } else if (obj instanceof Number) {
- value = ((Number) obj).doubleValue();
- }
- return value;
- }
- }
|