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