lundi 30 novembre 2015

Correct Date to appear in JSpinner and data formatted correctly to insert to database

I am making a employee scheduling program for a school project. The employee will be able to make their availability on a weekly basis. I am having trouble making a JSpinner show the date for a Monday of each week. Also, when the user clicks up or down on the arrows, I would like it to only cycle for the dates of upcoming or prior Mondays. When inserting the data into a database i would like the start and finish times of each day to be in Military time. For the week, i would like it to just be the number of the week of the year. When it is inserted into the database, the week is a primary key and should stay constant with the same week, i.e. two employees should be able to create availability for week 5. The employee ID is also a primary and foreign key. The weekID in the database is being created in ascending order i.e. 1,2,3. Start and End times are being shown as this: Mon Nov 30 09:19:29 CST 2015 when I want it to just show as 09:19.

here is the code for the week spinner:

    Date datef = new Date();
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);




    SpinnerDateModel ehm = new SpinnerDateModel(datef, null, null,     

    c.WEEK_OF_YEAR);
weekOf = new javax.swing.JSpinner(ehm);
JSpinner.DateEditor wo = new JSpinner.DateEditor(weekOf,"MM/dd/yyyy");
weekOf.setEditor(wo);
weekOf.setModel(new javax.swing.SpinnerDateModel());

The code for the time spinner is as follows(the times show correctly in these spinners, but just the output isn't correct)

Date date = new Date();
SpinnerDateModel sm = new SpinnerDateModel(date, null, null, Calendar.HOUR_OF_DAY);
monStart = new javax.swing.JSpinner(sm);
JSpinner.DateEditor ms = new JSpinner.DateEditor(monStart,"H:mm");
monStart.setEditor(ms);
monStart.setModel(new javax.swing.SpinnerDateModel());

That is how i have the code set for all of the time selectors, with different variables obviously.

here is the code for the action performed:

 public void btnAvailUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAvailUpdateActionPerformed
        Date weekID = (Date) weekOf.getValue();
        Date mstart = (Date) monStart.getValue();
        Date mend = (Date) monEnd.getValue();
        Date tstart = (Date) tuesStart.getValue();
        Date tend = (Date) tuesEnd.getValue();
        Date wstart = (Date) wedStart.getValue();
        Date wend = (Date) wedEnd.getValue();
        Date thstart = (Date) thursStart.getValue();
        Date thend = (Date) thursEnd.getValue();
        Date fstart = (Date) friStart.getValue();
        Date fend = (Date) friEnd.getValue();


        try {
            PreparedStatement st = conn.prepareStatement("INSERT INTO Availability (Week_ID, Employee_ID, monStart, monFinish, tuesStart, tuesFinish, wedStart, wedFinish, thursStart, thursFinish, friStart, friFinish) Values(?,?,?,?,?,?,?,?,?,?,?,?)");
            st.setString(1, String.valueOf(weekID));
            st.setInt(2, loginScreen.dbid);
            st.setString(3, String.valueOf(mstart));
            st.setString(4, String.valueOf(mend));
            st.setString(5, String.valueOf(tstart));
            st.setString(6, String.valueOf(tend));
            st.setString(7, String.valueOf(wstart));
            st.setString(8, String.valueOf(wend));
            st.setString(9, String.valueOf(thstart));
            st.setString(10, String.valueOf(thend));
            st.setString(11, String.valueOf(fstart));
            st.setString(12, String.valueOf(fend));
            st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //System.out.println(weekID);
    }//GN-LAST:event_btnAvailUpdateActionPerformed

Any help or suggestions would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire