Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.LocalDate;
- import static java.time.temporal.ChronoUnit.DAYS;
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.DatePicker;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.layout.HBox;
- import javafx.stage.Stage;
- /**
- *
- * @author Aamir khan
- */
- public class AgeCalculator extends Application {
- Label result = new Label("");
- @Override
- public void start(Stage window) throws Exception {
- HBox root = new HBox();
- root.setAlignment(Pos.CENTER);
- root.setStyle("-fx-padding:20px; -fx-spacing:10px");
- DatePicker birthDate = new DatePicker();
- Button calBtn = new Button("Calculate");
- calBtn.setDefaultButton(true);
- root.getChildren().addAll(new Label("Select your B'date"),birthDate, calBtn, result);
- calBtn.setOnAction(e -> {
- calculate(LocalDate.now(), birthDate.getValue());
- });
- window.setScene(new Scene(root, 800, 80));
- window.setTitle("How old Are You?");
- window.show();
- }
- private void calculate(LocalDate currentDate, LocalDate birthDate) {
- final long DAYS_BETWEEN = DAYS.between(birthDate, currentDate);
- final long YEARS = DAYS_BETWEEN / 365;
- final long MONTHS = (DAYS_BETWEEN % 365) / 30;
- String calculatedResult = String.format("You are %d Years and %d Months old", YEARS, MONTHS);
- result.setText(calculatedResult);
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Add Comment
Please, Sign In to add comment