How to have a Formatted Date in Java

This tutorial is about how to have a formatted date in java. We will use the DateFormatter and some text packages library of java here. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of defaultFormatterFactory.java. 2. Import the following package library.
  1. import java.text.*; // use to access DateFormat and SimpleDateFormat class
  2. import javax.swing.*; //used to access JFrame,JLabel,JPanel, JTextField, and JFormattedTextField class
  3. import javax.swing.text.*;  //used to access DateFormatter, and DefaultFormatterFactory class
3. Initialize your variable in your Main, variable frame for JFrame, and a Jlabel named label..
  1.     JFrame frame = new JFrame("Date Formatter");
  2.     JLabel label = new JLabel("Input Date:");
To create a formatted date to display we will use the DateFormat,SimpleDateFormat, and DateFormatter class.
  1.    DateFormat displayFormat = new SimpleDateFormat("yyyy--MM--dd");
  2.     DateFormatter displayFormatter = new DateFormatter(displayFormat);
  3.     DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
  4.     DateFormatter editFormatter = new DateFormatter(editFormat);'
The DateFormat class formats and parses dates or time and will use the SimpleDateFormat class to allow formatting of date to text, parsing text to date, and normalization. Now, we will use the DefaultFormatterFactory class to accommodate if a more specific formatter could not be found. The DateFormatter variables will be put inside the DefaultFormatterFactory class. Have this code below:
  1.     DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter,
  2.         displayFormatter, editFormatter);
4. Now, create JFormattedTextField component named input to have the formatted date be in the DefaultFormatterFactory be in the JFormattedTextField. Create also a JPanel to put their the JFormattedTextField.
  1.     JFormattedTextField txtInput = new JFormattedTextField(factory);
  2.     JPanel panel = new JPanel();
Limit the characters of the JFormattedTextField using the setColumn method.
  1. txtInput.setColumns(30);
Add the label and the formatted textfield in the panel using the add method.
  1.    panel.add(label);
  2.    panel.add(txtInput);
5. Lastly, add the panel to the frame, add a textfield to the frame, pack the frame, set its visibility to true, and have its close operation.
  1.    
  2.     frame.getContentPane().add(panel,"North");
  3.     frame.getContentPane().add(new JTextField(),"Center");
  4.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  5.     frame.pack();
  6.     frame.setVisible(true);
Output: output Here's the full code of this tutorial:
  1. import java.text.*; // use to access DateFormat and SimpleDateFormat class
  2. import javax.swing.*; //used to access JFrame,JLabel,JPanel, JTextField, and JFormattedTextField class
  3. import javax.swing.text.*;  //used to access DateFormatter, and DefaultFormatterFactory class
  4.  
  5.  
  6. public class defaultFormatterFactory {
  7.   public static void main(String args[]) {
  8.      JFrame frame = new JFrame("Date Formatter");
  9.     JLabel label = new JLabel("Input Date:");
  10.  
  11.     DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
  12.     DateFormatter displayFormatter = new DateFormatter(displayFormat);
  13.     DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
  14.     DateFormatter editFormatter = new DateFormatter(editFormat);
  15.    
  16.     DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter,
  17.         displayFormatter, editFormatter);
  18.  
  19.     JFormattedTextField input = new JFormattedTextField(factory);
  20.     input.setColumns(30);
  21.     JPanel panel = new JPanel();
  22.     panel.add(label);
  23.     panel.add(input);
  24.     frame.getContentPane().add(panel,"North");
  25.  
  26.     frame.getContentPane().add(new JTextField(),"Center");
  27.     frame.pack();
  28.     frame.setVisible(true);
  29.   }
  30. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment