string birthDay = "1980/1/1";
请问如何计算年龄
java.util.Date date = new java.util.Date();
int Age = date.getYear()- (int)senkoPrnInsei.getBirth().substring(0, 4));
计算方法不对
请各位给挑挑 谢谢
请问如何计算年龄
java.util.Date date = new java.util.Date();
int Age = date.getYear()- (int)senkoPrnInsei.getBirth().substring(0, 4));
计算方法不对
请各位给挑挑 谢谢
import java.util.Calendar;public class Main { public static void main(String[] args) {
String birthday = "1980/1/1";
Calendar calendar = Calendar.getInstance();
int birth = Integer.parseInt(birthday.substring(0, 4));
int now = calendar.get(Calendar.YEAR);
int age = now - birth;
System.out.println(age);
}
}
import java.util.Date;class test
{
public static void main(String[] args)
{
Date date=new Date();
int i=date.getYear()-(1980-1900);
System.out.println(i);
}
}
另外:getYear已经过时了,是以1900年为标准的
public class age { @SuppressWarnings("deprecation")
public static void main(String[] args){
try{
System.out.println("*****年龄计算******");
System.out.print("请输入年龄(年/月/日):");
DataInputStream di = new DataInputStream(System.in);
StringTokenizer stk = new StringTokenizer(di.readLine(),"/");
int year = Integer.parseInt(stk.nextToken());
int month = Integer.parseInt(stk.nextToken());
int day = Integer.parseInt(stk.nextToken());
Date date = new Date();
System.out.println(date.getYear());
int age = 0;
if(month == date.getMonth()){
if(day == date.getDay()){
age = date.getYear()-year+1900+1;
}
}
else{
age = date.getYear()-year+1900;
}
System.out.println("Your age is "+age);
}catch(IOException io){
System.out.println("IOException "+io);
}
}
}
import java.util.*;;class Age{
public static void main(String[] args){
Age a=new Age();
a.CaculateAge();
}
public void CaculateAge(){
int age;
Date date= new Date();
int Birth= GetBirth();
age=date.getYear()-(Birth-1900);
System.out.print("your age is :"+age);
}
public int GetBirth(){
int Birth;
System.out.print("please input your birth :");
Scanner scanner = new Scanner(System.in);
Birth = scanner.nextInt();
return Birth;
}
}
import java.util.Date;
import java.text.SimpleDateFormat;
public class Age
{
public static void main(String[] args)
{
/*取到用户的年龄,并且把年和月、日分开。*/
String sr=JOptionPane.showInputDialog(null,"请输入您的8位数年龄!");
int i=Integer.parseInt(sr);
int year,month;
year=i/10000;
month=i%10000;
/*取到当前的日期,并且把年和月、日分开。*/
Date date=new Date();
SimpleDateFormat s=new SimpleDateFormat("yyyyMMdd");
String si=s.format(date);
int time=Integer.parseInt(si);
int newYear,newMonth;
newYear=time/10000;
newMonth=time%10000;
/*计算年龄。*/
int age;
if (newMonth>month)
{
age=newYear-year;
}
else
{
age=newYear-year-1;
}
System.out.println("您的年龄是:"+age+"岁!"); //输出。
}
}
我是Java新手
呵呵