java date 年龄_java中常用的与时间有关的方法(string与date转化,出生日期转年龄,时间计算周次等)...

/**

* 计算两个日期之间相差的天数

* @param smdate 较小的时间

* @param bdate  较大的时间

* @return 相差天数

* @throws ParseException

*/

publicstaticintDaysMinus(Date startDate,Date endDate)throwsParseException

{

SimpleDateFormat sdf=newSimpleDateFormat("yyyy-MM-dd");

startDate = sdf.parse(sdf.format(startDate));

endDate = sdf.parse(sdf.format(endDate));

Calendar cal = Calendar.getInstance();

cal.setTime(startDate);

longstartTime = cal.getTimeInMillis();

cal.setTime(endDate);

longendTime = cal.getTimeInMillis();

longbetween_days=(endTime-startTime)/(1000*3600*24);

returnInteger.parseInt(String.valueOf(between_days));

}

/**

*字符串的日期格式的计算相差天数

*@param 开始日期:startDate

*@param 结束日期:endDate

*@return 相差天数

*/

publicstaticintDaysMinus(String startDate,String endDate)throwsParseException{

SimpleDateFormat sdf=newSimpleDateFormat("yyyy-MM-dd");

Calendar cal = Calendar.getInstance();

cal.setTime(sdf.parse(startDate));

longstartTime = cal.getTimeInMillis();

cal.setTime(sdf.parse(endDate));

longendTime = cal.getTimeInMillis();

longbetween_days = (endTime-startTime)/(1000*3600*24);

returnInteger.parseInt(String.valueOf(between_days));

}

/**

* String类型日期转Date

* @param date

* @return

* @throws ParseException

*/

publicstaticDate string2Date(String date)throwsParseException{

SimpleDateFormat sdf = newSimpleDateFormat("yyyy-MM-dd");

Date resultDate = sdf.parse(date);

returnresultDate;

}

/**

* Date类型日期转String

* @param date

* @return

* @throws ParseException

*/

publicstaticString date2String(Date date)throwsParseException{

SimpleDateFormat sdf = newSimpleDateFormat("yyyy-MM-dd");

String currentTime = sdf.format(date);

returncurrentTime;

}

/**

* 日期相加

* @param date

* @param days

* @return

*/

publicstaticDate dateAdd(Date date,intdays) {

Calendar calendar = newGregorianCalendar();

calendar.setTime(date);

calendar.add(calendar.DATE, days);

date = calendar.getTime();

returndate;

}

/**

* String 类型日期相加

* @param date

* @param days

* @return

* @throws ParseException

*/

publicstaticDate dateAdd(String date,intdays)throwsParseException {

Date Datedate = string2Date(date);

Calendar calendar = newGregorianCalendar();

calendar.setTime(Datedate);

calendar.add(calendar.DATE, days);

Datedate = calendar.getTime();

returnDatedate;

}

/**

* 获取当前日期的周次

* 07:"星期日", 01:"星期一", 02:"星期二", 03:"星期三", 04:"星期四", 05:"星期五", 06:"星期六"

* @author 秦海

* @param dt

* @return

*/

publicstaticString DayOfWeek2MatchDict(Date dt) {

String[] weekDays = {"07","01","02","03","04","05","06"};

Calendar cal = Calendar.getInstance();

cal.setTime(dt);

intdayOfWeek = cal.get(Calendar.DAY_OF_WEEK) -1;

if(dayOfWeek <0){

dayOfWeek = 0;

}

returnweekDays[dayOfWeek];

}

/**

* 获取系统当前日期 date类型

* @return

* @throws ParseException

*/

publicstaticDate getDateSysDate()throwsParseException {

Date now = newDate();

SimpleDateFormat sdf = newSimpleDateFormat("yyyy-MM-dd");

String sysDate = sdf.format(now);

Date resultDate = sdf.parse(sysDate);

returnresultDate;

}

/**

* 按照传入的格式获取系统date类型世间

* -年-月-日 时:分:秒.毫秒

* {yyyy-MM-dd HH:mm:ss.fff}:使用24小时制格式化日期

* {yyyy-MM-dd hh:mm:ss.fff}:使用12小时制格式化日期

* {yyyy-MM-dd HH:mm:ss zzz}

* {yyyy-MM-dd HH:mm:ss.ff zzz}

* {yyyy-MM-dd HH:mm:ss.fff zzz}

* {yyyy-MM-dd HH:mm:ss.ffff zzz}

* @param format

* @return

* @throws ParseException

*/

