0

I have a camping program that has a dialog box where the user inputs the date they are to check in and the date they plan to check out into a text field. Once they press ok on the dialog box, the dates they input should then be viewable in a JTable. Currently, in the date column I get a java.utl.GregorianCalendar[time....etc] when the user presses ok. The other information such as name, days staying, and site number appear okay. Obviously it is not formatting the date correctly, but I'm not sure how to fix it. Here is the code for the Gregorian Calendar in the class DialogCheckInTent:

 public GregorianCalendar getDateIn(){
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    GregorianCalendar g = new GregorianCalendar();
     try{Date date = df.parse(dateIn.getText()); 
     g.setTime(date);
     }
     catch (ParseException ex) {
            ex.printStackTrace();
        }
    return g;
}

public GregorianCalendar getCheckOutDate(){
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    GregorianCalendar g = new GregorianCalendar();
     try{Date date = df.parse(checkOutTxt.getText()); 
     g.setTime(date);
     }
     catch (ParseException ex) {
            ex.printStackTrace();
        }
    return g;
}

The above code is used in this method in the GUI when the user pressed okay.

 if (pressed == checkInTentItem) {
        newTent.clear();
        newTent.setVisible(true);
        if (model.getRowCount() < 5) {
            if (newTent.isOk()) {
                String nameReserving = newTent.getNameReserve();
                GregorianCalendar checkIn = newTent.getDateIn();
                int daysStaying = newTent.getDaysStaying();
                GregorianCalendar checkOutOn = newTent.getCheckOutDate();
                int siteNumber = newTent.getSiteNumber();
                int tenters = newTent.getTenters();
                Tent tent = new Tent(nameReserving, checkIn, daysStaying,
                        checkOutOn, siteNumber, tenters);
                model.add(tent);
            }

        }
        else {
            JOptionPane.showMessageDialog(frame, "All sites are full.",
                    " ", JOptionPane.INFORMATION_MESSAGE);
        }
    }

Everything compiles without a problem, but the date is just not working correctly. How do I fix this?

2 Answers2

3
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");

format is incorrect

months are represented with M, so change it to MM/dd/yyyy

also not sure what is the type for pressed and checkInTentItem for following condition

if (pressed == checkInTentItem) {
jmj
  • 237,923
  • 42
  • 401
  • 438
0

java.time

The Answer by Joshi is correct, and should be accepted.

The modern solution uses the LocalDate class. This class is part of the java.time classes that now supplant the troublesome old date-time classes such as GregorianCalendar, Date, and SimpleDateFormat.

Parse your input as a LocalDate.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu" , Locale.US ) ;
LocalDate ld = LocalDate.parse( "11/29/2017" ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154