5 Replies Latest reply: Oct 8, 2006 5:55 AM by 807598 RSS

    getting date

    807598
      Hi Techies,
      I am trying to get the number of days in a month. Here is my code
       int totalNumberOfDaysInTheCurrentMonth =calendar.getActualMaximum(calendar.DAY_OF_MONTH);
               Date endOfTheMonthDate = new Date(totalNumberOfDaysInTheCurrentMonth);
      I am getting 31 for october but I need to get in mm/dd/yyyy format. How can i do this one.

      regards,
      krishna
        • 1. Re: getting date
          807598
          You're not making sense. The number of days in a month is an integer. It doesn't have a "mm/dd/yyyy" format.

          Are you trying to say that you're trying to get a formatted date string representing the last day of a month?

          In that case, set the date in the calendar to the last day of the month, get a Date object from the Calendar, and then pass the Date object to a java.text.SimpleDateFormat.
          • 2. Re: getting date
            807598
            thanks for your suggestion.
            can you please send me the sample code
            • 3. Re: getting date
              807598
              No. What part of what I said didn't you understand?
              • 4. Re: getting date
                807598
                thanks , I got the solution;
                DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
                           int month =calendar.get(calendar.MONTH);
                           int year = calendar.get(calendar.YEAR);
                         int totalNumberOfDaysInTheCurrentMonth =calendar.getActualMaximum(calendar.DAY_OF_MONTH);
                calendar.set(year,month,totalNumberOfDaysInTheCurrentMonth);
                          Date d =calendar.getTime();
                  
                         System.out.println("krishna"+formatter.format(d));
                        
                          
                but I wonder why month is giving me 9 instead of 10.
                • 5. Re: getting date
                  807598
                  Months are zero-indexed. January is month number zero.