码迷,mamicode.com
首页 > 编程语言 > 详细

一个简单的java年龄计算器

时间:2015-04-18 23:22:22      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

制作一个如下图年龄计算器

 

技术分享

根据题目,我做了一个由Calendar类以及年月日各相减得到的年龄,当然正确的方法不止一个,以下为我的源代码和结果截图:

package com.Date;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Calendar;
import java.util.EmptyStackException;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class AgeCounts extends JFrame implements ActionListener {
JTextField text1, text2;
public static String str = "";

public static void main(String[] args) {
new AgeCounts();
}

public AgeCounts() {
JFrame mywindow = new JFrame();
mywindow.setTitle("年龄计算1.0版");
mywindow.setSize(400, 70);
mywindow.setLocation(350, 300);
mywindow.setResizable(false);
mywindow.setLayout(new FlowLayout());
JLabel lab = new JLabel("生日 :");
mywindow.add(lab);
text1 = new JTextField("", 10);
text2 = new JTextField("", 10);
mywindow.add(text1);
JButton btn = new JButton("计算");
mywindow.add(btn);
btn.addActionListener(this);
mywindow.add(text2);
text2.setEditable(false);
mywindow.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
boolean f = true;
str = text1.getText();
StringTokenizer st = new StringTokenizer(str, " ");
int myYear, myMonth, myDay;
try {
myYear = Integer.parseInt(st.nextToken());
myMonth = Integer.parseInt(st.nextToken());
myDay = Integer.parseInt(st.nextToken());
if (myMonth > 12 || myMonth < 1) {
f = false;
text2.setText("日期错误");
}
if (myDay > 31 || myDay < 1) {
f = false;
text2.setText("日期错误");
}
if (myYear < 0) {
f = false;
text2.setText("日期错误");
}
if (f) {
getAge(myYear, myMonth, myDay);
}

} catch (Exception e1) {
text2.setText("日期格式错误");
}

}

// 年龄计算函数
public void getAge(int year, int month, int day) {
boolean b = true;
Calendar cal = Calendar.getInstance();
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH) + 1;
int dayNow = cal.get(Calendar.DAY_OF_MONTH);
int years, months = 0, days = 0;
if (dayNow < day) {
dayNow = dayNow + 30;
monthNow = monthNow - 1;
}
if (monthNow < month) {
monthNow = monthNow + 12;
yearNow = yearNow - 1;
}
if (yearNow < year) {
b = false;
text2.setText("你还没有出生哟");
}
if (b) {
years = yearNow - year;
months = monthNow - month;
days = dayNow - day;
String result = years + "岁零" + months + "个月" + days + "天";
text2.setText(result);
}
}

}

----------------------------------------------------

结果截图:

技术分享

 

技术分享

 

技术分享

 

技术分享

一个简单的java年龄计算器

标签:

原文地址:http://www.cnblogs.com/wangxiuheng/p/4438159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!