/*
  输入年月日,判断多少岁
 */
 import javax.swing.*;
 import java.text.*;
 import java.util.*;
 class xxx
 {
 
 public static void main(String [] args)
 {
      String aaa,bbb,ccc; 
      int  a,year,mon,day,age,nyear,nmon,nday,nage,xage;
      Date today = new Date();
         GregorianCalendar cal = new GregorianCalendar();
         nyear = cal.get(Calendar.YEAR);
         nmon = cal.get(Calendar.MONTH);
         nday = cal.get(Calendar.DATE);
           aaa = JOptionPane.showInputDialog(null,"请输入你的生日的年份:");
       year = Integer.parseInt(aaa);
             bbb = JOptionPane.showInputDialog(null,"请输入你的生日月份:");
             mon  =Integer.parseInt(bbb);
           
            ccc = JOptionPane.showInputDialog(null,"请输入你的生日日期:");
               day = Integer.parseInt(ccc);
            
              if(nyear>year||nmon>=mon)
              {
               age = nyear-year;
            JOptionPane.showMessageDialog(null,"你的出生是:"+
                              year+"年"+mon+"月"+day+"日"+"\n"        
                                             +"你今年是:" + age+"岁");
              }
              else if(nyear>year||nmon>=mon||nday<=day)
              {
              nage = nyear-year-1;
              JOptionPane.showMessageDialog(null,"你的出生是:"+
                             year+"年"+mon+"月"+day+"日"+"\n"        
                                             +"你今年是:" + nage+"岁");
              }
              else
            {
               xage = nyear-year-1;
              JOptionPane.showMessageDialog(null,"你的出生是:"+
                              year+"年"+mon+"月"+day+"日"+"\n"        
                                             +"你今年是:" + xage+"岁");
              }
 } 
 }

解决方案 »

  1.   

    呵呵。。原来楼主是想算周岁啊。 我认为,可以用这样的算法实现:用当前年份减去出生年份,得到一个数 a.  那么,实际的年龄,要么是a,  
    要么是 a-1。 推算: 当实际年龄为a 时, 那么当前月>=生日月,并且当前天>=生日天。
    当实际年龄为a-1 时,则相反。 所以得到代码骨架://先计算出a
    a = 当前年- 出生年
    //再判断实际年龄是a,还是a-1
    if(当前月>=生日月&&当前天>=生日天){
        return a;
    }else{
        return a-1;
    }注:没有考虑到 1月29日等特殊情况。以上算法未经过测试。
      

  2.   

    try{
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    java.util.Date birthDate = df.parse("1982-12-23");


    long current = System.currentTimeMillis();

    Calendar c = Calendar.getInstance();
    c.setTime(birthDate);
    long temp = current-c.getTimeInMillis();

    double year = temp/(1000.0*60.0*60.0*24.0*365.0);

    System.out.print(year);
    }catch(Exception e)
    {}23.999758681031203没有验证润年,楼主可以判断下
      

  3.   

    Date today = new Date();
    GregorianCalendar cal = new GregorianCalendar();
    这里today都没有使用到,应该把today作为参数给cal吧
      

  4.   

    import java.text.*;
    import java.util.*;class Birthday {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.print("请输入你的生日(yyyy-mm-dd): ");
    Date birthday = parseBirthday(sc.nextLine());

    DateFormat format = DateFormat.getDateInstance();
    System.out.print("你的生日是: ");
    System.out.println(format.format(birthday));

    System.out.print("今天是: ");
    System.out.println(format.format(new Date()));

    int age = calculateAge(birthday);
    System.out.printf("你现在 %s 岁了.%n", age);
    }

    private static Date parseBirthday(String birth) {
    String parts[] = birth.split("-");
    int year = Integer.parseInt(parts[0]);
    int month = Integer.parseInt(parts[1]);
    int date = Integer.parseInt(parts[2]);
    GregorianCalendar birthday = new GregorianCalendar();
    birthday.set(Calendar.YEAR, year);
    birthday.set(Calendar.MONTH, month - 1);
    birthday.set(Calendar.DAY_OF_MONTH, date);
    return birthday.getTime();
    }

    private static int calculateAge(Date birth) {
    Calendar birthday = new GregorianCalendar();
    birthday.setTime(birth);
    Calendar today = new GregorianCalendar();
    int age = today.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
    if (!isBirthdayPassed(birthday, today))
    age = age - 1;
    return age;
    }

    private static boolean isBirthdayPassed(Calendar birthday, Calendar today) {
    if (birthday.get(Calendar.MONTH) < today.get(Calendar.MONTH))
    return true;
    if (birthday.get(Calendar.MONTH) == today.get(Calendar.MONTH) &&
    birthday.get(Calendar.DAY_OF_MONTH) <= today.get(Calendar.DAY_OF_MONTH))
    return true;
    return false;
    }
    }乱写的,楼主看看可不可以
      

  5.   

    看过勒
    逻辑混乱!
    不细说勒,你看看你写的if..else那套东西。自己好好检查一下。这种问题自己是可以解决的