計算出生年齡按規則輸出

2021-10-01 09:32:59 字數 2700 閱讀 6879

具體就是根據出生時間判斷是出生多久,並且按照出生的時間範圍決定輸出格式。

1星期內 : x天xx小時

1星期以上-1個月以內 : xx天

1個月以上-1周歲以內 : xx月xx天

1周歲以上-3周歲以內 : xx歲xx月

3周歲以上 : xx歲

重點:

1使用calendar類進行把出生時間和當前時間進行相減獲取出生時間。

2通過出生時間計算出出生的總天數,並且使用總天數來進行範圍的區分。

3考慮輸入是yyyy-mm-dd hh:mm:ss,使用擷取stringutils工具類。

calendar類 月份指標是0代表1月,1代表二月,潤平年的區分

commons-lang<

/groupid>

commons-lang<

/artifactid>

2.6<

/version>

<

/dependency>

public

static

void

main

(string[

] args)

throws parseexception

public

static string parseage

(string birthdayinfo)

if(month<0)

system.out.

println

("年齡:"

+year+

"年"+month+

"月"+day+

"天")

;//獲取潤平年

boolean isyear =

getisyear

(yearnow)

;//判斷閏年或者平年

if(isyear)

else

// 直接建立現在的日期

date nowdate =

newdate()

;// 將現在的日期轉換成字串

string now = ******dateformat.

format

(nowdate)

; system.out.

println

("今天的日期是"

+ now)

;// 將出生日期和當前日期轉換成毫秒值.gettime()

date olddate = null;

trycatch

(parseexception e)

string oldinputdate = ******dateformat.

format

(olddate)

; system.out.

println

("出生的日期是"

+ oldinputdate)

;long birthdaylong = olddate.

gettime()

; system.out.

println

(+birthdaylong)

;long nowday = nowdate.

gettime()

; system.out.

println

(+nowday)

;long time = nowday - birthdaylong;

system.out.

println

(+time)

;long daysum =

(time/

1000/60

/60/24

);// 除以計算得到出生天數

system.out.

println

("出生總天數:"

+daysum)

;long hour =

(time/

1000/60

/60);

// 除以計算得到出生小時

system.out.

println

("出生總小時:"

+hour)

;//使用出生天數進行區分(利用總天數和小時數用作範圍區分)

if(daysum <0)

else

if(daysum>

0&& daysum<=7)

else

if(daysum>

7&& daysum<=

getcurrentmonthday()

)else

if(daysum>=

getcurrentmonthday()

&&daysumelse

if(daysum>=yearselectnum&&daysumelse

if(daysum>=yearselectnum*3)

return outputstr;

}/**

* 獲取當月天數

* @return

*/public

static

long

getcurrentmonthday()

public

static

boolean

getisyear

(int year)

計算出生日期和年齡

根據月齡計算出生日期public class date 根據生日計算年齡 public class calbabyjoda catch exception e catch exception e1 system.out.println 寶寶從出生到 enddatetime.tostring yyyy...

根據資料庫中的出生年份計算年齡

在實際運用中我們會遇到在資料庫中加入使用者出生年份,而介面想要顯示年齡的需求。針對這個需求可以有多種實現方式,其中乙個是在資料庫查詢語句中計算,如下 select distinct u.cast date format now y as unsigned integer u.birth year a...

C 實驗2 計算出標準體重,輸出體重狀態

一 問題及 檔名稱 xyz.cpp 作 者 趙雲 完成日期 2016 年 03 月 22 日 版 本 號 v1.0 對任務及求解方法的描述部分 輸入描述 無 程式輸出 計算乙個人的標準體重,輸出體重狀態 問題分析 略 演算法設計 略 三 心得體會 編的過程中感覺還是有好多問題,過程中還改了好多次,但...