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.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Leave a Reply


5 × = fifteen



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books