问题
I've spent a lot of time reading Java docs and related questions but am still unable to understand exactly what I need to do to accomplish what I want.
I know that I will be using a MaskFormatter
(because I want / /
always displayed), SimpleDateFormater
and DocumentListeners
so I spent most of my time studying those but can't figure out how to use everything together.
I have a JFormattedTextField
that uses a MaskFormatter("##/##/####")
which I want to end up being stored as a Date in the format of MM/dd/yyyy
.
private JFormattedTextField weekEndingData = new JFormattedTextField(createFormatter("##/##/####"));
private String dateString = weekEndingData.toString();
private SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
private MaskFormatter createFormatter(String s) {
MaskFormatter formatter = null;
try {
formatter = new MaskFormatter(s);
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
return formatter;
}
This date reflects the "week ending date" which is Sunday in my case. This date will be sent to a database and will also be used to automatically display the dates of the rest of the days in the week. So for example:
If the week ending date entered by the user is 3/30/2014, then a JLabel on the form will fill in as 3/30 under the text "Sunday", and 3/29 under Saturday, and so on till monday. Which brings me to my next issue, subtracting dates.
I've done research and just about everywhere points towards JodaTime, but I also saw someone mention that the new Java8 JDK addresses much of the previous weak points for Java in terms of Dates.
I am looking for guidance in terms of mapping/listing out my requirements for what I want to accomplish because I feel very muddled and can't seem to find strong enough direction. I am also hoping for any feedback on the way JDK8 uses dates and if it is a better choice than Joda. (I know subjectiveness is frowned upon here but I don't know where else to ask.)
EDIT a visualization may help. This Panel will be printed so a JSpinner is unfavorable. The yellow is only there temporarily to help me visualize space distribution.
回答1:
There are better choices to allow users input dates in Swing, but in third-party libraries:
JDateChooser
available in JCalendar libraryJXDatePicker
available in SwingX libary
However if you don't want to use any third-party library but standard Swing components instead, I'd suggest you use JSpinner with a date model. See How to Use Spinners tutorial.
This code snippet should be enough to get it work:
SpinnerDateModel model = new SpinnerDateModel();
JSpinner spinner = new JSpinner(model);
JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, "MM/dd/yyyy");
DateFormatter formatter = (DateFormatter)editor.getTextField().getFormatter();
formatter.setAllowsInvalid(false);
formatter.setOverwriteMode(true);
spinner.setEditor(editor);
In order to work with dates honestly I didn't try Java 8 yet, but there's an official trail: Trail: Date Time
If you don't want to use the new API then you can use Calendar API (the old school API). For instance:
Date date = (Date)spinner.getModel().getValue();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
System.out.println("Today is Sunday!");
}
If you want to substract a day you can do as follows:
calendar.add(Calendar.DAY_OF_MONTH, -1);
There are good answers in this question too: How can I increment a date by one day in Java?
来源:https://stackoverflow.com/questions/22670461/how-to-get-the-date-from-jformattedtextfield-with-a-mask