-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathDateFormatTest.java
More file actions
161 lines (150 loc) · 5.58 KB
/
Copy pathDateFormatTest.java
File metadata and controls
161 lines (150 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package dateFormat;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
/**
* This program demonstrates formatting dates under various locales.
* @version 1.13 2007-07-25
* @author Cay Horstmann
*/
public class DateFormatTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new DateFormatFrame();
frame.setTitle("DateFormatTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
/**
* This frame contains combo boxes to pick a locale, date and time formats, text fields to display
* formatted date and time, buttons to parse the text field contents, and a "lenient" check box.
*/
class DateFormatFrame extends JFrame
{
private Locale[] locales;
private Date currentDate;
private Date currentTime;
private DateFormat currentDateFormat;
private DateFormat currentTimeFormat;
private JComboBox<String> localeCombo = new JComboBox<>();
private JButton dateParseButton = new JButton("Parse date");
private JButton timeParseButton = new JButton("Parse time");
private JTextField dateText = new JTextField(30);
private JTextField timeText = new JTextField(30);
private JCheckBox lenientCheckbox = new JCheckBox("Parse lenient", true);
private EnumCombo dateStyleCombo = new EnumCombo(DateFormat.class, "Default",
"Full", "Long", "Medium", "Short");
private EnumCombo timeStyleCombo = new EnumCombo(DateFormat.class, "Default",
"Full", "Long", "Medium", "Short");
public DateFormatFrame()
{
setLayout(new GridBagLayout());
add(new JLabel("Locale"), new GBC(0, 0).setAnchor(GBC.EAST));
add(new JLabel("Date style"), new GBC(0, 1).setAnchor(GBC.EAST));
add(new JLabel("Time style"), new GBC(2, 1).setAnchor(GBC.EAST));
add(new JLabel("Date"), new GBC(0, 2).setAnchor(GBC.EAST));
add(new JLabel("Time"), new GBC(0, 3).setAnchor(GBC.EAST));
add(localeCombo, new GBC(1, 0, 2, 1).setAnchor(GBC.WEST));
add(dateStyleCombo, new GBC(1, 1).setAnchor(GBC.WEST));
add(timeStyleCombo, new GBC(3, 1).setAnchor(GBC.WEST));
add(dateParseButton, new GBC(3, 2).setAnchor(GBC.WEST));
add(timeParseButton, new GBC(3, 3).setAnchor(GBC.WEST));
add(lenientCheckbox, new GBC(0, 4, 2, 1).setAnchor(GBC.WEST));
add(dateText, new GBC(1, 2, 2, 1).setFill(GBC.HORIZONTAL));
add(timeText, new GBC(1, 3, 2, 1).setFill(GBC.HORIZONTAL));
locales = (Locale[]) DateFormat.getAvailableLocales().clone();
Arrays.sort(locales, new Comparator<Locale>()
{
public int compare(Locale l1, Locale l2)
{
return l1.getDisplayName().compareTo(l2.getDisplayName());
}
});
for (Locale loc : locales)
localeCombo.addItem(loc.getDisplayName());
localeCombo.setSelectedItem(Locale.getDefault().getDisplayName());
currentDate = new Date();
currentTime = new Date();
updateDisplay();
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
updateDisplay();
}
};
localeCombo.addActionListener(listener);
dateStyleCombo.addActionListener(listener);
timeStyleCombo.addActionListener(listener);
dateParseButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String d = dateText.getText().trim();
try
{
currentDateFormat.setLenient(lenientCheckbox.isSelected());
Date date = currentDateFormat.parse(d);
currentDate = date;
updateDisplay();
}
catch (ParseException e)
{
dateText.setText("Parse error: " + d);
}
catch (IllegalArgumentException e)
{
dateText.setText("Argument error: " + d);
}
}
});
timeParseButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String t = timeText.getText().trim();
try
{
currentDateFormat.setLenient(lenientCheckbox.isSelected());
Date date = currentTimeFormat.parse(t);
currentTime = date;
updateDisplay();
}
catch (ParseException e)
{
timeText.setText("Parse error: " + t);
}
catch (IllegalArgumentException e)
{
timeText.setText("Argument error: " + t);
}
}
});
pack();
}
/**
* Updates the display and formats the date according to the user settings.
*/
public void updateDisplay()
{
Locale currentLocale = locales[localeCombo.getSelectedIndex()];
int dateStyle = dateStyleCombo.getValue();
currentDateFormat = DateFormat.getDateInstance(dateStyle, currentLocale);
String d = currentDateFormat.format(currentDate);
dateText.setText(d);
int timeStyle = timeStyleCombo.getValue();
currentTimeFormat = DateFormat.getTimeInstance(timeStyle, currentLocale);
String t = currentTimeFormat.format(currentTime);
timeText.setText(t);
}
}