How To Make A Formatted Date In Java


This instructional exercise 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.

import java.text.*; // use to access DateFormat and SimpleDateFormat class
import javax.swing.*; //used to access JFrame,JLabel,JPanel, JTextField, and JFormattedTextField class
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..

JFrame frame = new JFrame("Date Formatter");
JLabel label = new JLabel("Input Date:");

 

To create a formatted date to display we will use the DateFormat,SimpleDateFormat, and DateFormatter class.

DateFormat displayFormat = new SimpleDateFormat("yyyy--MM--dd");
 DateFormatter displayFormatter = new DateFormatter(displayFormat);
 DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
 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 format could not be found. The DateFormatter variables will be put inside the DefaultFormatterFactory class. Copy this code below:

DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter,
    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.

JFormattedTextField txtInput = new JFormattedTextField(factory);
JPanel panel = new JPanel();

 

Limit the characters of the JFormattedTextField using the setColumn method.

Add the label and the formatted textfield in the panel using the add method.

panel.add(label);
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.

 
frame.getContentPane().add(panel,"North");
frame.getContentPane().add(new JTextField(),"Center");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

 


Output:

output


Here’s the full code of this tutorial:

import java.text.*; // use to access DateFormat and SimpleDateFormat class
import javax.swing.*; //used to access JFrame,JLabel,JPanel, JTextField, and JFormattedTextField class
import javax.swing.text.*;  //used to access DateFormatter, and DefaultFormatterFactory class
 
 
public class defaultFormatterFactory {
  public static void main(String args[]) {
     JFrame frame = new JFrame("Date Formatter");
    JLabel label = new JLabel("Input Date:");
 
    DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
    DateFormatter displayFormatter = new DateFormatter(displayFormat);
    DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
    DateFormatter editFormatter = new DateFormatter(editFormat);
 
    DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter,
        displayFormatter, editFormatter);
 
    JFormattedTextField input = new JFormattedTextField(factory);
    input.setColumns(30);
    JPanel panel = new JPanel();
    panel.add(label);
    panel.add(input);
    frame.getContentPane().add(panel,"North");
 
    frame.getContentPane().add(new JTextField(),"Center");
    frame.pack();
    frame.setVisible(true);
  }
}

 



Sharing is caring!

Facebook Comments

Leave a Reply