Java Reference
In-Depth Information
Format mediumItalian =
DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ITALIAN);
label = new JLabel("Medium Italian date:");
input = new JFormattedTextField(mediumItalian);
input.setValue(new Date());
input.setColumns(20);
panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel.add(label);
panel.add(input);
frame.add(panel);
Format dayOfWeek = new SimpleDateFormat("E", Locale.FRENCH);
label = new JLabel("French day of week:");
input = new JFormattedTextField(dayOfWeek);
input.setValue(new Date());
input.setColumns(20);
panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel.add(label);
panel.add(input);
frame.add(panel);
Format shortTime = DateFormat.getTimeInstance(DateFormat.SHORT);
label = new JLabel("Short time:");
input = new JFormattedTextField(shortTime);
input.setValue(new Date());
input.setColumns(20);
panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel.add(label);
panel.add(input);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Figure 16-6 shows an example of running the program. To start the program in a different
locale, you can set the user.language and user.country settings from the command line with a
command similar to the following:
java -Duser.language=fr -Duser.country=FR DateInputSample
However, this will alter the valid input only for those formats without a specific locale set.
Search WWH ::




Custom Search