Date Calculation in JavaFX2.0


From following code you can easily get idea how to calculate date in JavaFX. JavaFX is fully based on java you can easily implement any java concept in your business logic.Here is sample code how to use all type of date calculation in javaFX.
// Comment

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datecalculation;

import java.awt.Label;
import java.util.Calendar;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author saibaba
 */
public class DateCalculation extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Date Calculation");
       Button btn = new Button();
       
       
        btn.setText("Calculate Date ");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                 Calendar cal = Calendar.getInstance();
             System.out.println("Today : " + cal.getTime());
  
         // Subtract 30 days from the calendar
         cal.add(Calendar.DATE, -30);
         System.out.println("30 days ago: " + cal.getTime());
  
         // Add 10 months to the calendar
         cal.add(Calendar.MONTH, 10);
         System.out.println("10 months later: " + cal.getTime());
  
         // Subtract 1 year from the calendar
         cal.add(Calendar.YEAR, -1);
         System.out.println("1 year ago: " + cal.getTime());
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}
Output
Today : Sun Jun 17 17:44:00 IST 2012
30 days ago: Fri May 18 17:44:00 IST 2012
10 months later: Mon Mar 18 17:44:00 IST 2013
1 year ago: Sun Mar 18 17:44:00 IST 2012


raja P Web Developer

9 comments:

Related Posts Plugin for WordPress, Blogger...