fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. }
  14. }
Success #stdin #stdout 0.07s 54584KB
stdin
import java.awt.*;
import java.awt.event.*;
import java.time.*;
import javax.swing.*;
public class AgeCalculator extends JFrame {
    private JLabel birthDateLabel, resultLabel;
    private JComboBox<Integer> dayOfBirth, monthOfBirth, yearOfBirth;
    private JButton calculateButton;
    public AgeCalculator() {
        setTitle("Age Calculator");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(5, 2));
        birthDateLabel = new JLabel("Enter your birth date:");
        resultLabel = new JLabel("Your age will be displayed here.");
        Integer[] days = new Integer[31];
        for (int i = 1; i <= 31; i++) days[i - 1] = i;
        Integer[] months = new Integer[12];
        for (int i = 1; i <= 12; i++) months[i - 1] = i;
        Integer[] years = new Integer[100];
        for (int i = 1920; i <= 2020; i++) years[i - 1920] = i;
        dayOfBirth = new JComboBox<>(days);
        monthOfBirth = new JComboBox<>(months);
        yearOfBirth = new JComboBox<>(years);
        calculateButton = new JButton("Calculate Age");
        calculateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int day = (int) dayOfBirth.getSelectedItem();
                int month = (int) monthOfBirth.getSelectedItem();
                int year = (int) yearOfBirth.getSelectedItem();
                LocalDate birthDate = LocalDate.of(year, month, day);
                LocalDate currentDate = LocalDate.now();
                Period age = Period.between(birthDate, currentDate);
                resultLabel.setText("You are " + age.getYears() + " years, " + age.getMonths() + " months, and " + age.getDays() + " days old.");
            }
        });
        add(birthDateLabel);
        add(new JLabel(""));
        add(new JLabel("Day:"));
        add(dayOfBirth);
        add(new JLabel("Month:"));
        add(monthOfBirth)days;
stdout
Standard output is empty