Desktop Java

JavaFX-Based SimpleDateFormat Demonstrator

One of the things that can be a little tricky for developers new to Java or even for experienced Java developers new to formatting with Java Dates, is the specification of a date/time format using SimpleDateFormat. The class-level Javadoc-based documentation for SimpleDateFormat is pretty thorough in its coverage of patterns representing various components of a date/time.
However, unless one carefully reads and understands these various patterns, it can be tricky to remember the difference between lowercase ‘d’ for day in the month and uppercase ‘D’ for day in the year or to remember if it’s lowercase ‘m’ or uppercase ‘M’ used for months versus minutes. In this post, I look at a simple application written in JavaFX that allows a developer to quickly try arbitrary patterns to see how SimpleDateFormat will render the current date/time given the arbitrary pattern. In theory, a developer could use this simple tool to quickly determine the effect of his or her date/time pattern, but it’s really more of an excuse to apply JavaFX.

The code listing below contains the complete JavaFX 2.x-based application.

package dustin.examples;

import java.text.SimpleDateFormat;
import java.util.Date;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFieldBuilder;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 * JavaFX application allowing for testing and demonstration of various String
 * formats for date/time.
 * 
 * @author Dustin
 */
public class DateTimeStringFormatDemonstrator extends Application
{
   /**
    * Generate the application's main pane.
    * 
    * @return Main pane for the application.
    */
   private Pane generateMainPane()
   {
      final VBox vbox = new VBox();
      final TextField dateTimeFormatField =
         TextFieldBuilder.create().prefWidth(350).alignment(Pos.CENTER)
                                  .promptText("Enter DateFormat")
                                  .build();
      vbox.getChildren().add(dateTimeFormatField);
      final TextField formattedDateField =
         TextFieldBuilder.create().prefWidth(350).alignment(Pos.BASELINE_CENTER)
                                  .promptText("Date Output Goes Here").build();
      formattedDateField.setEditable(false);
      final Button applyButton = new Button("Apply Format");
      applyButton.setPrefWidth(350);
      applyButton.setOnMousePressed(
         new EventHandler<MouseEvent>()
         {
            @Override
            public void handle(MouseEvent mouseEvent)
            {
               try
               {
                  final SimpleDateFormat sdf =
                     new SimpleDateFormat(dateTimeFormatField.getText());
                  formattedDateField.setText(sdf.format(new Date()));
                  formattedDateField.setAlignment(Pos.CENTER);
               }
               catch (Exception ex)
               {
                  formattedDateField.setText("ERROR");
                  formattedDateField.setAlignment(Pos.CENTER);
               }
               formattedDateField.setAlignment(Pos.BASELINE_CENTER);
            }
         });
      vbox.getChildren().add(applyButton);
      vbox.getChildren().add(formattedDateField);
      return vbox;
   }

   /**
    * The method overridden from Application for starting the application.
    * 
    * @param stage Primary stage.
    * @throws Exception Exceptions throwing during execution of JavaFX application.
    */
   @Override
   public void start(final Stage stage) throws Exception
   {
      stage.setTitle("JavaFX Date/Time String Format Presenter");
      final Group group = new Group();
      group.getChildren().add(generateMainPane());
      final Scene scene = new Scene(group, 350, 65, Color.DARKKHAKI);
      stage.setScene(scene);
      stage.show();
   }

   /**
    * Main function for running date/time format JavaFX application.
    * 
    * @param arguments Command-line arguments; none expected.
    */
   public static void main(final String[] arguments)
   {
      Application.launch(arguments);
   }
}
The simple JavaFX 2-based application shown above makes it easy to try out different date/time format patterns to see what SimpleDateFormat will do with each. A series of these used on the evening of Tuesday, 8 May 2012, are shown next. These examples demonstrate several key aspects of using SimpleDateFormat:
  • Uppercase ‘M’ is used for months while lowercase ‘m’ is used for minutes.
  • Number of ‘M’ characters represents month’s representation (example: 5, 05, or ‘May’ for May).
  • Uppercase ‘D’ is for the number of the day of the year (since January 1) while lowercase ‘d’ is the number of the day of the month (since May 1 in this case).
  • Two ‘y’ or ‘Y’ digits represent 2-digit year, but 3 or 4 ‘Y’ or ‘y’ digits can be used for a 4-digit year.
The simple example highlighted in this blog post demonstrates the simplicity of JavaFX and provides an example of how JavaFX can provide graphical interfaces to make Java applications more intuitive. As part of this, mouse event handling in JavaFX and the common JavaFX idiom of using builders are both demonstrated. A practical use of this application is to quickly and easily determine the representation that is provided by SimpleDateFormat for a given pattern.

Reference: JavaFX-Based SimpleDateFormat Demonstrator from our JCG partner Dustin Marx at the Inspired by Actual Events blog.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button