A simple Java Age calculator

Source: Internet
Author: User

Make a calculator like age

According to the topic, I made a calendar class and the date of the month and the day of the age, of course, the correct way more than one, the following is my source code and results:

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 ("Age Calculation 1.0 Edition");
Mywindow.setsize (400, 70);
Mywindow.setlocation (350, 300);
Mywindow.setresizable (FALSE);
Mywindow.setlayout (New FlowLayout ());
JLabel Lab = new JLabel ("Birthday:");
Mywindow.add (Lab);
Text1 = new JTextField ("", 10);
Text2 = new JTextField ("", 10);
Mywindow.add (Text1);
JButton btn = new JButton ("calculation");
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 > | | Mymonth < 1) {
F = false;
Text2.settext ("date error");
}
if (Myday > | | Myday < 1) {
F = false;
Text2.settext ("date error");
}
if (Myyear < 0) {
F = false;
Text2.settext ("date error");
}
if (f) {
Getage (Myyear, Mymonth, myday);
}

} catch (Exception E1) {
Text2.settext ("date format error");
}

}

Age Calculation function
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 ("You are not yet born Yo");
}
if (b) {
years = Yearnow-year;
months = Monthnow-month;
days = Daynow-day;
String result = years + "Age 0" + months + "month" + Days + "Day";
Text2.settext (result);
}
}

}

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

Results:

A simple Java Age calculator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.