導航:首頁 > 編程語言 > java根據出生日期判斷是否成年

java根據出生日期判斷是否成年

發布時間:2024-05-21 17:48:36

java 輸入生日年齡 然後算出幾歲 最好能把具體代碼發過來

以下代碼是關於年齡計算的 其中不包含正則判斷部分,如果有什麼問題可以再交流
希望可以幫到你~

package api;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
/**
* 業務需求輸入一個生日(字元串),
* 返回該生日到系統時間的時間間隔
* @author Administrator
*
*/
public class BirthDate {
public static void main(String[] args) throws ParseException {
//創建Scanner
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入生日(格式為yyyy-MM-dd):");
String BirthDate = scanner.nextLine();
//將字元串轉換為Date類型
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
date = sdf.parse(BirthDate);
//使用calendar進行計算
Calendar calendar = Calendar.getInstance();
//獲取當前時間毫秒值
long now = (new Date()).getTime();
long Birthdate = date.getTime();
long time = now-Birthdate;
int count=0;
//時間換算
long days = time/1000/60/60/24;
//判斷閏年
int birthYear = Integer.parseInt(( BirthDate.substring(0, 4)));
for(int i=calendar.get(Calendar.YEAR);i>=birthYear;i--)
if((i%4==0 && !(i%100==0)) ||
(i%400==0) ){
count++;
}
//加入閏年因素進行整理換算
int age = ((int)days-count)/365;
System.out.println("到目前為止,活了"+age+"歲");

}

}

② java紼嬪簭鎬庝箞鍒ゆ柇韜浠借瘉鏄涓嶆槸鏈鎴愬勾

鎶婅韓浠借瘉鍙7-14浣嶅彇鍑烘潵錛屽姞涓18騫達紝璺熶粖澶╁仛姣旇緝錛屽垽鏂浠婂ぉ鏄涓嶆槸鍦ㄦや箣鍚

publicstaticvoidmain(String[]args){
try{
System.out.println(ifGrown_up("xxxxxx19970629xxxx"));
}catch(ParseExceptione){
e.printStackTrace();
}
}

publicstaticbooleanifGrown_up(Stringnum)throwsParseException{
intyear=Integer.parseInt(num.substring(6,10));
SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMdd");
Dateupdate=sdf.parse(String.valueOf(year+18)+num.substring(10,14));
Datetoday=newDate();
returntoday.after(update);
}

③ Java 根據出生日期獲得年齡

String
t1
=
現在實際日期.replace('-','/');
String
t2
=
出生日期.replace('-','/');
Date
dt1=
new
Date(t1);
Date
dt2=
new
Date(t2);
long
i=
(dt1.getTime()
-
dt2.getTime())/(1000*60*60*24);
//i就是總天數了,之後你除個365
就是歲數,
余數就是天數
要算月的話就把余數再除以12
我的這個日期的格式是2008-11-22
實歲就是現在的年減去出生的年呀,虛歲是實歲再加1
這個天數就是能比較精確了吧

④ 用java寫用戶在控制台按照「yyyy/mm/dd」的格式輸入出生日期,請計算用戶的年齡

年齡就是把當前的年份與用戶的年份相減得到一個對象值1。然後將用戶輸入日期中的年份換成當年的,組成一個新的日期,將這新的日期與當天的日期進行比較,得到另一個對象值2。這個對象值2就是距離用戶的生日的天數。這天數是正,那對象值1就是用戶的年齡,是負把對象值+1就好。參考兩日期之間的天數差方法:
public static int getDiffDay(String firstString, String secondString) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date firstDate = null;
Date secondDate = null;
try {
firstDate = df.parse(firstString);
secondDate = df.parse(secondString);
} catch (Exception e) {
// 日期型字元串格式錯誤
}
int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
return nDay;
}

⑤ 用java實現年齡計算器,就是在界面上輸入一個人出生年月日(yyyy-mm-dd),顯示現在多少歲,假設一年有365天

