12 Replies Latest reply: Mar 1, 2008 3:45 PM by DrClap RSS

    One last question for payroll - Manipulating Dates

    807591
      I'm so close to nailing this thing...only one thing left and I'm done with that program. (There will be others, but this one has been murder on me.)

      So, again, this program reads an assortment of stuff from a file based on input and...well, the details aren't important. The point is, one of the scanned lines from the file is a certain date, in mm/dd/yy format. This is the start date, the beginning of a period of time over which the employee worked. The ending of this time period is supposed to be at the end of the work week, five days later. How can I put a string in my program that takes the date string from the file and adds five days onto it?
        • 1. Re: One last question for payroll - Manipulating Dates
          807591
          Get the String value, use the java.text.SimpleDateFormat to parse it to an java.util.Date object. Then get an instance of java.util.Calendar and set it with the Date object. Then add 5 days to it. Finally you can get the changed Date object back from the Calendar object and format it to String with SimpleDateFormat.

          Here are the API documentations of interest:
          [http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html]
          [http://java.sun.com/javase/6/docs/api/java/util/Date.html]
          [http://java.sun.com/javase/6/docs/api/java/util/Calendar.html]
          • 2. Re: One last question for payroll - Manipulating Dates
            807591
            How do I call the value of the date without the slashes? That seems to be my problem right now, I try to call the value of the string and it says I can't because of the slashes.

            (Sorry, again, new to Java, just trying to get my sea legs.)
            • 3. Re: One last question for payroll - Manipulating Dates
              807591
              I have no idea what you're talking about.
              • 4. Re: One last question for payroll - Manipulating Dates
                807591
                Well, when I try to turn the string that has the original date in it into an integer, I can't because of the slashes.

                Like, my first date is

                1/03/05

                I want to turn it into an integer so I can go dd+5 and make the end date at the end of the week.

                But I can't turn it into an integer because of the / and / in the middle of it.

                How do I get past this?
                • 5. Re: One last question for payroll - Manipulating Dates
                  807591
                  So you didn't understand my answer? Why are you asking how to get a certainly bad approach to work instead of saying that you didn't understood my answer, or to elaborate why you don't want to use the right way to handle the date?
                  • 6. Re: One last question for payroll - Manipulating Dates
                    DrClap
                    You don't "get past" that. You should not have gone that way in the first place. Reply #1 was a perfectly good answer, why did you completely ignore it?
                    • 7. Re: One last question for payroll - Manipulating Dates
                      807591
                      Well, I thought that's what I was attempting...to get the string value, that is, like you said to start with. There must be something I'm not seeing.
                      • 8. Re: One last question for payroll - Manipulating Dates
                        807591
                        Step 1:
                        String dateString = "03/01/08";
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");
                        Date dateObject = simpleDateFormat.parse(dateString);
                        For the rest of the steps, please follow the links I gave in my first reply. The code almost writes itself if you follow my explanation carefully and consult the API documentation thoroughly.
                        • 9. Re: One last question for payroll - Manipulating Dates
                          807591
                          I can't have the string be a date set in the code though...I need to call it from a file, THEN use that called date to make a second string which has that date + 5 days.

                          I'm sorry to be such a bother, but I'm really just not grasping what it is I need to do...I've been staring at those documents for awhile now and everything I try doesn't work.
                          • 10. Re: One last question for payroll - Manipulating Dates
                            796440
                            the777joker777 wrote:
                            I can't have the string be a date set in the code though...I need to call it from a file,
                            How does that make any difference? (Hint: It doesn't. A String is a String, regardless of whether it's a literal in your source code or read from a file or a DB or user input or generated randomly from the background radiation from the big bang. Literals are just easier to use here to demonstrate the concept.)
                            • 11. Re: One last question for payroll - Manipulating Dates
                              807591
                              Okay, I managed to figure out almost all of it. I'm almost there.

                              I tried to do this...

                                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");
                                        Date date1 = simpleDateFormat.parse(info);
                                        Calendar calendar = new GregorianCalendar();
                                        calendar.setTime(date1);
                                        calendar.add(Calendar.DAY_OF_MONTH,5);
                                        calendar.get(calendar.MONTH);
                                        calendar.get(calendar.DAY_OF_MONTH);
                                        calendar.get(calendar.YEAR);
                                        
                                        String enddate1 = Integer.toString(calendar.MONTH);
                                        String enddate2 = Integer.toString(calendar.DAY_OF_MONTH);
                                        String enddate3 = Integer.toString(calendar.YEAR);

                              then at the end in the print section...

                                        System.out.println("Memo: employee #" + num + " for the period " + info + " through " + enddate1 + "/" + enddate2 + "/" + enddate3);

                              But the enddates got returned as 2/5/1, which isn't accurate nor in proper format. I thought I declared the format at the top...
                              • 12. Re: One last question for payroll - Manipulating Dates
                                807591
                                Get rid of all that rubbish with the enddate Strings.

                                A format is applied to a date when you want to print out a formatted date. So you have to actually make use of it. You also didn't read the documentation for Calendar clearly.Although again that's okay because you don't need it.
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");
                                Date date1 = simpleDateFormat.parse(info);
                                Calendar calendar = new GregorianCalendar();
                                calendar.setTime(date1);
                                calendar.add(Calendar.DAY_OF_MONTH,5);
                                Date date2 = calendar.getTime();
                                String newDateFormattedTheWayYouWant = simpleDateFormat.format(date2);
                                And then print the String newDateFormattedTheWayYouWan