6 Replies Latest reply: Nov 6, 2007 9:26 AM by 807603 RSS

    java.text.SimpleDateFormat -- formatting my date

    807603
      Hello,

      I want to generate a date in following format;

      "Dec, 5th 2007" or "Dec, 1st 2007" or "Dec, 3rd 2007"

      So far, I have,

      SimpleDateFormat sdf = new SimpleDateFormat ( "MMM, dd yyyy" );
      return sdf.format ( new Date () );

      but I have not been able to get day of month in the "th", "rd", "st" format.

      Anyone know how to do this?

      Thanks!
        • 1. Re: java.text.SimpleDateFormat -- formatting my date
          794342
          something like this perhaps
          class Testing
          {
            public Testing()
            {
              for(int x = 0; x < 10; x++)
              {
                System.out.println(getFormattedDate(new java.util.Date((long)(Math.random()*100000000000L)+1200000000000L)));
              }
            }
            public String getFormattedDate(java.util.Date date)
            {
              int day = Integer.parseInt(new java.text.SimpleDateFormat("dd").format(date));
              return new java.text.SimpleDateFormat ( "MMM, dd'"+getSuffix(day)+"' yyyy" ).format(date);
            }
            public String getSuffix(int dayOfMonth)
            {
              String suffix = "";
              switch (dayOfMonth)
              {
                  case 1:
                  case 21:
                  case 31:
                      suffix = "st";
                      break;
                  case 2:
                  case 22:
                      suffix = "nd";
                      break;
                  case 3:
                  case 23:
                      suffix = "rd";
                      break;
                  default:
                      suffix = "th";
              }
              return suffix;
            }
            public static void main(String[]args)
            {
              new Testing();
            }
          }
          • 2. Re: java.text.SimpleDateFormat -- formatting my date
            807603
            Thank you very much! I thought that java would have something like that built in. suprised i have to code something as mundane as formating a day but oh well.

            Thank you very much!
            • 3. Re: java.text.SimpleDateFormat -- formatting my date ; [UPDATE]
              807603
              oh yes, using your sample code I wrote a simple program to generate the following arrays;
                      double[] limitDays =  {  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
                                              12, 13, 14, 15, 16, 17, 18, 19, 20, 
                                              21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
                      String[] formatDays = { "st", "nd", "rd", "th", "th", "th", "th", 
                                              "th", "th", "th", "th", "th", "th", "th", 
                                              "th", "th", "th", "th", "th", "th", "st", 
                                              "nd", "rd", "th", "th", "th", "th", "th", 
                                              "th", "th", "st" };
              Then I created an instance of ChoiceFormat;
              ChoiceFormat fmt = new ChoiceFormat ( limitDays, formatDays );
              I tested it using;
              import java.text.ChoiceFormat;
              public class Test {
                  public static void main (String[] args) {
                      double[] limitDays =  {  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
                                              12, 13, 14, 15, 16, 17, 18, 19, 20, 
                                              21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
                      String[] formatDays = { "st", "nd", "rd", "th", "th", "th", "th", 
                                              "th", "th", "th", "th", "th", "th", "th", 
                                              "th", "th", "th", "th", "th", "th", "st", 
                                              "nd", "rd", "th", "th", "th", "th", "th", 
                                              "th", "th", "st" };
              
                      ChoiceFormat fmt = new ChoiceFormat ( limitDays, formatDays );
              
                      for (int i = 1; i <= 31; i++) {
                          System.out.print (( i < 10 ? " " : "" ) + i + fmt.format ( i ) + "  " );
                          if (( i % 10 ) == 0)
                              System.out.println();
                      }
                  }
              }
              I got the following result;
              >
              1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
              11th 12th 13th 14th 15th 16th 17th 18th 19th 20th
              21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th
              31st
              >
              Pretty cool huh, and it did not take much work to do this either; less time it took to write this update. I wanted to be Javacally* correct so I took a few moments and used proper class :). I wish that the writters of Java* would have added a format string in SimpleDateFormat* class as a convenience but I think they are following some kind of ISO standard.

              Thanks!

              Edited by: earamsey on Nov 5, 2007 12:25 PM

              Edited by: earamsey on Nov 5, 2007 12:30 PM
              • 4. Re: java.text.SimpleDateFormat -- formatting my date ; [UPDATE]
                807603
                Ummm, I found bugs in that code so I replaced it with much simpler following code;
                    static final String getFormattedDateStr ()
                    {
                     Calendar cal = Calendar.getInstance();
                     Date dt = cal.getTime ();
                
                     String suffix = "'th'";
                
                     switch ( cal.get ( Calendar.DAY_OF_MONTH )) {
                         case 1:
                         case 21:
                         case 31:
                          suffix = "'st'";
                          break;
                         case 2:
                         case 22:
                          suffix = "'nd'";
                          break;
                         case 3:
                         case 23:
                          suffix = "'rd'";
                          break;
                     }
                
                     return new SimpleDateFormat ( "MMM, d" + suffix + " yyyy" ).format (dt );
                    }
                I guess I was making something simple into something complex. Oh well, ya live and learn :))


                Again, Thanks for the help!!!!
                • 5. Re: java.text.SimpleDateFormat -- formatting my date
                  DrClap
                  earamsey wrote:
                  I thought that java would have something like that built in.
                  Check out all of the Locales that Java supports. Now imagine writing similar code for each of those locales. Sure, it's easy to do that for English but Java wasn't designed for only English-speakers.

                  Have you encountered this feature in other languages you have worked with? Try it and see if it works in Japanese in that language. Or Portuguese.
                  • 6. Re: java.text.SimpleDateFormat -- formatting my date
                    807603
                    Ok, I shall try it this weekend and see what kind of problems I encounter!

                    Thanks!