這是一段javascript寫的,無論是java或javascript原理都一樣,進攻參考
<!-- Begin
function run() {
with (document.agecalc) {
dd = parseInt(day.selectedIndex) + 1;
mm = parseInt(month.selectedIndex) + 1;
yy = year.value;
if (yy.length != 4 || isNaN(yy)) {
document.agecalc.timealive.value = "請輸入4位數的年份.";
document.agecalc.year.select();
document.agecalc.year.focus();
return;
}
}
days = new Date();
gdate = days.getDate();
gmonth = days.getMonth();
gyear = days.getYear();
if (gyear < 2000) gyear += 1900;
age = gyear - yy;
if ((mm == (gmonth + 1)) && (dd <= parseInt(gdate))) {
age = age;
} else {
if (mm <= (gmonth)) {
age = age;
} else {
age = age - 1;
}
}
if (age == 0)
age = age;
document.agecalc.timealive.value = "你已經滿了 " + age+ " 周歲 . . .\n\n";
if (mm <= (gmonth + 1))
age = age - 1;
if ((mm == (gmonth + 1)) && (dd > parseInt(gdate)))
age = age + 1;
var m;
var n;
if (mm == 12) n = 31 - dd;
if (mm == 11) n = 61 - dd;
if (mm == 10) n = 92 - dd;
if (mm == 9) n = 122 - dd;
if (mm == 8) n = 153 - dd;
if (mm == 7) n = 184 - dd;
if (mm == 6) n = 214 - dd;
if (mm == 5) n = 245 - dd;
if (mm == 4) n = 275 - dd;
if (mm == 3) n = 306 - dd;
if (mm == 2) { n = 334 - dd; if (leapyear(yy)) n++; }
if (mm == 1) { n = 365 - dd; if (leapyear(yy)) n++; }
if (gmonth == 1) m = 31;
if (gmonth == 2) {
m = 59;
if (leapyear(gyear)) m++;
}
if (gmonth == 3) { m = 90; if (leapyear(gyear)) m++; }
if (gmonth == 4) { m = 120; if (leapyear(gyear)) m++; }
if (gmonth == 5) { m = 151; if (leapyear(gyear)) m++; }
if (gmonth == 6) { m = 181; if (leapyear(gyear)) m++; }
if (gmonth == 7) { m = 212; if (leapyear(gyear)) m++; }
if (gmonth == 8) { m = 243; if (leapyear(gyear)) m++; }
if (gmonth == 9) { m = 273; if (leapyear(gyear)) m++; }
if (gmonth == 10) { m = 304; if (leapyear(gyear)) m++; }
if (gmonth == 11) { m = 334; if (leapyear(gyear)) m++; }
if (gmonth == 12) { m = 365; if (leapyear(gyear)) m++; }
months = age * 12;
months += 12 - parseInt(mm);
months += gmonth;
totdays = (parseInt(age) * 365);
totdays += age / 4;
totdays = parseInt(totdays) + gdate + m + n;
if (gmonth == 1) p = 31 + gdate;
if (gmonth == 2) {
p = 59 + gdate;
if (leapyear(gyear)) m = m+1;
}
if (gmonth == 3) { p = 90 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 4) { p = 120 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 5) { p = 151 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 6) { p = 181 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 7) { p = 212 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 8) { p = 243 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 9) { p = 273 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 10) { p = 304 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 11) { p = 334 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 12) { p = 365 + gdate; if (leapyear(gyear)) p++; }
weeks = (age * 365) + n + p;
weeks = weeks / 7;
etcdays = parseFloat(weeks) - parseInt(weeks);
etcdays = Math.round(etcdays * 7);
weeks = parseInt(weeks);
etcdays += parseInt(age / 4);
if (etcdays > 7)
weeks += parseInt(etcdays / 7);
document.agecalc.timealive.value += " 或 " + months + " 月齡\n";
document.agecalc.timealive.value += " 或 " + weeks + " 周齡\n";
document.agecalc.timealive.value += " 或 " + totdays + " 日齡\n";
var time = new Date();
ghour = time.getHours();
gmin = time.getMinutes();
gsec = time.getSeconds();
hour = ((age * 365) + n + p) * 24;
hour += (parseInt(age / 4) * 24);
document.agecalc.timealive.value += " 或 " + hour + " 小時\n";
var min = (hour * 60) + gmin;
document.agecalc.timealive.value += " 或 " + min + " 分鍾\n";
sec = (min * 60) + gsec;
document.agecalc.timealive.value += " 或 " + sec + " 秒鍾";
mm = mm - 1;
var r;
if (mm == 0) r = 0;
if (mm == 1) r = 31;
if (mm == 2) { r = 59; if (leapyear(gyear)) m++; }
if (mm == 3) { r = 90; if (leapyear(gyear)) r++; }
if (mm == 4) { r = 120; if (leapyear(gyear)) r++; }
if (mm == 5) { r = 151; if (leapyear(gyear)) r++; }
if (mm == 6) { r = 181; if (leapyear(gyear)) r++; }
if (mm == 7) { r = 212; if (leapyear(gyear)) r++; }
if (mm == 8) { r = 243; if (leapyear(gyear)) r++; }
if (mm == 9) { r = 273; if (leapyear(gyear)) r++; }
if (mm == 10) { r = 304; if (leapyear(gyear)) r++; }
if (mm == 11) { r = 334; if (leapyear(gyear)) r++; }
mm = mm + 1;
r = parseInt(r) + parseInt(dd);

if ((mm >= (gmonth + 1)) && (dd > gdate)) {
bday = r - m - gdate;
}
else {
if ((leapyear(gyear)) && ((mm > 2) && (dd < 29))) {
a = 366;
} else {
a = 365;
}
bday = a + (r - m - gdate);
}
nhour = 24 - parseInt(ghour);
nmin = 60 - parseInt(gmin);
nsec = 60 - parseInt(gsec);
while (bday > 366) bday -= 365;
if (((bday == 366) && (leapyear(gyear)) || ((bday == 365) && (!leapyear(gyear))))) {
document.agecalc.timealive.value += "\n\nSenlon祝賀您!今天是你的生日!祝你生日快樂!";
} else {
document.agecalc.timealive.value += "\n\n另外,你下一個生日距今還有:\n"
+ bday + " 天 " + nhour + " 小時 " + nmin + " 分 " + nsec + " 秒";
setTimeout("run()", 1000);
}
}
function leapyear(a) {
if (((a%4 == 0) && (a%100 != 0)) || (a%400 == 0))
return true;
else
return false;
}
// End -->
來自 http://www.dragon-guide.net/hangye/birthday.htm

