String fomatter.

Discussion in 'Java' started by crazytazo, Dec 1, 2008.

  1. crazytazo

    crazytazo Guest

    I want to represent an elapsed time to a text.
    The text format is follow,
    ex ) 23 : 59 : 59
    01 : 05 : 05

    String.format("%2d : %2d : %2d", jElapsedTime / 3600, (jElapsedTime %
    3600) / 60, jElapsedTime % 60)
    I wrote code, but my code is represented 0 : 5 : 5

    How can I do that?
     
    crazytazo, Dec 1, 2008
    #1
    1. Advertising

  2. crazytazo

    Lew Guest

    On Dec 1, 3:17 am, Ewald Ertl <> wrote:
    > Hi,
    >
    > On 1 Dez., 07:59, crazytazo <> wrote:> I want to represent an elapsed time to a text.
    > > The text format is follow,
    > > ex ) 23 : 59 : 59
    > > 01 : 05 : 05

    >
    > > String.format("%2d : %2d : %2d", jElapsedTime / 3600, (jElapsedTime %
    > > 3600) / 60, jElapsedTime % 60)
    > > I wrote code, but my code is represented 0 : 5 : 5

    >
    > > How can I do that?

    >
    > I haven't done this, but the api documentation :http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax
    > seems to be the same as in C with "%02d" for zero padding.


    Even better, use java.text.DateFormat (SimpleDateFormat).

    --
    Lew
     
    Lew, Dec 1, 2008
    #2
    1. Advertising

  3. Lew wrote:
    > On Dec 1, 3:17 am, Ewald Ertl <> wrote:
    >> Hi,
    >>
    >> On 1 Dez., 07:59, crazytazo <> wrote:> I want to represent an elapsed time to a text.
    >>> The text format is follow,
    >>> ex ) 23 : 59 : 59
    >>> 01 : 05 : 05
    >>> String.format("%2d : %2d : %2d", jElapsedTime / 3600, (jElapsedTime %
    >>> 3600) / 60, jElapsedTime % 60)
    >>> I wrote code, but my code is represented 0 : 5 : 5
    >>> How can I do that?

    >> I haven't done this, but the api documentation :http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax
    >> seems to be the same as in C with "%02d" for zero padding.

    >
    > Even better, use java.text.DateFormat (SimpleDateFormat).
    >
    > --
    > Lew


    It looks like he is using a period rather than a time. I haven't
    figured out a way to do that with DateFormat. If you know please share.

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Dec 1, 2008
    #3
  4. crazytazo

    Lew Guest

    Lew wrote:
    >> Even better, use java.text.DateFormat (SimpleDateFormat).


    Knute Johnson wrote:
    > It looks like he is using a period rather than a time.  I haven't
    > figured out a way to do that with DateFormat.  If you know please share..


    <sscce source="eegee.PeriodFormats" >
    package eegee;

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.TimeZone;

    /** PeriodFormats. */
    public class PeriodFormats
    {
    private static final long PERIOD = 3905000L;

    /** Main method.
    * @param args <code>String []</code> command line arguments.
    */
    public static void main( String [] args )
    {
    DateFormat df = new SimpleDateFormat( "HH : mm : ss" );
    df.setTimeZone( TimeZone.getTimeZone( "GMT" ));

    Date period = new Date( PERIOD );
    System.out.println( df.format( period ));
    }
    }
    </sscce>

    run:
    01 : 05 : 05

    This could run into trouble with periods longer than 24 hours, I
    suppose. I haven't tested that.

    --
    Lew
     
    Lew, Dec 1, 2008
    #4
  5. crazytazo

    crazytazo Guest

    On Dec 1, 3:59 pm, crazytazo <> wrote:
    > I want to represent an elapsed time to a text.
    > The text format is follow,
    > ex ) 23 : 59 : 59
    > 01 : 05 : 05
    >
    > String.format("%2d : %2d : %2d", jElapsedTime / 3600, (jElapsedTime %
    > 3600) / 60, jElapsedTime % 60)
    > I wrote code, but my code is represented 0 : 5 : 5
    >
    > How can I do that?


    I've done.
    It was too easy.

    String.format("%02d : %02d : %02d", jElapsedTime / 3600, (jElapsedTime
    % 3600) / 60, jElapsedTime % 60)
     
    crazytazo, Dec 2, 2008
    #5
  6. crazytazo

    Roedy Green Guest

    On Sun, 30 Nov 2008 22:59:06 -0800 (PST), crazytazo
    <> wrote, quoted or indirectly quoted someone who
    said :

    >String.format("%2d : %2d : %2d", jElapsedTime / 3600, (jElapsedTime %
    >3600) / 60, jElapsedTime % 60)
    >I wrote code, but my code is represented 0 : 5 : 5
    >
    >How can I do that?


    see http://mindprod.com/jgloss/printf.html

    you want the 0 flag to use lead 0s.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    "Humanity is conducting an unintended, uncontrolled, globally pervasive experiment
    whose ultimate consequences could be second only to global nuclear war."
    ~ Environment Canada (The Canadian equivalent of the EPA on global warming)
     
    Roedy Green, Dec 2, 2008
    #6
  7. crazytazo

    Arne Vajhøj Guest

    Lew wrote:
    > Lew wrote:
    >>> Even better, use java.text.DateFormat (SimpleDateFormat).

    >
    > Knute Johnson wrote:
    >> It looks like he is using a period rather than a time. I haven't
    >> figured out a way to do that with DateFormat. If you know please share.

    >
    > <sscce source="eegee.PeriodFormats" >
    > package eegee;
    >
    > import java.text.DateFormat;
    > import java.text.SimpleDateFormat;
    > import java.util.Date;
    > import java.util.TimeZone;
    >
    > /** PeriodFormats. */
    > public class PeriodFormats
    > {
    > private static final long PERIOD = 3905000L;
    >
    > /** Main method.
    > * @param args <code>String []</code> command line arguments.
    > */
    > public static void main( String [] args )
    > {
    > DateFormat df = new SimpleDateFormat( "HH : mm : ss" );
    > df.setTimeZone( TimeZone.getTimeZone( "GMT" ));
    >
    > Date period = new Date( PERIOD );
    > System.out.println( df.format( period ));
    > }
    > }
    > </sscce>
    >
    > run:
    > 01 : 05 : 05
    >
    > This could run into trouble with periods longer than 24 hours, I
    > suppose. I haven't tested that.


    I find it hard to see why that should be better. The integer
    division and modulus is rather obvious the TZ GMT conversion
    is not.

    Arne
     
    Arne Vajhøj, Dec 2, 2008
    #7
  8. crazytazo

    Lew Guest

    On Dec 1, 7:55 pm, Arne Vajhøj <> wrote:
    > Lew wrote:
    > > Lew wrote:
    > >>> Even better, use java.text.DateFormat (SimpleDateFormat).

    >
    > > Knute Johnson wrote:
    > >> It looks like he is using a period rather than a time.  I haven't
    > >> figured out a way to do that with DateFormat.  If you know please share.

    >
    > > <sscce source="eegee.PeriodFormats" >
    > > package eegee;

    >
    > > import java.text.DateFormat;
    > > import java.text.SimpleDateFormat;
    > > import java.util.Date;
    > > import java.util.TimeZone;

    >
    > > /** PeriodFormats. */
    > > public class PeriodFormats
    > > {
    > >   private static final long PERIOD = 3905000L;

    >
    > >   /** Main method.
    > >    * @param args <code>String []</code> command line arguments.
    > >    */
    > >   public static void main( String [] args )
    > >   {
    > >     DateFormat df = new SimpleDateFormat( "HH : mm : ss" );
    > >     df.setTimeZone( TimeZone.getTimeZone( "GMT" ));

    >
    > >     Date period = new Date( PERIOD );
    > >     System.out.println( df.format( period ));
    > >   }
    > > }
    > > </sscce>

    >
    > > run:
    > > 01 : 05 : 05

    >
    > > This could run into trouble with periods longer than 24 hours, I
    > > suppose.  I haven't tested that.

    >
    > I find it hard to see why that should be better. The integer
    > division and modulus is rather obvious the TZ GMT conversion
    > is not.


    The TZ conversion is not strictly necessary.

    It's better in that one doesn't have to remember the "0" in the format
    string, or perform explicit calculations, leaving it to the library
    code which already has those calculations in its code. A DateFormat
    can be created once and re-used without fear of transcription error of
    the format string. It yields a String object that can be passed
    around, say from a logic element to a presentation element, without
    explicit reference to an output stream or Writer.

    --
    Lew
     
    Lew, Dec 2, 2008
    #8
  9. In article
    <>,
    Lew <> wrote:

    > On Dec 1, 7:55 pm, Arne Vajhøj <> wrote:
    > > Lew wrote:
    > > > Lew wrote:
    > > >>> Even better, use java.text.DateFormat (SimpleDateFormat).

    > >
    > > > Knute Johnson wrote:
    > > >> It looks like he is using a period rather than a time.  I haven't
    > > >> figured out a way to do that with DateFormat.  If you know please share.

    [sscce revisited below]
    > > > This could run into trouble with periods longer than 24 hours, I
    > > > suppose.  I haven't tested that.


    The DateFormat approach is very appealing, but it does wrap after
    23:59:59:999. One can use the 'D' token up to a year, but days start
    with number one.

    > > I find it hard to see why that should be better. The integer
    > > division and modulus is rather obvious the TZ GMT conversion
    > > is not.

    >
    > The TZ conversion is not strictly necessary.


    I'm missing how this can be. If I don't set the format's TimeZone
    explicitly, I see my locale's offset in each value.

    > It's better in that one doesn't have to remember the "0" in the format
    > string, or perform explicit calculations, leaving it to the library
    > code which already has those calculations in its code. A DateFormat
    > can be created once and re-used without fear of transcription error of
    > the format string. It yields a String object that can be passed
    > around, say from a logic element to a presentation element, without
    > explicit reference to an output stream or Writer.


    FWIW, Apache Commons offers these two congeners that offer similar
    advantages:

    <http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/time/Dura
    tionFormatUtils.html>
    <http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apac
    he/commons/lang/time/DurationFormatUtils.java?view=markup>

    <http://commons.apache.org/lang/api-release/org/apache/commons/lang/time/
    StopWatch.html>
    <http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apac
    he/commons/lang/time/StopWatch.java?view=markup>

    <sscce>
    import java.text.DateFormat;
    import java.util.Date;
    import java.text.SimpleDateFormat;
    import java.util.TimeZone;

    /** @author Lew, JBM */
    public class Date7 {

    private static final long MS_DAY = 24 * 60 * 60 * 1000;
    private static final DateFormat df =
    new SimpleDateFormat("D : HH : mm : ss : S");

    public static void main(String[] args) {
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    for (int i = 0; i < 5; ++i) {
    print(i * MS_DAY);
    print(i * MS_DAY + MS_DAY - 1);
    }
    }

    private static void print(long elapsed) {
    System.out.println(df.format(new Date(elapsed)));
    }
    }
    </sscce>
    <console>
    1 : 00 : 00 : 00 : 0
    1 : 23 : 59 : 59 : 999
    2 : 00 : 00 : 00 : 0
    2 : 23 : 59 : 59 : 999
    3 : 00 : 00 : 00 : 0
    3 : 23 : 59 : 59 : 999
    4 : 00 : 00 : 00 : 0
    4 : 23 : 59 : 59 : 999
    5 : 00 : 00 : 00 : 0
    5 : 23 : 59 : 59 : 999
    </console>

    --
    John B. Matthews
    trashgod at gmail dot com
    http://home.roadrunner.com/~jbmatthews/
     
    John B. Matthews, Dec 3, 2008
    #9
  10. crazytazo

    Arne Vajhøj Guest

    Lew wrote:
    > On Dec 1, 7:55 pm, Arne Vajhøj <> wrote:
    >> Lew wrote:
    >>> Lew wrote:
    >>>>> Even better, use java.text.DateFormat (SimpleDateFormat).
    >>> Knute Johnson wrote:
    >>>> It looks like he is using a period rather than a time. I haven't
    >>>> figured out a way to do that with DateFormat. If you know please share.
    >>> <sscce source="eegee.PeriodFormats" >
    >>> package eegee;
    >>> import java.text.DateFormat;
    >>> import java.text.SimpleDateFormat;
    >>> import java.util.Date;
    >>> import java.util.TimeZone;
    >>> /** PeriodFormats. */
    >>> public class PeriodFormats
    >>> {
    >>> private static final long PERIOD = 3905000L;
    >>> /** Main method.
    >>> * @param args <code>String []</code> command line arguments.
    >>> */
    >>> public static void main( String [] args )
    >>> {
    >>> DateFormat df = new SimpleDateFormat( "HH : mm : ss" );
    >>> df.setTimeZone( TimeZone.getTimeZone( "GMT" ));
    >>> Date period = new Date( PERIOD );
    >>> System.out.println( df.format( period ));
    >>> }
    >>> }
    >>> </sscce>
    >>> run:
    >>> 01 : 05 : 05
    >>> This could run into trouble with periods longer than 24 hours, I
    >>> suppose. I haven't tested that.

    >> I find it hard to see why that should be better. The integer
    >> division and modulus is rather obvious the TZ GMT conversion
    >> is not.

    >
    > The TZ conversion is not strictly necessary.


    It is.

    Else the conversion returns wrong data.

    > It's better in that one doesn't have to remember the "0" in the format
    > string, or perform explicit calculations, leaving it to the library
    > code which already has those calculations in its code.


    To remember to ask for leading zeros if you want leadings zeros
    and to do the division and modulus is rather obvious.

    > A DateFormat
    > can be created once and re-used without fear of transcription error of
    > the format string. It yields a String object that can be passed
    > around, say from a logic element to a presentation element, without
    > explicit reference to an output stream or Writer.


    You can just as well reuse a method doing the formatting the
    other way.

    And. DateFormat is not the right choice, because we do not have
    a point in time - we have a time interval. Using DateTime for that
    is a hack.

    Arne
     
    Arne Vajhøj, Dec 4, 2008
    #10
  11. crazytazo

    Lew Guest

    Arne Vajhøj wrote:
    > And. DateFormat is not the right choice, because we do not have
    > a point in time - we have a time interval. Using DateTime for that
    > is a hack.


    Yeah. You're right.

    --
    Lew
     
    Lew, Dec 4, 2008
    #11
  12. Lew wrote:
    > On Dec 1, 7:55 pm, Arne Vajhøj <> wrote:
    >> Lew wrote:
    >>> Lew wrote:
    >>>>> Even better, use java.text.DateFormat (SimpleDateFormat).
    >>> Knute Johnson wrote:
    >>>> It looks like he is using a period rather than a time. I haven't
    >>>> figured out a way to do that with DateFormat. If you know please share.
    >>> <sscce source="eegee.PeriodFormats" >
    >>> package eegee;
    >>> import java.text.DateFormat;
    >>> import java.text.SimpleDateFormat;
    >>> import java.util.Date;
    >>> import java.util.TimeZone;
    >>> /** PeriodFormats. */
    >>> public class PeriodFormats
    >>> {
    >>> private static final long PERIOD = 3905000L;
    >>> /** Main method.
    >>> * @param args <code>String []</code> command line arguments.
    >>> */
    >>> public static void main( String [] args )
    >>> {
    >>> DateFormat df = new SimpleDateFormat( "HH : mm : ss" );
    >>> df.setTimeZone( TimeZone.getTimeZone( "GMT" ));
    >>> Date period = new Date( PERIOD );
    >>> System.out.println( df.format( period ));
    >>> }
    >>> }
    >>> </sscce>
    >>> run:
    >>> 01 : 05 : 05
    >>> This could run into trouble with periods longer than 24 hours, I
    >>> suppose. I haven't tested that.

    >> I find it hard to see why that should be better. The integer
    >> division and modulus is rather obvious the TZ GMT conversion
    >> is not.

    >
    > The TZ conversion is not strictly necessary.
    >
    > It's better in that one doesn't have to remember the "0" in the format
    > string, or perform explicit calculations, leaving it to the library
    > code which already has those calculations in its code. A DateFormat
    > can be created once and re-used without fear of transcription error of
    > the format string. It yields a String object that can be passed
    > around, say from a logic element to a presentation element, without
    > explicit reference to an output stream or Writer.
    >
    > --
    > Lew
    >


    thanks.

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Dec 4, 2008
    #12
    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. Mladen Adamovic
    Replies:
    0
    Views:
    777
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,777
    Mladen Adamovic
    Dec 5, 2003
  3. Matt
    Replies:
    3
    Views:
    549
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Bruce Sam
    Replies:
    15
    Views:
    8,064
    John C. Bollinger
    Nov 19, 2004
  5. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,818
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
Loading...

Share This Page