java计算年龄

package webdemo;



import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.crypto.Data;

public class Main {
	
	//时间 出生日期计算年龄
	public static void main(String[] args) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd");
		String dates = sdf.format(date);
		String birth = "2000 04 25";
		int age = Integer.valueOf(Integer.valueOf(dates.substring(0, 4))- Integer.valueOf(birth.substring(0,4)));
		if(dates.compareTo(birth)<0) 
			age--;
		System.out.println("age : " + age);
		
	}
}

 

 

你可能感兴趣的:(Java合集)