Re: Newbie Date SimpleDateFormat HELL

From: Tilman Bohn (myfirstname_at_gmx.net)
Date: 02/23/05


Date: Wed, 23 Feb 2005 08:25:10 GMT

In message <MPG.1c85ef1ee80a5d598a0fa@news.verizon.net>,
Nobody wrote on Wed, 23 Feb 2005 06:39:51 GMT:

[...]
>> I don't know what you are trying to do with that Stringbuffer...
>
> Oh, just fumbling through the doc for SimpleDateFormat...
>
> format
>
> public StringBuffer format(Date date,
> StringBuffer toAppendTo,
> FieldPosition pos)
>
> Formats the given Date into a date/time string and appends the result to
> the given StringBuffer.

  Ok, I wasn't fully awake when I replied and missed your remark about
wanting to append the date to some other string. But you're thinking
too complicated: just do

sbuf.append( sdf.format( date ) );

and you're done. The overloaded version of format you mention is only
useful if at a later point you need to know at what position in the
formatted StringBuffer a certain field is situated, for example to
align other output on the same column. So if you really want to use
that one, in your case it doesn't matter what FieldPosition you pass
in. You could use new FieldPosition( DateFormat.Field.MILLISECOND ) or
new FieldPosition( DateFormat.Field.YEAR )) or whatever. Even just new
FieldPosition( 0 ) should work just fine, although I'm always in favor
of typesafe enums if they're available. If, however, you really do need
the position of a given field, then use the FieldPosition object from
DateField.Field correspoinding for that particular field. Then you can
later retrieve the position of that field in the formatted string by
calling getBeginIndex() and getEndIndex() on that FieldPosition object.

  But as I said, I'd just append to the StringBuffer and be done with
it.

-- 
Cheers, Tilman
`Boy, life takes a long time to live...'      -- Steven Wright


Relevant Pages

  • Re: how to stop the subclass from overriding a method.
    ... public final String format (Object obj) { ... return format(obj, new StringBuffer (), new FieldPosition ... Java doesn't provide the faicility of multiple arguments. ...
    (comp.lang.ruby)
  • Re: Help with double arithmetic
    ... >>> I need to extract this value but ... Just wanted to get him started with format, expecting that he would look it ... > Secondly, your last line calls format, converts the StringBuffer to a ... > Standards bodies are goofy things in my estimation. ...
    (comp.lang.java.programmer)
  • Re: Searching and replacing multiple strings in text
    ... I suggested creating a Hashtable of the word pairs to be ... append the transform to a StringBuffer. ... compose words in StringBuffers by whatever rules you want. ... For an original estimate of the StringBuffer length use the original ...
    (comp.lang.java.programmer)
  • Re: Strings as value objects
    ... I don't find append() ugly at all, ... StringBuffer sb = new StringBuffer; ...
    (comp.object)
  • Re: Division of Dollar value
    ... write a loop that looks at each char in turn and discards it if it is ... $ or, and if not, append to a StringBuffer. ... Keep in mind Europeans reverse the meaning of, ...
    (comp.lang.java.programmer)