1

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. this

exit_1
  • 1,240
  • 4
  • 13
  • 32
  • About how to increase/substract a `Date` see: http://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java – DSquare Mar 26 '14 at 19:11

1 Answers1

4

There are better choices to allow users input dates in Swing, but in third-party libraries:

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?

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • thanks for your answer. I have seen a slot of people recommend JSpinner in similiar questions. my only concern, and why I've been avoiding it, is that I will need to print the JPanel that I'm working on; so using a JSpinner seems like it wouldn't look as clean. – exit_1 Mar 26 '14 at 20:28
  • You're welcome :) Yes maybe it wouldn't but on the other hand it would be easier to input data. `JFormattedTextfield` is very powerful (`JSpinner` makes use of that component) but in this case you would have to work very hard (parsing dates, validating input, etc.) to achieve *almost* the same functionality, not to say users' input won't likely be as easy as using `JSpinner` (not to mention date pickers such as `JDateChooser` or `JXDatePicker`). At the end of the day users will appreciate more the ease of data entry than screen printing cleanliness, IMHO. @solleks – dic19 Mar 27 '14 at 10:36