导航:首页 > 编程语言 > java如何计算年龄

java如何计算年龄

发布时间:2022-09-23 19:04:45

Ⅰ 用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代码计算全班同学年龄

计算全班同学年龄之和。
思路:从键盘上依次输入每个同学的年内龄,然后求和输出:容
import java.util.Scanner;
public class TestMain {

public static void main(String[] args) {
int sum=0;
Scanner in =new Scanner(System.in);
int n = in.nextInt();
sum+=n;
System.out.println(sum);
}
}

Ⅳ 请用java计算你的年龄 告诉我你是哪月哪日出生的

变量无非就是闰年和平年的天数,以及精确到闰年的月份和平年的月份天数不差1天,加个询问让使用者输入生日,并约束格式(1999-9-9)

Ⅳ Java 根据出生日期获得年龄

实现步骤:

1、获取当前时间

2、判断出生日期是否小于当前时间,如果大于,则引发一场

3、从当前时间中取出年、月、日;从出生日期中取出年、月、日,年份相减

4、然后做具体判断

示例代码如下:

publicstaticintgetAge(DatebirthDay)throwsException{
//获取当前系统时间
Calendarcal=Calendar.getInstance();
//如果出生日期大于当前时间,则抛出异常
if(cal.before(birthDay)){
(
"ThebirthDayisbeforeNow.It'sunbelievable!");
}
//取出系统当前时间的年、月、日部分
intyearNow=cal.get(Calendar.YEAR);
intmonthNow=cal.get(Calendar.MONTH);
intdayOfMonthNow=cal.get(Calendar.DAY_OF_MONTH);

//将日期设置为出生日期
cal.setTime(birthDay);
//取出出生日期的年、月、日部分
intyearBirth=cal.get(Calendar.YEAR);
intmonthBirth=cal.get(Calendar.MONTH);
intdayOfMonthBirth=cal.get(Calendar.DAY_OF_MONTH);
//当前年份与出生年份相减,初步计算年龄
intage=yearNow-yearBirth;
//当前月份与出生日期的月份相比,如果月份小于出生月份,则年龄上减1,表示不满多少周岁
if(monthNow<=monthBirth){
//如果月份相等,在比较日期,如果当前日,小于出生日,也减1,表示不满多少周岁
if(monthNow==monthBirth){
if(dayOfMonthNow<dayOfMonthBirth)age--;
}else{
age--;
}
}
System.out.println("age:"+age);
returnage;
}

Ⅵ 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+"岁");

}

}

Ⅶ 年龄自动计算公式

第一步、把已经知道的统计到了一个年月日信息,先在右旁设置一个单元格格式,数值,0位数。

Ⅷ java计算年龄

import java.util.Calendar;
import java.util.Date;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;

public class H {
public static void main(String args[]) {
new Time("年龄计算器");

}
}

class Time extends Frame implements ActionListener {
Calendar calendar;
Button button;
TextField t1, t2, t3;
Label l, l1, l2, l3;

Time(String s) {
super(s);
setLayout(new FlowLayout());
button = new Button("确定");
button.addActionListener(this);
t1 = new TextField(2);
t2 = new TextField(2);
t3 = new TextField(2);
l = new Label(" 请输入您的生日 ");
l.setBackground(Color.cyan);
l1 = new Label("年");
l2 = new Label("月");
l3 = new Label("日");
add(l);
add(t1);
add(l1);
add(t2);
add(l2);
add(t3);
add(l3);
add(button);
setBounds(100, 100, 280, 100);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
validate();
}

public void actionPerformed(ActionEvent e) {
calendar = Calendar.getInstance();
calendar.setTime(new Date());
NumberFormat f = NumberFormat.getInstance();
long time = calendar.getTimeInMillis();
if (e.getSource() == button) {
try {
int n = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int r = Integer.parseInt(t3.getText());
calendar.set(n, y - 1, r);
double time1 = calendar.getTimeInMillis();
double c = (time - time1) / (1000 * 60 * 60 * 24);
double d = c/365;
f.setMaximumFractionDigits(2);
String s = f.format(d);
l.setText("您的年龄约为" + s + " 岁");
} catch (NumberFormatException ee) {
l.setText("请正确输入");
}
}
}
}

功底浅薄,如果有问题,还望指教。

Ⅸ 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一段计算年龄代码如何用最简化的方式写

可以用循环啊,定义类person
Class person{

@Getter
@Setter

private int age;
@Getter
@Setter
private String name;

public person(int age,String name){
this.age=age;
this.name=name.

}

}

判断年龄就用数组做了,假设你放一个ArrayList里
ArrayList<Person> personList=new ArrayList();
personList.add(父亲)
personList.add(爷爷)
personList.add(儿子)
for(Person person:personList){
if (person.getage>90){
game over

}else if(person.getage<0){
洗洗睡内吧

}else{
打印容名字和年龄 getname()+getage()

}

}

阅读全文

与java如何计算年龄相关的资料

热点内容
闪送员是哪里的app 浏览:530
火车站进站闸机的数据哪里可以查 浏览:503
cad备份文件清理软件 浏览:822
夹具装配图cad文件百度网盘 浏览:567
如何将excel表格转成文件包 浏览:1
网络配置文件应该怎么设置 浏览:886
苹果全能文件王下载位置 浏览:700
中国知网是哪些数据库 浏览:280
fastjson优点 浏览:302
mstcam数控铣床编程如何串连 浏览:502
d4252用什么软件编程 浏览:35
大学生如何参与大数据 浏览:779
autocad3维教程 浏览:2
港澳台版本有什么区别 浏览:263
java四个月能学到什么 浏览:46
开发板和linux文件 浏览:202
appstore外国帐号怎么看预约游戏 浏览:137
有什么免费加速网站的cdn 浏览:781
哪个文件存在最安全 浏览:199
淘宝导航栏分割线代码 浏览:271

友情链接