导航:首页 > 编程语言 > 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根据出生日期判断是否成年相关的资料

热点内容
云闪付可以适用于哪些app 浏览:210
jsp循环输出44的表格 浏览:772
win10有哪些必装软件 浏览:151
xml文件打开是空白 浏览:79
snifferpro使用教程 浏览:391
各部门统计数据要统一该怎么表达 浏览:405
bat文件名汇总到excel 浏览:220
如何使用机器语言去编程 浏览:244
审计要学哪些编程帮助工作 浏览:988
qq账号注销要去哪个网站 浏览:337
安装在手机里的app怎么分解 浏览:865
新主板做xp系统教程 浏览:751
linux访问php文件 浏览:58
光驱数据线连接在主板哪里 浏览:577
85版本怎么样赚钱快 浏览:14
计算机网络信息保密技术 浏览:855
jsonformat注解用法 浏览:697
设置ipad需要选择备份文件路径 浏览:309
电脑文件怎么保存到桌面不卡 浏览:810
java获得本机外网的ip地址 浏览:391

友情链接