-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateInputExample.java
More file actions
29 lines (26 loc) · 1 KB
/
Copy pathDateInputExample.java
File metadata and controls
29 lines (26 loc) · 1 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
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.*;
public class DateInputExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Date Input Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JFormattedTextField dateField = new JFormattedTextField(new SimpleDateFormat("yyyy-MM-dd"));
frame.add(new JLabel("Enter Date (yyyy-MM-dd):"));
frame.add(dateField);
JButton button = new JButton("Get Date");
button.addActionListener(e -> {
Date date = (Date) dateField.getValue();
if (date != null) {
JOptionPane.showMessageDialog(frame, "Entered Date: " + date);
} else {
JOptionPane.showMessageDialog(frame, "Invalid Date Format");
}
});
frame.add(button);
frame.pack();
frame.setVisible(true);
}
}