publicstaticDate getDateSysDate(String format)throwsParseException {

Date now = newDate();

SimpleDateFormat sdf = newSimpleDateFormat(format);

String sysDate = sdf.format(now);

Date resultDate = sdf.parse(sysDate);

returnresultDate;

}

}

/**

* 根据出生日期获取人的年龄

*

* @param strBirthDate(yyyy-mm-dd or yyyy/mm/dd)

* @return

*/

publicstaticString getPersonAgeByBirthDate(Date dateBirthDate){

if(dateBirthDate ==null){

return"";

}

SimpleDateFormat dateFormat = newSimpleDateFormat("yyyy-MM-dd");

String strBirthDate=dateFormat.format(dateBirthDate);

//读取当前日期

Calendar c = Calendar.getInstance();

intyear = c.get(Calendar.YEAR);

intmonth = c.get(Calendar.MONTH)+1;

intday = c.get(Calendar.DATE);

//计算年龄

intage = year - Integer.parseInt(strBirthDate.substring(0,4)) -1;

if(Integer.parseInt(strBirthDate.substring(5,7)) 

age++;

} elseif(Integer.parseInt(strBirthDate.substring(5,7))== month && Integer.parseInt(strBirthDate.substring(8,10)) <= day){

age++;

}

returnString.valueOf(age);

}

/**

* 根据出生日期获取人的年龄

*

* @param strBirthDate(yyyy-mm-dd or yyyy/mm/dd)

* @return

*/

publicstaticString getPersonAgeByBirthDate(String strBirthDate){

if("".equals(strBirthDate) || strBirthDate ==null){

return"";

}

//读取当前日期

Calendar c = Calendar.getInstance();

intyear = c.get(Calendar.YEAR);

intmonth = c.get(Calendar.MONTH)+1;

intday = c.get(Calendar.DATE);

//计算年龄

intage = year - Integer.parseInt(strBirthDate.substring(0,4)) -1;

if(Integer.parseInt(strBirthDate.substring(5,7)) 

age++;

} elseif(Integer.parseInt(strBirthDate.substring(5,7))== month && Integer.parseInt(strBirthDate.substring(8,10)) <= day){

age++;

}

returnString.valueOf(age);

}

/**

* 获取当前日期是星期几

* 0-"星期日", 1-"星期一", 2-"星期二", 3-"星期三", 4-"星期四", 5-"星期五", 6-"星期六"

* @param dt

* @return

*/

publicstaticintgetDayOfWeek(Date dt) {

Calendar cal = Calendar.getInstance();

cal.setTime(dt);

intdayOfWeek = cal.get(Calendar.DAY_OF_WEEK) -1;

returndayOfWeek;

}

/**

* 获取当前日期是星期几

* 0-"星期日", 1-"星期一", 2-"星期二", 3-"星期三", 4-"星期四", 5-"星期五", 6-"星期六"

* @param: strDate

*

*/

publicstaticintgetDayOfWeek(String strDate){

String format="yyyy-MM-dd";

//可以方便地修改日期格式

SimpleDateFormat dateFormat = newSimpleDateFormat(format);

Date date = null;

try{

date = dateFormat.parse(strDate);

Calendar c = Calendar.getInstance();

c.setTime(date);

intdayOfWeek=c.get(Calendar.DAY_OF_WEEK)-1;;

returndayOfWeek;

} catch(ParseException e) {

e.printStackTrace();

}

return-1;

}

/**

* 获取系统当前时间

*/

publicstaticString getSysTime(){

Date now = newDate();

//可以方便地修改日期格式

SimpleDateFormat dateFormat = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String sysDate = dateFormat.format(now);

returnsysDate;

}

/**

* 获取系统当前时间

*/

publicstaticString getSysDate(){

Date now = newDate();

SimpleDateFormat dateFormat = newSimpleDateFormat("yyyy-MM-dd");

String sysDate = dateFormat.format(now);

returnsysDate;

}

/**

* 按格式获取系统当前时间

*

* @param: format

*

*/

publicstaticString getSysDate(String format){

Date now = newDate();

//可以方便地修改日期格式

SimpleDateFormat dateFormat = newSimpleDateFormat(format);

String sysDate = dateFormat.format(now);

returnsysDate;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值