Formatting a time (not current time)

Discussion in 'Java' started by B. Schad, Sep 3, 2003.

  1. B. Schad

    B. Schad Guest

    Hi,

    to format a certain time (say 14:15) what would you have to do? I found
    the following way:

    Calendar cal = Calendar.getInstance();
    cal.set (Calendar.HOUR_OF_DAY, 14);
    cal.set (Calendar.MINUTE, 15);
    Date date = cal.getTime();
    DateFormat df = DateFormat.getTimeInstance();

    StringBuffer msg = new StringBuffer();
    msg.append (df.format (date));

    The problem seemed to be that it is not possible to directly format a
    Calendar time. Do I really have to create a (somewhat incomplete) Date
    from the Calender time first?

    Kind regards,
    B. Schad
     
    B. Schad, Sep 3, 2003
    #1
    1. Advertising

  2. B. Schad

    Tim Guest

    why not use Date directly?

    Date d = new Date();

    d.setHours(14);

    d.setMinutes(15);

    DateFormat df = DateFormat.getTimeInstance();

    StringBuffer msg = new StringBuffer();

    msg.append(df.format(d));



    "B. Schad" <> wrote in message
    news:...
    > Hi,
    >
    > to format a certain time (say 14:15) what would you have to do? I found
    > the following way:
    >
    > Calendar cal = Calendar.getInstance();
    > cal.set (Calendar.HOUR_OF_DAY, 14);
    > cal.set (Calendar.MINUTE, 15);
    > Date date = cal.getTime();
    > DateFormat df = DateFormat.getTimeInstance();
    >
    > StringBuffer msg = new StringBuffer();
    > msg.append (df.format (date));
    >
    > The problem seemed to be that it is not possible to directly format a
    > Calendar time. Do I really have to create a (somewhat incomplete) Date
    > from the Calender time first?
    >
    > Kind regards,
    > B. Schad
    >
     
    Tim, Sep 3, 2003
    #2
    1. Advertising

  3. B. Schad

    B. Schad Guest

    B. Schad, Sep 3, 2003
    #3
  4. B. Schad

    Tim Guest

    "B. Schad" <> wrote in message
    news:...
    >
    > setHours and setMinutes are deprecated.
    >


    Oops.. I had to know that!!

    Well, I think there's the answer why you've to use Calendar too..
     
    Tim, Sep 3, 2003
    #4
  5. "B. Schad" <> writes:
    > to format a certain time (say 14:15) what would you have to do?


    > I found
    > the following way:


    Yes, it is a mess. Sadly that is how it is supposed to be done. You can
    play a little bit and e.g. create an own SimpleDateFormat instead of
    using DateFormat.getTimeInstance(), but that doesn't change much
    (DateFormat.getTimeInstance() also returns a SimpleDateFormat).

    The really annoying thing is that SimpleDateFormat uses a Calendar
    internally for separating e.g. hours and minutes. But there is no
    format method that allows to provide an own Calendar for formating.
    And no, setCalendar() does not what one would hope.

    /Thomas
     
    Thomas Weidenfeller, Sep 3, 2003
    #5
  6. B. Schad wrote:
    > Hi,
    >
    > to format a certain time (say 14:15) what would you have to do? I found
    > the following way:
    >
    > Calendar cal = Calendar.getInstance();
    > cal.set (Calendar.HOUR_OF_DAY, 14);
    > cal.set (Calendar.MINUTE, 15);
    > Date date = cal.getTime();
    > DateFormat df = DateFormat.getTimeInstance();
    >
    > StringBuffer msg = new StringBuffer();
    > msg.append (df.format (date));
    >


    Calendar.getTime() returns a Date, use that in the format
     
    David Zimmerman, Sep 3, 2003
    #6
  7. B. Schad

    Roedy Green Guest

    On Wed, 03 Sep 2003 10:50:34 +0200, "B. Schad" <> wrote
    or quoted :

    >The problem seemed to be that it is not possible to directly format a
    >Calendar time. Do I really have to create a (somewhat incomplete) Date
    >from the Calender time first?


    It is a bit odd they left out a convenience format( Calendar )
    method, but what do you expect from goofs who create code like this:

    now.getTime().getTime();

    where one getTime gets a Date object and the other a long.

    They are SO muddled about vocabulary in those classes! I fulminate
    about it in http://mindprod.com/jgloss/gotchas.html#DATE

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 3, 2003
    #7
  8. B. Schad

    B. Schad Guest

    >> Calendar cal = Calendar.getInstance();
    >> cal.set (Calendar.HOUR_OF_DAY, 14);
    >> cal.set (Calendar.MINUTE, 15);
    >> Date date = cal.getTime();
    >> DateFormat df = DateFormat.getTimeInstance();
    >>
    >> StringBuffer msg = new StringBuffer();
    >> msg.append (df.format (date));
    >>

    >
    > Calendar.getTime() returns a Date, use that in the format


    That already is what I do.
     
    B. Schad, Sep 4, 2003
    #8
  9. B. Schad

    B. Schad Guest

    >>The problem seemed to be that it is not possible to directly format a
    >>Calendar time. Do I really have to create a (somewhat incomplete) Date

    >
    >>from the Calender time first?

    >
    > It is a bit odd they left out a convenience format( Calendar )


    When calling Calendar.getTime() it is tried to find a time instance that
    matches the Calendar (which is different depending on the Locale). The
    information I provide is incomplete so getTime() probably guesses the
    missing information (year, month, day and so on).

    The problem is that all this is not needed to format the time I provide
    in the code. Hopefully finding a time instance does not change hour and
    minute. So this is not just convenience missing. :-(
     
    B. Schad, Sep 4, 2003
    #9
    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. Manu
    Replies:
    1
    Views:
    6,106
    Natty Gur
    Jul 20, 2003
  2. David Hearn
    Replies:
    1
    Views:
    417
    David Hearn
    Sep 10, 2003
  3. Matthew Heidemann
    Replies:
    14
    Views:
    248
    eT Ma
    Mar 22, 2011
  4. Replies:
    3
    Views:
    180
  5. murrayatuptowngallery
    Replies:
    0
    Views:
    91
    murrayatuptowngallery
    Feb 11, 2008
Loading...

Share This Page