For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2005 > Newbie Date SimpleDateFormat HELL









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Newbie Date SimpleDateFormat HELL
Nobody

2005-02-23, 4:00 am

I'm trying to create an ANSI date string to use at the end of a file
name. "20050222" for today, for example.

This is trivial in C, and it's probably trivial in Java too but I'm
getting bogged down into Date, SimpleDateFormat, FieldPosition hell.

I think I need to use SimpleDateFormat's format method [format( Date,
StringBuffer, FieldPosition )] but the FieldPosition is kicking my
arse.

I've instantiated a SimpleDateFormat object

SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMdd" ) ;
Date date = new Date() ;

StringBuffer sbuf = new StringBuffer() ;

FieldPosition fp = new FieldPosition() ; // What goes here????

There's got to be a simpler way.

Anyway, as I RTFW if anyone can point me to a more appropriate class
I'd be very grateful.
Tilman Bohn

2005-02-23, 4:00 am

In message <MPG.1c85d32932cffdef98a0f7@news.verizon.net>,
Nobody wrote on Wed, 23 Feb 2005 04:51:54 GMT:

> I'm trying to create an ANSI date string to use at the end of a file
> name. "20050222" for today, for example.

[...]
> SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMdd" ) ;
> Date date = new Date() ;


String formattedDate = sdf.format( date );

http://java.sun.com/docs/books/tuto...DateFormat.html

I don't know what you are trying to do with that Stringbuffer...

--
Cheers, Tilman

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

2005-02-23, 4:00 am

myfirstname@gmx.net wrote...
> In message <MPG.1c85d32932cffdef98a0f7@news.verizon.net>,
> Nobody wrote on Wed, 23 Feb 2005 04:51:54 GMT:
>
> [...]
>
> String formattedDate = sdf.format( date );
>
> http://java.sun.com/docs/books/tuto...DateFormat.html
>
> 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.
Tilman Bohn

2005-02-23, 4:01 pm

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

[...]
>
> 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
Tilman Bohn

2005-02-23, 4:01 pm

In message <slrnd1ofer.ko6.myfirstname@urizen.tilmanbohn.com>,
I wrote on Wed, 23 Feb 2005 09:24:59 +0100:

[...]
> DateField.Field correspoinding for that particular field. Then you can


I guess I'm still not quite awake... ;-)

--
Cheers, Tilman

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

2005-02-23, 4:02 pm

When looking at the java api make sure you look at the methods inherited
from the superclass. In particular DataFormat has

public final String format(Date date)

as Tilman pointed out. I was using that same format method as you (with the
FieldPosition argument) for months, wondering why the hell java didn't have
a simple format method, before I noticed the one above.


"Nobody" <nobody@domain.invalid> wrote in message
news:MPG.1c85ef1ee80a5d598a0fa@news.verizon.net...
> myfirstname@gmx.net wrote...
>
> 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.



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com