java自定義Date工具類,計算年齡

package com.pam.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 處理時間工具類
 * 
 * @author xuyang
 *
 */
public class DateUtil {


/**
* 格式類型:"yyyyMM"
*/
public static final String DATE_JFP_STR = "yyyyMM";
/**
* 格式類型:"yyyy-MM-dd HH:mm:ss"
*/
public static final String DATE_FULL_STR = "yyyy-MM-dd HH:mm:ss";

/**
* 格式類型:"yyyy-MM-dd HH:mm:ss:SS"
*/
public static final String DATE_FULL_SS_STR = "yyyy-MM-dd HH:mm:ss:SS";

/**
* 格式類型:"yyyy-MM-dd"
*/
public static final String DATE_SMALL_STR = "yyyy-MM-dd";
/**
* 格式類型:"yyMMddHHmmss"
*/
public static final String DATE_KEY_STR = "yyMMddHHmmss";
/**
* 格式類型:"yyMMddHHmmssSS"
*/
public static final String DATE_FULL_KEY_SS_STR = "yyyyMMddHHmmssSS";

/**
* 格式類型:"yyyy-MM-dd-HH-mm-ss"
*/
public static final String DATE_KEY_FULL_STR = "yyyy-MM-dd-HH-mm-ss";
/**
* 格式類型:"yyyyMMdd"
*/
public static final String DATE_YYYYMMDD_STR = "yyyyMMdd";
/**
* 格式類型:"MM/dd/yyyy"
*/
public static final String STR_MMDDYYYY_DATE = "MM/dd/yyyy";
/**
* 格式類型:"MM/dd/yyyy HH:mm:ss"
*/
public static final String STR_MMDDYYYYHHMMSS_DATE = "MM/dd/yyyy HH:mm:ss";
/**
* 格式類型:"yyyy/MM/dd HH:mm:ss"
*/
public static final String STR_YYYYMMDDHHMMSS_DATE = "yyyy/MM/dd HH:mm:ss";
/**
* 格式類型:"yyyy,MM,dd,HH,mm"
*/
public static final String DATE_YYYY_MM_DD_HH_MM = "yyyy,MM,dd,HH,mm";


/**
* 將Date轉換爲特定格式字符串
* 
* @param date
*            要格式化的源時間coordinate
* @param pattern
*            要轉化的目的格式;例:"yyyy-MM-dd HH:mm:ss"
* @return String
*/
public static String getDateToString(Date date, String pattern) {
String returnValue = "";
if (date != null) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
returnValue = sdf.format(date);
}
return returnValue;
}


/**
* 將字符串轉換爲Date
* 
* @param dateStr
*            要格式化的源時間字符串
* @param pattern
*            要轉化的目的格式;例:"yyyy-MM-dd HH:mm:ss"
* @return Date
*/
public static Date getStringToDate(String dateStr, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}


/**
* Date轉Calender
* 
* @param date
*            要轉化的源時間
* @return Calendar
*/
public static Calendar getDateToCalendar(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}


/**
* Calender轉Date
* 
* @param date
*            要轉化的源時間
* @return Date
*/
public static Date getCalendarToDate(Calendar calendar) {
return calendar.getTime();
}


/**
* 增加或減少時間後的時間
* 
* @param date
*            要增加或減少的源時間
* @param dateType
*            取值Calendar常量;例:Calendar.MINUTE
* @param amount
*            對應增加或減少的數量;例:+5或-5
* @return Date
*/
public static Date getDateAfterAddOrSubtract(Date date, int dateType, int amount) {
Date newDate = null;
if (date != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(dateType, amount);
newDate = calendar.getTime();
}
return newDate;
}


/**
* 獲取日期中的某數值
* 
* @param date
*            要取值的源時間
* @param dateType
*            取值Calendar常量;例:Calendar.MINUTE
* @return Integer
*/
public static Integer getInteger(Date date, int dateType) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(dateType);
}


/**
* 獲取日期中的分鐘數
* 
* @param date
*            要取值的源時間
* @return Integer
*/
public static Integer getMinute(Date date) {
return getInteger(date, Calendar.MINUTE);
}


/**
* 獲取日期中的小時數(24小時)
* 
* @param date
*            要取值的源時間
* @return Integer
*/
public static Integer getHourFor24(Date date) {
return getInteger(date, Calendar.HOUR_OF_DAY);
}


/**
* 獲取日期中的小時數(12小時)
* 
* @param date
*            要取值的源時間
* @return Integer
*/
public static Integer getHourFor12(Date date) {
return getInteger(date, Calendar.HOUR);
}


/**
* 獲取日期中的秒數
* 
* @param date
*            要取值的源時間
* @return Integer
*/
public static Integer getSecond(Date date) {
return getInteger(date, Calendar.SECOND);
}


/**
* 獲取日期中的天數
* 
* @param date
*            要取值的源時間
* @return Integer
*/
public static Integer getDay(Date date) {
return getInteger(date, Calendar.DATE);
}


/**
* 獲取日期中的月數
* 
* @param date
*            要取值的源時間
* @return Integer
*/
public static Integer getMonth(Date date) {
return getInteger(date, Calendar.MONTH) + 1;
}


/**
* 獲取日期中的年數
* 
* @param date
*            要取值的源時間
* @return Integer
*/
public static Integer getYear(Date date) {
return getInteger(date, Calendar.YEAR);
}


/**
* 獲取年齡
* @param birthDate
* @return
*/
public static int getAge(Date birthDate) {


if (birthDate == null)
throw new RuntimeException("出生日期不能爲null");


int age = 0;


Date now = new Date();


SimpleDateFormat format_y = new SimpleDateFormat("yyyy");
SimpleDateFormat format_M = new SimpleDateFormat("MM");
SimpleDateFormat format_d = new SimpleDateFormat("dd");


String birth_year = format_y.format(birthDate);
String this_year = format_y.format(now);


String birth_month = format_M.format(birthDate);
String this_month = format_M.format(now);

String birth_day = format_d.format(birthDate);
String this_day = format_d.format(now);


// 初步,估算
age = Integer.parseInt(this_year) - Integer.parseInt(birth_year);


// 如果未到出生月份,則age - 1
if (this_month.compareTo(birth_month) < 0){
age -= 1;
}else{
if(this_day.compareTo(birth_day) < 0){
age -= 1;
}
}
if (age < 0)
age = 0;
return age;
}


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章