SimpleDateFormat

Discussion in 'Java' started by bob, Sep 5, 2011.

  1. bob

    bob Guest

    Anyone know how to use SimpleDateFormat to get a lowercase am and pm?
    bob, Sep 5, 2011
    #1
    1. Advertising

  2. bob

    Eric Sosman Guest

    On 9/4/2011 9:14 PM, bob wrote:
    > Anyone know how to use SimpleDateFormat to get a lowercase am and pm?


    You could use String.toLowerCase() on the whole thing, at the
    risk of turning "Fri" into "fri". (If you're super-worried, you could
    use a regex to locate the AM/PM and apply toLowerCase() only to that
    portion.) Or you could extend the class. Other than that, I can't
    think of anything.

    --
    Eric Sosman
    d
    Eric Sosman, Sep 5, 2011
    #2
    1. Advertising

  3. bob

    Arne Vajhøj Guest

    On 9/4/2011 9:14 PM, bob wrote:
    > Anyone know how to use SimpleDateFormat to get a lowercase am and pm?


    Code snippet:

    import java.text.DateFormatSymbols;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class AMPM {
    public static void main(String[] args) {
    Date d1 = new Date();
    Date d2 = new Date(d1.getTime() + 12*60*60*1000L);
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
    System.out.println(sdf.format(d1));
    System.out.println(sdf.format(d2));
    DateFormatSymbols sym = sdf.getDateFormatSymbols();
    sym.setAmPmStrings(new String[] { "am", "pm" });
    sdf.setDateFormatSymbols(sym);
    System.out.println(sdf.format(d1));
    System.out.println(sdf.format(d2));
    }
    }

    Arne
    Arne Vajhøj, Sep 5, 2011
    #3
  4. bob

    Eric Sosman Guest

    On 9/4/2011 9:22 PM, Eric Sosman wrote:
    > On 9/4/2011 9:14 PM, bob wrote:
    >> Anyone know how to use SimpleDateFormat to get a lowercase am and pm?

    >
    > You could use String.toLowerCase() on the whole thing, at the
    > risk of turning "Fri" into "fri". (If you're super-worried, you could
    > use a regex to locate the AM/PM and apply toLowerCase() only to that
    > portion.) Or you could extend the class. Other than that, I can't
    > think of anything.


    <!Dope slap!>

    Never mind; see Arne Vajhøj's response.

    --
    Eric Sosman
    d
    Eric Sosman, Sep 5, 2011
    #4
  5. bob

    Roedy Green Guest

    On Sun, 4 Sep 2011 18:14:18 -0700 (PDT), bob <>
    wrote, quoted or indirectly quoted someone who said :

    >Anyone know how to use SimpleDateFormat to get a lowercase am and pm?


    there is no built in code for that. Think instead how to convert your
    date/time string AM/PM to am/pm

    There various ways you can do it with String.charAt String.indexOf
    String.toLowerCase
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
    the search for a superior moral justification for selfishness.
    ~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
    Roedy Green, Sep 5, 2011
    #5
  6. bob

    Lew Guest

    Roedy Green wrote:
    > bob wrote, quoted or indirectly quoted someone who said :
    >> Anyone know how to use SimpleDateFormat to get a lowercase am and pm?

    >
    > there is no built in code for that. Think instead how to convert your
    > date/time string AM/PM to am/pm
    >
    > There various ways you can do it with String.charAt String.indexOf
    > String.toLowerCase


    Or use Arne's suggestion, which has the virtue of being compact and employing the standard API without having to post-process the output of 'format()'.

    --
    Lew
    Lew, Sep 5, 2011
    #6
  7. bob

    Arne Vajhøj Guest

    On 9/5/2011 2:12 PM, Roedy Green wrote:
    > On Sun, 4 Sep 2011 18:14:18 -0700 (PDT), bob<>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >> Anyone know how to use SimpleDateFormat to get a lowercase am and pm?

    >
    > there is no built in code for that. Think instead how to convert your
    > date/time string AM/PM to am/pm
    >
    > There various ways you can do it with String.charAt String.indexOf
    > String.toLowerCase


    A slightly modified version of an old saying: some people
    can code in C in any language.

    Arne
    Arne Vajhøj, Sep 5, 2011
    #7
  8. bob

    Roedy Green Guest

    On Mon, 5 Sep 2011 13:11:33 -0700 (PDT), Lew <>
    wrote, quoted or indirectly quoted someone who said :

    >
    >Or use Arne's suggestion, which has the virtue of being compact and employing the standard API without having to post-process the output of 'format()'.


    but arne does not exist in my universe. I had with him long ago and
    plonked him.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
    the search for a superior moral justification for selfishness.
    ~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
    Roedy Green, Sep 5, 2011
    #8
  9. bob

    Arne Vajhøj Guest

    On 9/5/2011 6:57 PM, Roedy Green wrote:
    > On Mon, 5 Sep 2011 13:11:33 -0700 (PDT), Lew<>
    > wrote, quoted or indirectly quoted someone who said :
    >> Or use Arne's suggestion, which has the virtue of being compact and employing the standard API without having to post-process the output of 'format()'.

    >
    > but arne does not exist in my universe. I had with him long ago and
    > plonked him.


    That is your right.

    But I will consider it somewhat an ego trip that you want people to read
    your outdated posts because you don't want to read all posts to the
    thread.

    Arne
    Arne Vajhøj, Sep 6, 2011
    #9
  10. bob

    Eric Sosman Guest

    On 9/5/2011 6:57 PM, Roedy Green wrote:
    > On Mon, 5 Sep 2011 13:11:33 -0700 (PDT), Lew<>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >>
    >> Or use Arne's suggestion, which has the virtue of being compact and employing the standard API without having to post-process the output of 'format()'.

    >
    > but arne does not exist in my universe. I had with him long ago and
    > plonked him.


    Bad move. But that's just my opinion.

    --
    Eric Sosman
    d
    Eric Sosman, Sep 6, 2011
    #10
  11. bob

    markspace Guest

    On 9/5/2011 3:57 PM, Roedy Green wrote:
    > On Mon, 5 Sep 2011 13:11:33 -0700 (PDT), Lew<>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >>
    >> Or use Arne's suggestion, which has the virtue of being compact and
    >> employing the standard API without having to post-process the
    >> output of 'format()'.

    >
    > but arne does not exist in my universe. I had with him long ago and
    > plonked him.



    Then maybe you should un-plonk him, because he write good code and has
    valuable insights.
    markspace, Sep 6, 2011
    #11
  12. bob

    Lew Guest

    Roedy Green wrote:
    > Lew wrote, quoted or indirectly quoted someone who said :
    >> Or use Arne's suggestion, which has the virtue of being compact and employing the standard API without having to post-process the output of 'format()'.

    >
    > but arne [sic] does not exist in my universe. I had [it] with him long ago and
    > plonked him.


    That neither invalidates his suggestion nor excuses your ignorance of the API.

    I, too, was ignorant of the SimpleDateFormat API, at least of the methods Arne mentioned, but I am in the habit of reading the Javadocs. The lesson for me, for the OP, and for you, too, is to check the Javadocs, in this casesince the question related to 'SimpleDateFormat', the Javadocs for 'SimpleDateFormat'.

    The trouble with your answer is that it was suboptimal in the context of the much, much better answer that Arne had provided. This was a disservice to the OP. I recommend that you swallow your ego and unplonk Arne. After all, you are the one who's all about providing good information to those wishing to learn Java, aren't you?

    --
    Lew
    Lew, Sep 6, 2011
    #12
  13. bob

    Roedy Green Guest

    On Mon, 05 Sep 2011 19:38:23 -0400, Eric Sosman
    <> wrote, quoted or indirectly quoted
    someone who said :

    > Bad move. But that's just my opinion.


    I try to balance. I don't plonk Lew, even though I find him grating.
    He is just too full of useful information to give up.

    Arne is intelligent, but I felt the much smaller number of gems he put
    out were not worth the more vicious thorn jabs.

    Perhaps I should unplonk him for a while to see if he has mellowed or
    my hide has toughened.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
    the search for a superior moral justification for selfishness.
    ~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
    Roedy Green, Sep 15, 2011
    #13
  14. bob

    Eric Sosman Guest

    On 9/14/2011 9:27 PM, Roedy Green wrote:
    > On Mon, 05 Sep 2011 19:38:23 -0400, Eric Sosman
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    >> Bad move. But that's just my opinion.

    >
    > I try to balance. I don't plonk Lew, even though I find him grating.
    > He is just too full of useful information to give up.
    >
    > Arne is intelligent, but I felt the much smaller number of gems he put
    > out were not worth the more vicious thorn jabs.
    >
    > Perhaps I should unplonk him for a while to see if he has mellowed or
    > my hide has toughened.


    Be sure to announce your decision to Usenet at large. Burning
    the ballots to produce white smoke is traditional.

    --
    Eric Sosman
    d
    Eric Sosman, Sep 15, 2011
    #14
  15. On 15/09/2011 02:38, Eric Sosman wrote:
    > On 9/14/2011 9:27 PM, Roedy Green wrote:
    >> On 05 Sep 2011 19:38 Eric Sosman wrote:
    >>
    >>> Bad move. But that's just my opinion.

    >>
    >> Perhaps I should unplonk him for a while to see if he has mellowed or
    >> my hide has toughened.

    >
    > Be sure to announce your decision to Usenet at large. Burning
    > the ballots to produce white smoke is traditional.
    >


    From excommunicant to infallibility in a single step?

    --
    RGB
    RedGrittyBrick, Sep 15, 2011
    #15
    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. Manuel
    Replies:
    0
    Views:
    545
    Manuel
    Aug 28, 2003
  2. Niko
    Replies:
    10
    Views:
    2,453
    Wojtek
    Sep 18, 2003
  3. Eric

    SimpleDateFormat

    Eric, Sep 24, 2003, in forum: Java
    Replies:
    4
    Views:
    9,534
    Chris Smith
    Sep 25, 2003
  4. Bill Angel
    Replies:
    4
    Views:
    1,342
    Bill Angel
    Sep 28, 2003
  5. Kyote
    Replies:
    16
    Views:
    4,154
    Michael Borgwardt
    Oct 24, 2003
Loading...

Share This Page