how can i change the date format

Discussion in 'Java' started by KelvinWongYW@gmail.com, Jan 14, 2008.

  1. Guest

    Dear All,

    i have some problem on changing the date format.

    i would like to change 2008-02-15 to 15th which the th should be the
    power of 15

    is there any method to do it ? i am so familiar with JAVA and i have
    look for the answer on web but cannot find

    hope someone can help me out

    thanks
     
    , Jan 14, 2008
    #1
    1. Advertising

  2. Lew Guest

    wrote:
    > i [sic] have some problem on changing the date format.
    >
    > i would like to change 2008-02-15 to 15th which the th should be the
    > power of 15


    What do you mean by "the th should be the power of 15"? That doesn't make any
    kind of sense that I can discern.

    > is there any method to do it ? i am so familiar with JAVA


    It's "Java", not "JAVA".

    > and i have look for the answer on web but cannot find
    >
    > hope someone can help me out


    <http://java.sun.com/javase/6/docs/api/java/text/DateFormat.html>

    Always start with the Javadocs if you have a Java API matter.

    --
    Lew
     
    Lew, Jan 14, 2008
    #2
    1. Advertising

  3. On Jan 14, 2:50 pm, wrote:
    ...
    > i would like to change 2008-02-15 to 15th which the th should be the
    > power of 15


    Did you mean that the 'th' characters should be
    written as a superscript (raised above the normal
    baseline of the text flow)?

    e.g.
    <sscce>
    import javax.swing.*;

    class SuperScript {

    public static void main(String[] args) {
    String html =
    "<html><body>" +
    "<p>2<sup>3</sup> = 2 x 2 x 2";
    JOptionPane.showMessageDialog(
    null,
    new JLabel(html) );
    }
    }
    </sscce>

    Also: note that in English, the word 'I' should
    *always* be capitalized.

    --
    Andrew T.
    PhySci.org
     
    Andrew Thompson, Jan 14, 2008
    #3
  4. Roedy Green Guest

    On Sun, 13 Jan 2008 19:50:48 -0800 (PST),
    wrote, quoted or indirectly quoted someone who said :

    >i would like to change 2008-02-15 to 15th which the th should be the
    >power of 15


    In North America that form disappeared back in the 50s, so I don't it
    occurred to the Sun people to support it directly. What you can do is
    this:

    Display the date normally. Extract the day of the month. Use my
    ordinalSuffix method to tack on a suffix.

    See the code for ordinalSuffix at
    http://mindprod.com/jgloss/ordinal.html

    For applying ordinal suffixes to arbitrarily large numbers written out
    in words see http://mindprod.com/products1.html#INWORDS
    look at the AmericanOrdinals class.

    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
     
    Roedy Green, Jan 14, 2008
    #4
  5. Guest

    On Jan 14, 9:59 am, Roedy Green <>
    wrote:
    > On Sun, 13 Jan 2008 19:50:48 -0800 (PST),
    > wrote, quoted or indirectly quoted someone who said :
    >
    > >i would like to change 2008-02-15 to 15th which the th should be the
    > >power of 15

    >
    > In North America that form disappeared back in the 50s, so I don't it
    > occurred to the Sun people to support it directly. What you can do is
    > this:
    >
    > Display the date normally. Extract the day of the month. Use my
    > ordinalSuffix method to tack on a suffix.
    >
    > See the code for ordinalSuffix athttp://mindprod.com/jgloss/ordinal.html
    >
    > For applying ordinal suffixes to arbitrarily large numbers written out
    > in words seehttp://mindprod.com/products1.html#INWORDS
    > look at the AmericanOrdinals class.
    >
    > --
    > Roedy Green, Canadian Mind Products
    > The Java Glossary,http://mindprod.com


    Interesting, I've never heard of the 11st, 12nd or 13rd...

    It's much better (and easier) to just do this:
    private static String getSuffix(int i) {
    switch (i) {
    case 1:
    case 21:
    case 31:
    return "st";
    case 2:
    case 22:
    return "nd";
    case 3:
    case 23:
    return "rd";
    default:
    return "th";
    }
    }
     
    , Jan 31, 2008
    #5
  6. Roedy Green Guest

    On Wed, 30 Jan 2008 16:52:24 -0800 (PST),
    wrote, quoted or indirectly
    quoted someone who said :

    >It's much better (and easier) to just do this:
    > private static String getSuffix(int i) {
    > switch (i) {
    > case 1:
    > case 21:
    > case 31:
    > return "st";
    > case 2:
    > case 22:
    > return "nd";
    > case 3:
    > case 23:
    > return "rd";
    > default:
    > return "th";
    > }
    > }


    here is my corrected version

    /**
    * produces an ordinal "th" suffix string for given number.
    * @param number value you want the ordinal suffix for:
    * @return corresponding ordinal suffix, i.e. "st", "nd", "rd", or
    "th"
    */
    String ordinalSuffix ( int value )
    {
    value = Math.abs( value );
    final int lastDigit = value % 10;
    final int last2Digits = value % 100;
    switch ( lastDigit )
    {
    case 1 :
    return last2digits == 11 ? "th" : "st";

    case 2:
    return last2digits == 12 ? "th" : "nd";

    case 3:
    return last2digits == 13 ? "th" : "rd";

    default:
    return "th";
    }
    }

    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
     
    Roedy Green, Jan 31, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,205
    Janaka
    Feb 18, 2004
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,365
    Michael Borgwardt
    May 30, 2004
  3. Lew
    Replies:
    1
    Views:
    391
  4. Roedy Green
    Replies:
    0
    Views:
    553
    Roedy Green
    Jul 29, 2009
  5. Josh Sharpe
    Replies:
    1
    Views:
    227
    Brian Candler
    Sep 21, 2010
Loading...

Share This Page