java计算年龄
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java计算年龄相关的知识,希望对你有一定的参考价值。
Java根据生日计算年龄,要保留两位小数,各位大侠帮帮忙啊
换个说法就是两个日期相减(日期形式:YYYY—MM—DD)得到的年数要精确到2位小数
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("请正确输入");
功底浅薄,如果有问题,还望指教。 参考技术A private static String dealAge(int days, Date bornDate)
String age = "";
if(days < 0 || bornDate == null)
return "-";
Date currentDate = DateTool.getNextDate(bornDate, days); //当前日期
Calendar from = Calendar.getInstance();
from.setTime(bornDate);
Calendar to = Calendar.getInstance();
to.setTime(currentDate);
int fromYear = from.get(Calendar.YEAR);
int fromMonth = from.get(Calendar.MONTH);
int fromDay = from.get(Calendar.DAY_OF_MONTH);
int toYear = to.get(Calendar.YEAR);
int toMonth = to.get(Calendar.MONTH);
int toDay = to.get(Calendar.DAY_OF_MONTH);
int year = toYear - fromYear;
int month = toMonth - fromMonth;
int day = toDay - fromDay;
if(month < 0)
year += -1;
month += 12;
if(day < 0 )
month += -1;
Calendar lastMonth = Calendar.getInstance();
lastMonth.setTime(DateTool.getNextMonth(currentDate, -1));
day += lastMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
// System.out.println("bornDate="+DateTool.getFullDate().format(bornDate));
// System.out.println("currentDate="+DateTool.getFullDate().format(currentDate));
// System.out.println("oneYear="+DateTool.getFullDate().format(oneYear));
// System.out.println("year="+year);
// System.out.println("month="+month);
// System.out.println("day="+day);
if(year < 1) //小于1岁
if(month > 0)
age += month + "个月";
if(day >= 0)
if(StringUtils.isNotBlank(age))
age += "零" + day + "天";
else
age += day + "天";
else
age += year + "岁";
if(month > 0)
age += month + "个月";
else
if(day > 0)
age += "零" + day + "天";
return age;
参考技术B String birthDay = "1990-01-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//得到当前的年份
String cYear = sdf.format(new Date()).substring(0,4);
//得到生日年份
String birthYear = birthDay .substring(0,4);
int age = Integer.parseInt(cYear) - Integer.parseInt(birthYear);
//不会产生小数啊。 参考技术C 如果不是要求特别精确地话:求出两日期相差天数,将日期格式化(YYYYMMDD),再将格式化 后的日期转化为数值再除以365就ok了 参考技术D SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date=new Date();
java.util.Date mydate= myFormatter.parse("2003-05-01");
long day=(date.getTime()-mydate.getTime())/(24*60*60*1000) + 1;
String year=new java.text.DecimalFormat("#.00").format(day/365f);
System.out.println("niu:" + year);本回答被提问者和网友采纳
以上是关于java计算年龄的主要内容,如果未能解决你的问题,请参考以下文章