⑥ java根據孩子生日,比如1979-05-13,怎麼轉換孩子年齡為幾歲幾月幾周,比如一歲10月兩周

publicclassTestDate{
/**
*獲取現在時間
*/
(){
DatecurrentTime=newDate();
SimpleDateFormatformatter=newSimpleDateFormat("yyyy-MM-dd");
StringdateString=formatter.format(currentTime);
returndateString;
}

/**
*兩個時間之間的天數
*/
publicstaticlonggetDays(Stringdate1,Stringdate2){
if(date1==null||date1.equals(""))
return0;
if(date2==null||date2.equals(""))
return0;
//轉換為標准時間
SimpleDateFormatmyFormatter=newSimpleDateFormat("yyyy-MM-dd");
java.util.Datedate=null;
java.util.Datemydate=null;
try{
date=myFormatter.parse(date1);
mydate=myFormatter.parse(date2);
}catch(Exceptione){
}
longday=(date.getTime()-mydate.getTime())/(24*60*60*1000);
returnMath.abs(day);
}
publicstaticvoidmain(String[]args){
longday=getDays("1979-04-13",getStringDate());
longyear=day/365;
longmonth=(day-365L*year)/30+1;
longweek=day/7+1;
System.out.println(year+"歲"+month+"月"+week+"周");
}
}

由於沒考慮閏年閏月,不是那麼准確,另外題目對於周的顯示上是指一個月的第幾周還是總共多少周?

樓下JAVA軟體工程師孫成 截取字元串的方法也很不錯,但在計算周有錯誤,不應該用%,改成Math.abs((stopz +30)- (startz+30))/7+1

⑦ 用java代碼通過出生時間和當前時間如何計算年齡

自己寫啊 很簡單的

類方法中帶2個參數

當前時間 減去 出生時間

⑧ Java出生日期應該用哪個類,年齡怎麼獲得

importjava.io.UnsupportedEncodingException;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;

publicclassTests{
publicstaticvoidmain(String[]args){
=newSimpleDateFormat("yyyy-MM-dd");
try{
Personperson=newPerson("Jack",0,format.parse("1990-01-01"));
Calendarcal=Calendar.getInstance();
longl=cal.getTimeInMillis();
longa=l-person.getBirthday().getTime();
longb=a/1000/60/60/24;//換算為天
System.out.println(b/365+"歲");
}catch(ParseExceptione){
e.printStackTrace();
}
}


}
classPerson{
privateStringname;
privateintsex;
privateDatebirthday;


publicPerson(Stringname,intsex,Datebirthday){
super();
this.name=name;
this.sex=sex;
this.birthday=birthday;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetSex(){
returnsex;
}
publicvoidsetSex(intsex){
this.sex=sex;
}
publicDategetBirthday(){
returnbirthday;
}


}

閱讀全文

與java根據出生日期判斷是否成年相關的資料

熱點內容
文件系統鎖定怎樣解除 瀏覽:191
applepay綁定設備 瀏覽:396
d盤的壓縮文件如何解壓 瀏覽:750
哪個編程軟體適合新手 瀏覽:952
在桌面建造一個文件夾 瀏覽:683
java中文簡繁體轉換工具 瀏覽:157
c好看的登陸界面代碼 瀏覽:622
系統自帶信息非默認程序 瀏覽:668
網站有專利兩個字被罰要多少錢 瀏覽:84
手機儲存文件的路徑 瀏覽:771
三作標需要什麼文件格式 瀏覽:585
該應用與手機中的版本簽名不一致 瀏覽:239
linux文件命名 瀏覽:480
win10如何開游戲模式 瀏覽:622
hacknet創建文件夾 瀏覽:730
什麼語言做app 瀏覽:922
應用數據哪些可清理 瀏覽:976
數據區域下移一行該怎麼辦 瀏覽:679
華為賬號用qq注冊賬號 瀏覽:327
台達plc編程完成怎麼試運行 瀏覽:412

友情鏈接