6 Replies Latest reply on Jun 17, 2008 7:16 AM by 807591

    Adding a date to a time range

    807591
      Greetings to all, I really need help on this issue:

      Problem:

      Is possible to add the system date(dd/mm/yyyy) to a pre-existing time range? In my project, I have an appointment column which holds the value for a time range, e.g. 0900 - 1100hrs.
      How do I add the system date to this column so that the database would store the the final data as 0900-1100 hrs dd/mm/yyyy? Or is this even possible?
        • 1. Re: Adding a date to a time range
          darrylburke
          Take a look at java.util.GregorianCalendar and don't miss the methods it inherits from Calendar.

          db
          • 2. Re: Adding a date to a time range
            807591
            Ok so I managed to somewhat merge the text time and the system date.
            BUT I still ran into a minor problem.

            Heres my code for what i did.

            String start = tuple[9].substring(0, 4);
            Calendar calender = new GregorianCalendar();
            String appointmentStart = start + " " + calender.get(Calendar.DATE);

            The problem I face now is that when I check my DB, the results are not what I thought.

            Expected results: 0900 17 June 2008

            Actual results: 0900 17

            I can't seem to get the month and the year displayed. I know that I am really really close. So close that I almost touch it.

            Any thoughts?
            • 3. Re: Adding a date to a time range
              807591
              What display are you talking about exactly?

              Because if you want to display a date in a certain way in Java then you need to use a SimpleDateFormat.

              If you are talking about how it displays in some database GUI that's totally off-topic for here because it has nothing to do with Java.

              So where are you seeing this date that isn't formatted as you'd like?
              • 4. Re: Adding a date to a time range
                807591
                Hi cotton, thanks for taking the time to help me out with my problem.

                ok I got the date, month and year to display in my DB column. But once again, all is not well.....Sighh.....

                The problem I am facing now is that MONTH display.

                My code:
                String start = tuple[9].substring(0, 4);
                Calendar calendar = new GregorianCalendar();
                String appointmentStart = start + "hrs- " + calendar.get(Calendar.DATE) +"/" calendar.get(Calendar.MONTH) "/" +calendar.get(Calendar.YEAR);

                Expected output for the MONTH is suppose to be 6 right? For the month of June. But why am I getting a 5 for May? If I am not wrong, the Calendar.MONTH gets the current month from the system right?
                • 5. Re: Adding a date to a time range
                  807591
                  bangbangbogi wrote:
                  Hi cotton, thanks for taking the time to help me out with my problem.

                  ok I got the date, month and year to display in my DB column. But once again, all is not well.....Sighh.....

                  The problem I am facing now is that MONTH display.

                  My code:
                  String start = tuple[9].substring(0, 4);
                  Calendar calendar = new GregorianCalendar();
                  String appointmentStart = start + "hrs- " + calendar.get(Calendar.DATE) +"/" calendar.get(Calendar.MONTH) "/" +calendar.get(Calendar.YEAR);

                  Expected output for the MONTH is suppose to be 6 right? For the month of June.
                  Nope.

                  This is why you should not be building Strings that way but instead turn your calendar into a java.util.Date and then format it with SimpleDateFormat.

                  The months in Calendar start with 0 not 1. That's your problem. So January is 0 etc up to June being 5. Which all seems horrible but that's okay because you never have to actually be exposed to that ugliness.
                  • 6. Re: Adding a date to a time range
                    807591
                    Hi cotton, thanks for your advice. I did what you said and it worked like a charm! Thanks for your and time.

                    Cheers